Module: CacheCow::Cacheable::ClassMethods

Defined in:
lib/cache_cow/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#cache_key(cache_id) ⇒ Object



31
32
33
# File 'lib/cache_cow/cacheable.rb', line 31

def cache_key(cache_id)
  [cache_name, acts_as_cached_version, cache_id].compact.join(':').gsub(' ', '_')[0..(max_key_length - 1)]
end

#cache_nameObject



35
36
37
# File 'lib/cache_cow/cacheable.rb', line 35

def cache_name
  @cache_name ||= respond_to?(:base_class) ? base_class.name : name
end

#cached?(cache_id = nil) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/cache_cow/cacheable.rb', line 27

def cached?(cache_id = nil)
  Rails.cache.exist?(cache_key(cache_id))
end

#expire_cache(cache_id = nil, options = {}) ⇒ Object



23
24
25
# File 'lib/cache_cow/cacheable.rb', line 23

def expire_cache(cache_id = nil, options = {})
  Rails.cache.delete cache_key(cache_id), options
end

#fetch_cache(*args, &block) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/cache_cow/cacheable.rb', line 7

def fetch_cache(*args, &block)
  options = args.extract_options!
  keys    = args.flatten

  # raise "Doesn't support multiget" unless keys.size == 1
  Rails.cache.fetch cache_key(keys.first), &block
end

#max_key_lengthObject



39
40
41
# File 'lib/cache_cow/cacheable.rb', line 39

def max_key_length
  200
end

#read_cache(cache_id = nil, options = {}) ⇒ Object



15
16
17
# File 'lib/cache_cow/cacheable.rb', line 15

def read_cache(cache_id = nil, options = {})
  Rails.cache.read cache_key(cache_id), options
end

#write_cache(cache_id, value, options = {}) ⇒ Object



19
20
21
# File 'lib/cache_cow/cacheable.rb', line 19

def write_cache(cache_id, value, options = {})
  Rails.cache.write cache_key(cache_id), value, { :expires_in => 1500 }.merge(options)
end