Class: IdentityCache::MemoizedCacheProxy
- Inherits:
-
Object
- Object
- IdentityCache::MemoizedCacheProxy
- Defined in:
- lib/identity_cache/memoized_cache_proxy.rb
Instance Attribute Summary collapse
-
#memcache ⇒ Object
writeonly
Sets the attribute memcache.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(memcache = nil) ⇒ MemoizedCacheProxy
constructor
A new instance of MemoizedCacheProxy.
- #memoized_key_values ⇒ Object
- #read(key) ⇒ Object
- #read_multi(*keys) ⇒ Object
- #with_memoization(&block) ⇒ Object
- #write(key, value) ⇒ Object
Constructor Details
#initialize(memcache = nil) ⇒ MemoizedCacheProxy
Returns a new instance of MemoizedCacheProxy.
7 8 9 10 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 7 def initialize(memcache = nil) @memcache = memcache || Rails.cache @key_value_maps = Hash.new {|h, k| h[k] = {} } end |
Instance Attribute Details
#memcache=(value) ⇒ Object (writeonly)
Sets the attribute memcache
5 6 7 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 5 def memcache=(value) @memcache = value end |
Instance Method Details
#clear ⇒ Object
103 104 105 106 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 103 def clear clear_memoization @memcache.clear end |
#delete(key) ⇒ Object
54 55 56 57 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 54 def delete(key) memoized_key_values.delete(key) if memoizing? @memcache.delete(key) end |
#memoized_key_values ⇒ Object
12 13 14 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 12 def memoized_key_values @key_value_maps[Thread.current] end |
#read(key) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 29 def read(key) used_memcached = true result = if memoizing? used_memcached = false mkv = memoized_key_values mkv.fetch(key) do used_memcached = true mkv[key] = @memcache.read(key) end else @memcache.read(key) end if result IdentityCache.logger.debug { "[IdentityCache] #{ used_memcached ? '(memcache)' : '(memoized)' } cache hit for #{key}" } else IdentityCache.logger.debug { "[IdentityCache] cache miss for #{key}" } end result end |
#read_multi(*keys) ⇒ Object
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 59 def read_multi(*keys) if IdentityCache.logger.debug? memoized_keys , memcache_keys = [], [] end result = if memoizing? hash = {} mkv = memoized_key_values missing_keys = keys.reject do |key| if mkv.has_key?(key) memoized_keys << key if IdentityCache.logger.debug? hit = mkv[key] hash[key] = hit unless hit.nil? true end end hits = missing_keys.empty? ? {} : @memcache.read_multi(*missing_keys) missing_keys.each do |key| hit = hits[key] mkv[key] = hit hash[key] = hit unless hit.nil? end hash else @memcache.read_multi(*keys) end if IdentityCache.logger.debug? result.each do |k, v| memcache_keys << k if !v.nil? && !memoized_keys.include?(k) end memoized_keys.each{ |k| IdentityCache.logger.debug "[IdentityCache] (memoized) cache hit for #{k} (multi)" } memcache_keys.each{ |k| IdentityCache.logger.debug "[IdentityCache] (memcache) cache hit for #{k} (multi)" } end result end |
#with_memoization(&block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 16 def with_memoization(&block) Thread.current[:memoizing_idc] = true yield ensure clear_memoization Thread.current[:memoizing_idc] = false end |
#write(key, value) ⇒ Object
24 25 26 27 |
# File 'lib/identity_cache/memoized_cache_proxy.rb', line 24 def write(key, value) memoized_key_values[key] = value if memoizing? @memcache.write(key, value) end |