Class: Cachetastic::Caches::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb

Overview

MyAwesomeCache.get(1) # => 10

Direct Known Subclasses

MackSessionCache, PageCache, RailsSessionCache

Defined Under Namespace

Classes: RegisteredCaches

Class Method Summary collapse

Class Method Details

.adapterObject

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



139
140
141
142
143
144
145
146
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 139

def adapter
  a = cache_conn_instance.get(cache_name)
  if adapter_supported?(a.class)
    return a
  else
    raise Cachetastic::Errors::UnsupportedAdapter.new(cache_name, a.class)
  end
end

.adapter_supported?(a = cache_conn_instance.get(cache_name).class) ⇒ Boolean

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

Returns:

  • (Boolean)


161
162
163
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 161

def adapter_supported?(a = cache_conn_instance.get(cache_name).class)
  return !unsupported_adapters.include?(a)
end

.all_registered_cachesObject

Returns a list of all registered caches in the system.



48
49
50
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 48

def all_registered_caches
  RegisteredCaches.instance.list
end

.cache_nameObject

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"


134
135
136
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 134

def cache_name
  self.name.methodize
end

.delete(key, delay = 0) ⇒ Object

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.



108
109
110
111
112
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 108

def delete(key, delay = 0)
  do_with_logging(:delete, key) do
    adapter.delete(key.to_s, delay)
  end
end

.expire_allObject

Expires all objects for this cache.



115
116
117
118
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 115

def expire_all
  adapter.expire_all
  logger.info('', '', :expired, cache_name)
end

.get(key, &block) ⇒ Object

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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 58

def get(key, &block)
  res = nil
  do_with_logging(:get, key) do
    retryable(:on => ArgumentError) do
      begin
        res = adapter.get(key.to_s)
        if res.nil?
          res = yield key if block_given?
        else
          res = unmarshall(res)
        end
        res
      rescue ArgumentError => e
        m = e.message.match(/class\/module .*/)
        if m
          m = m.to_s
          m.gsub!("class/module", '')
          m.gsub!("(ArgumentError)", '')
          require m.strip.underscore
          raise e
        end
      rescue Exception => e
        raise e
      end
    end
  end
  res
end

.loggerObject

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



149
150
151
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 149

def logger
  adapter.logger
end

.marshall(value) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 165

def marshall(value)
  return value if value.nil?
  return case adapter.configuration.retrieve(:marshall_method, :none).to_sym
  when :yaml
    YAML.dump(value)
  when :ruby
    Marshal.dump(value)
  else
    value
  end
end

.set(key, value, expiry = adapter.configuration.retrieve(:default_expiry, 0)) ⇒ Object Also known as: put

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



94
95
96
97
98
99
100
101
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 94

def set(key, value, expiry = adapter.configuration.retrieve(:default_expiry, 0))
  do_with_logging(:set, key) do
    expiry = calculate_expiry_time(expiry)
    adapter.set(key.to_s, marshall(value), expiry.to_i)
    logger.info('', '', :expiry, cache_name, key, expiry.to_i)
    value
  end
end

.statsObject

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



126
127
128
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 126

def stats
  adapter.stats
end

.unmarshall(value) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 177

def unmarshall(value)
  return value if value.nil?
  return case adapter.configuration.retrieve(:marshall_method, :none).to_sym
  when :yaml
    YAML.load(value)
  when :ruby
    Marshal.load(value)
  else
    value
  end
end

.unsupported_adaptersObject

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.



156
157
158
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/caches/base.rb', line 156

def unsupported_adapters
  []
end