Class Cachetastic::Caches::Base
In: lib/caches/cachetastic_caches_base.rb
Parent: Object

When creating a new ‘Cache’ this class should be extended. Once extended you‘ll only need to override just the methods that are different for your cache.

  class MyAwesomeCache < Cachetastic::Caches::Base
  end
  MyAwesomeCache.set(1, "One")
  MyAwesomeCache.get(1) # => "One"
  MyAwesomeCache.update(1, "One!!")
  MyAwesomeCache.get(1) # => "One!!"
  MyAwesomeCache.delete(1)
  MyAwesomeCache.get(1) # => nil

  class MyAwesomeCache < Cachetastic::Caches::Base
    class << self
      def get(key)
        super(key) do
          set(key, key * 10)
        end
      end
    end
  end
  MyAwesomeCache.set(1, "One")
  MyAwesomeCache.get(1) # => "One"
  MyAwesomeCache.delete(1)
  MyAwesomeCache.get(1) # => 10

Methods

Classes and Modules

Class Cachetastic::Caches::Base::RegisteredCaches

External Aliases

set -> put

Public Class methods

Returns the underlying Cachetastic::Adapters::Base for this cache.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 141
141:     def adapter
142:       a = cache_conn_instance.get(cache_name)
143:       if adapter_supported?(a.class)
144:         return a
145:       else
146:         raise Cachetastic::Errors::UnsupportedAdapter.new(cache_name, a.class)
147:       end
148:     end

Returns true/false on whether the adapter you want to use is supported for the cache.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 163
163:     def adapter_supported?(a = cache_conn_instance.get(cache_name).class)
164:       return !unsupported_adapters.include?(a)
165:     end

Returns a list of all registered caches in the system.

[Source]

    # File lib/caches/cachetastic_caches_base.rb, line 48
48:     def all_registered_caches
49:       RegisteredCaches.instance.list
50:     end

Returns a ‘methodize’ version of the cache‘s class name. This gets used in logging, namespacing, and as the key in the Cachetastic::Connection class.

  MyAwesomeCache.cache # => "my_awesome_cache"
  Cachetastic::Caches::Base # => "cachetastic_caches_base"

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 136
136:     def cache_name
137:       self.name.methodize
138:     end

Deletes an object from the cache. The optional delay parameter sets an offset, in seconds, for when the object should get deleted. The default of 0 means the object gets deleted right away.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 108
108:     def delete(key, delay = 0)
109:       do_with_logging(:delete, key) do
110:         adapter.delete(key.to_s, delay)
111:       end
112:     end

Expires all objects for this cache.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 115
115:     def expire_all
116:       adapter.expire_all
117:       logger.info('', '', :expired, cache_name)
118:     end

Returns an object from the cache for a given key. If the object comes back as nil and a block is given that block will be run and the results of the block will be run. This can be used to JIT caches, just make sure in the block to call the set method because the results of the block are not automatically cached.

[Source]

    # File lib/caches/cachetastic_caches_base.rb, line 58
58:     def get(key)
59:       res = nil
60:       do_with_logging(:get, key) do
61:         retryable(:on => ArgumentError) do
62:           begin
63:             res = adapter.get(key.to_s)
64:             if res.nil?
65:               res = yield key if block_given?
66:             else
67:               res = unmarshall(res)
68:             end
69:             res
70:           rescue ArgumentError => e
71:             m = e.message.match(/class\/module .*/)
72:             if m
73:               m = m.to_s
74:               m.gsub!("class/module", '')
75:               m.gsub!("(ArgumentError)", '')
76:               require m.strip.underscore
77:               raise e
78:             end
79:           rescue Exception => e
80:             raise e
81:           end
82:         end
83:       end
84:       res
85:     end

Returns the Cachetastic::Logger for the underlying Cachetastic::Adapters::Base.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 151
151:     def logger
152:       adapter.logger
153:     end

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 167
167:     def marshall(value)
168:       return value if value.nil?
169:       return case adapter.all_options["marshall_method"]
170:       when "yaml"
171:         YAML.dump(value)
172:       when "ruby"
173:         Marshal.dump(value)
174:       else
175:         value
176:       end
177:     end

Raises a MethodNotImplemented exception. This method should be overridden in the child class to enable a populating the cache with all things that should be in there.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 123
123:     def populate_all
124:       raise MethodNotImplemented.new("populate_all")
125:     end

Set a particular object info the cache for the given key. An optional third parameter sets the expiry time for the object in the cache. The default for this expiry is set as either 0, meaning it never expires, or if there‘s a default_expiry time set in the config file, that file will be used. If there is an expiry_swing set in the config file it will be +/- to the expiry time. See also: calculate_expiry_time

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 94
 94:     def set(key, value, expiry = (adapter.all_options["default_expiry"] || 0))
 95:       do_with_logging(:set, key) do
 96:         expiry = calculate_expiry_time(expiry)
 97:         adapter.set(key.to_s, marshall(value), expiry.to_i)
 98:         logger.info('', '', :expiry, cache_name, key, expiry.to_i)
 99:         value
100:       end
101:     end

A convenience method that returns statistics for the underlying Cachetastic::Adapters::Base for the cache.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 128
128:     def stats
129:       adapter.stats
130:     end

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 179
179:     def unmarshall(value)
180:       return value if value.nil?
181:       return case adapter.all_options["marshall_method"]
182:       when "yaml"
183:         YAML.load(value)
184:       when "ruby"
185:         Marshal.load(value)
186:       else
187:         value
188:       end
189:     end

Returns an array of unsupported adapters for this cache. Defaults to an empty array which will let any adapter be used by the cache. Override in your specific cache to prevent certain adapters.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 158
158:     def unsupported_adapters
159:       []
160:     end

Private Class methods

If the expiry time is set to 60 minutes and the expiry_swing time is set to 15 minutes, this method will return a number between 45 minutes and 75 minutes.

[Source]

     # File lib/caches/cachetastic_caches_base.rb, line 194
194:     def calculate_expiry_time(expiry) # :doc:
195:       exp_swing = adapter.all_options["expiry_swing"]
196:       if exp_swing
197:         swing = rand(exp_swing.to_i)
198:         case rand(2)
199:         when 0
200:           expiry = (expiry.to_i + swing)
201:         when 1
202:           expiry = (expiry.to_i - swing)
203:         end
204:       end
205:       expiry
206:     end

[Validate]