Class: IdentityCache::MemoizedCacheProxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memcache = nil) ⇒ MemoizedCacheProxy

Returns a new instance of MemoizedCacheProxy.



7
8
9
10
# File 'lib/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

Parameters:

  • value

    the value to set the attribute memcache to.



5
6
7
# File 'lib/memoized_cache_proxy.rb', line 5

def memcache=(value)
  @memcache = value
end

Instance Method Details

#clearObject



56
57
58
59
# File 'lib/memoized_cache_proxy.rb', line 56

def clear
  clear_memoization
  @memcache.clear
end

#delete(key) ⇒ Object



37
38
39
40
# File 'lib/memoized_cache_proxy.rb', line 37

def delete(key)
  memoized_key_values.delete(key) if memoizing?
  @memcache.delete(key)
end

#memoized_key_valuesObject



12
13
14
# File 'lib/memoized_cache_proxy.rb', line 12

def memoized_key_values
  @key_value_maps[Thread.current.object_id]
end

#read(key) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/memoized_cache_proxy.rb', line 29

def read(key)
  if memoizing?
    memoized_key_values[key] ||= @memcache.read(key)
  else
    @memcache.read(key)
  end
end

#read_multi(*keys) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/memoized_cache_proxy.rb', line 42

def read_multi(*keys)
  hash = {}

  if memoizing?
    keys.reduce({}) do |hash, key|
      hash[key] = memoized_key_values[key] if memoized_key_values[key].present?
      hash
    end
  end

  missing_keys = keys - hash.keys
  hash.merge(@memcache.read_multi(*missing_keys))
end

#with_memoization(&block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/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/memoized_cache_proxy.rb', line 24

def write(key, value)
  memoized_key_values[key] = value if memoizing?
  @memcache.write(key, value)
end