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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/am_memcache_memoize.rb', line 49 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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/am_memcache_memoize.rb', line 28 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
95 96 97 98 99 100 |
# File 'lib/am_memcache_memoize.rb', line 95 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
87 88 89 90 |
# File 'lib/am_memcache_memoize.rb', line 87 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’.
78 79 80 81 82 |
# File 'lib/am_memcache_memoize.rb', line 78 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.
65 66 67 68 69 70 71 |
# File 'lib/am_memcache_memoize.rb', line 65 def without_associations(obj) if obj.is_a?(ActiveRecord::Base) obj.remove_associations else obj end end |