Module: MemcacheMemoize
- Defined in:
- lib/am_memcache_memoize.rb
Instance Method Summary collapse
-
#cache_read(basename, args = {}) ⇒ Object
Only read a value from memcache.
-
#cache_value(basename, expiry = 0, args = {}, &cached_code) ⇒ Object
The following is used to cache, using memcache, the results of executing a code block.
-
#check_cached_value(basename, args = {}) ⇒ Object
Return true if there is such a value in the memcache, otherwise false.
-
#expire_cached_namespace(namespace_string) ⇒ Object
Helper method to invalidate namespaces.
-
#expire_cached_value(basename, expiry = 0, args = {}) ⇒ Object
This method is used to expire cached data.
-
#without_associations(obj) ⇒ Object
Returns the obj, unless the obj is_a?(ActiveRecord::Base), in which case it will return the object without associations.
Instance Method Details
#cache_read(basename, args = {}) ⇒ Object
Only read a value from memcache
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/am_memcache_memoize.rb', line 35 def cache_read(basename, args={}) return nil if $DISABLE_CACHE key = compute_memcache_key(basename, args) result = CACHE.get key RAILS_DEFAULT_LOGGER.debug "MemCache: Read #{key.inspect}" if result == :MemCache_no_such_entry nil else result end end |
#cache_value(basename, expiry = 0, args = {}, &cached_code) ⇒ Object
The following is used to cache, using memcache, the results of executing a code block. The scope of the caching is determined by the basename and the hash args. For instance:
cache_value(:unread_messages, 0, :user => @user.id) do
Messages.find(:all, :conditions => ......).size
end
will store the result of the block under the key “unread_messages:user:123” in memcache, for ever (expiry=0 seconds, meaning no expiration time). The number of hash pairs is unlimited. Duplicates will be removed.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/am_memcache_memoize.rb', line 14 def cache_value(basename, expiry=0, args={}, &cached_code) return cached_code.call if $DISABLE_CACHE key = compute_memcache_key(basename, args) # Check if the value is cached result = CACHE.get key if result == :MemCache_no_such_entry # No, we need to call the block and cache the result result = cached_code.call CACHE.set key, result, expiry RAILS_DEFAULT_LOGGER.debug "MemCache: Computed and set value for #{key.inspect} (expiry: #{expiry} seconds)" else RAILS_DEFAULT_LOGGER.debug "MemCache: Got #{key.inspect}" end # Return the result result end |
#check_cached_value(basename, args = {}) ⇒ Object
Return true if there is such a value in the memcache, otherwise false
81 82 83 84 85 86 |
# File 'lib/am_memcache_memoize.rb', line 81 def check_cached_value(basename, args={}) key = compute_memcache_key(basename, args) rval = CACHE.check(key) RAILS_DEFAULT_LOGGER.debug "MemCache: Checked for #{key.inspect} - returned: " + rval.to_s return rval end |
#expire_cached_namespace(namespace_string) ⇒ Object
Helper method to invalidate namespaces
73 74 75 76 |
# File 'lib/am_memcache_memoize.rb', line 73 def expire_cached_namespace(namespace_string) RAILS_DEFAULT_LOGGER.debug "MemCache: Invalidating namespace: #{namespace_string.to_s}" CACHE.invalidate_namespace(namespace_string) end |
#expire_cached_value(basename, expiry = 0, args = {}) ⇒ Object
This method is used to expire cached data. Basename and args are as per cache_value(), but expiry controls in how many seconds the expiration is to take place. The default value of 0 means ‘immediately’.
64 65 66 67 68 |
# File 'lib/am_memcache_memoize.rb', line 64 def expire_cached_value(basename, expiry=0, args={}) key = compute_memcache_key(basename, args) RAILS_DEFAULT_LOGGER.debug "MemCache: Invalidating #{key.inspect}" CACHE.delete key, expiry end |
#without_associations(obj) ⇒ Object
Returns the obj, unless the obj is_a?(ActiveRecord::Base), in which case it will return the object without associations.
51 52 53 54 55 56 57 |
# File 'lib/am_memcache_memoize.rb', line 51 def without_associations(obj) if obj.is_a?(ActiveRecord::Base) obj.remove_associations else obj end end |