Class: Cachy

Inherits:
Object
  • Object
show all
Defined in:
lib/cachy.rb

Defined Under Namespace

Classes: MemcachedWrapper, MonetaWrapper, RedisWrapper, Wrapper

Constant Summary collapse

WHILE_RUNNING_TIMEOUT =

seconds

5*60
KEY_VERSION_TIMEOUT =

seconds

30
HEALTH_CHECK_KEY =
'cachy_healthy'
KEY_VERSIONS_KEY =
'cachy_key_versions'
@@cache_error =
false
@@locales =

locales

nil

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.hash_keysObject

Returns the value of attribute hash_keys.



113
114
115
# File 'lib/cachy.rb', line 113

def hash_keys
  @hash_keys
end

Class Method Details

.cache(*args) ⇒ Object

Cache the result of a block

Cachy.cache(:my_key){ expensive() } Cachy.cache(:my_key, :expires_in=>1.hour){ expensive() } Cachy.cache(:my_key, :keys=>){ expensive() } Cachy.cache(:my_key, :without_locale=>true){ expensive() } Cachy.cache(:my_key, :hash_key=>true){ expensive() }



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cachy.rb', line 18

def self.cache(*args)
  key = key(*args)
  options = extract_options!(args)

  result = cache_store.read(key)
  return result unless result == nil

  # Calculate result!
  set_while_running(key, options)

  result = yield
  cache_store.write key, result, options
  result
end

.cache_if(cond, *args, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/cachy.rb', line 33

def self.cache_if(cond, *args, &block)
  if cond
    cache(*args, &block)
  else
    block.call
  end
end

.cache_storeObject



123
124
125
# File 'lib/cachy.rb', line 123

def self.cache_store
  @cache_store || raise("Use: Cachy.cache_store = your_cache_store")
end

.cache_store=(cache) ⇒ Object

Wrap non ActiveSupport style cache stores, to get the same interface for all



118
119
120
121
# File 'lib/cachy.rb', line 118

def self.cache_store=(cache)
  @cache_store = wrap_cache(cache)
  @cache_store.write HEALTH_CHECK_KEY, 'yes'
end

.delete_key(key) ⇒ Object



106
107
108
109
110
# File 'lib/cachy.rb', line 106

def self.delete_key(key)
  versions = key_versions.dup
  versions.delete(key.to_sym)
  self.key_versions = versions
end

.expire(*args) ⇒ Object

Expire all possible locales of a cache, use the same arguments as with cache

Cachy.expire(:my_key, User.first) Cachy.expire(:my_key, User.first, :keys=>) Cachy.expire(:my_key, :prefix=>‘views/’)



71
72
73
74
75
76
77
78
79
# File 'lib/cachy.rb', line 71

def self.expire(*args)
  options = extract_options!(args)

  (locales+[false]).each do |locale|
    without_locale = (locale==false)
    args_with_locale = args + [options.merge(:locale=>locale, :without_locale=>without_locale)]
    cache_store.delete key(*args_with_locale)
  end
end

.expire_view(*args) ⇒ Object



81
82
83
84
85
# File 'lib/cachy.rb', line 81

def self.expire_view(*args)
  options = extract_options!(args)
  args = args + [options.merge(:prefix=>'views/')]
  expire(*args)
end

.get(*args) ⇒ Object



41
42
43
# File 'lib/cachy.rb', line 41

def self.get(*args)
  cache_store.read(key(*args))
end

.increment_key(key) ⇒ Object

Expires all caches that use this key



97
98
99
100
101
102
103
104
# File 'lib/cachy.rb', line 97

def self.increment_key(key)
  key = key.to_sym
  current_versions = read_versions
  version = current_versions[key] || 0
  version += 1
  self.key_versions = current_versions.merge(key => version)
  version
end

.key(*args) ⇒ Object

Constructs a cache-key (first argument must be a String/Symbol)

Cachy.key :my_key Cachy.key :my_key, User.first, :locale=>:de Cachy.key :my_key, User.first, :without_locale=>true, :hash_key=>true



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cachy.rb', line 50

def self.key(*args)
  options = extract_options!(args)
  ensure_valid_keys options

  key = (args + meta_key_parts(args.first, options)).compact.map do |part|
    if part.respond_to? :cache_key
      part.cache_key
    else
      part
    end
  end * "_"

  key = (options[:hash_key] || hash_keys) ? hash(key) : key
  (options[:prefix].to_s + key + options[:suffix].to_s).gsub(' ', '_')
end

.key_versionsObject



87
88
89
# File 'lib/cachy.rb', line 87

def self.key_versions
  memory_store.cache{ read_versions }
end

.key_versions=(data) ⇒ Object



91
92
93
94
# File 'lib/cachy.rb', line 91

def self.key_versions=(data)
  memory_store.clear
  write_version(data)
end

.key_versions_cache_storeObject



131
132
133
# File 'lib/cachy.rb', line 131

def self.key_versions_cache_store
  @key_versions_cache_store || cache_store
end

.key_versions_cache_store=(cache) ⇒ Object



127
128
129
# File 'lib/cachy.rb', line 127

def self.key_versions_cache_store=(cache)
  @key_versions_cache_store = wrap_cache(cache)
end

.localesObject



141
142
143
144
145
146
147
148
# File 'lib/cachy.rb', line 141

def self.locales
  return @@locales if @@locales
  if defined?(I18n) and I18n.respond_to?(:available_locales)
    I18n.available_locales
  else
    []
  end
end

.locales=(x) ⇒ Object



137
138
139
# File 'lib/cachy.rb', line 137

def self.locales=(x)
  @@locales = x
end