Class Cachetastic::Caches::Base
In: lib/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/cachetastic/caches/base.rb, line 139
139:     def adapter
140:       a = cache_conn_instance.get(cache_name)
141:       if adapter_supported?(a.class)
142:         return a
143:       else
144:         raise Cachetastic::Errors::UnsupportedAdapter.new(cache_name, a.class)
145:       end
146:     end

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

[Source]

     # File lib/cachetastic/caches/base.rb, line 161
161:     def adapter_supported?(a = cache_conn_instance.get(cache_name).class)
162:       return !unsupported_adapters.include?(a)
163:     end

Returns a list of all registered caches in the system.

[Source]

    # File lib/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/cachetastic/caches/base.rb, line 134
134:     def cache_name
135:       self.name.methodize
136:     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/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/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/cachetastic/caches/base.rb, line 58
58:     def get(key, &block)
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/cachetastic/caches/base.rb, line 149
149:     def logger
150:       adapter.logger
151:     end

[Source]

     # File lib/cachetastic/caches/base.rb, line 165
165:     def marshall(value)
166:       return value if value.nil?
167:       return case adapter.configuration.retrieve(:marshall_method, :none).to_sym
168:       when :yaml
169:         YAML.dump(value)
170:       when :ruby
171:         Marshal.dump(value)
172:       else
173:         value
174:       end
175:     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/cachetastic/caches/base.rb, line 94
 94:     def set(key, value, expiry = adapter.configuration.retrieve(: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/cachetastic/caches/base.rb, line 126
126:     def stats
127:       adapter.stats
128:     end

[Source]

     # File lib/cachetastic/caches/base.rb, line 177
177:     def unmarshall(value)
178:       return value if value.nil?
179:       return case adapter.configuration.retrieve(:marshall_method, :none).to_sym
180:       when :yaml
181:         YAML.load(value)
182:       when :ruby
183:         Marshal.load(value)
184:       else
185:         value
186:       end
187:     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/cachetastic/caches/base.rb, line 156
156:     def unsupported_adapters
157:       []
158:     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/cachetastic/caches/base.rb, line 192
192:     def calculate_expiry_time(expiry) # :doc:
193:       exp_swing = adapter.configuration.retrieve(:expiry_swing, 0)
194:       if exp_swing && exp_swing != 0
195:         swing = rand(exp_swing.to_i)
196:         case rand(2)
197:         when 0
198:           expiry = (expiry.to_i + swing)
199:         when 1
200:           expiry = (expiry.to_i - swing)
201:         end
202:       end
203:       expiry
204:     end

[Validate]