Class: PrefixedCacheStore

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/prefixed_cache_store.rb

Overview

A caching store that will expire only the keys saved through it, by using a common prefix for the namespace and the version number that can be ratched up to “unlink” all the related keys. It assumes that the keys are being evicted automatically if they do not get used often.

Constant Summary collapse

VERSION =
'0.2.1'
RETAIN_PREFIX_FOR_SECONDS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, prefix = 'pfx') ⇒ PrefixedCacheStore

Returns a new instance of PrefixedCacheStore.



22
23
24
25
# File 'lib/prefixed_cache_store.rb', line 22

def initialize(store, prefix = 'pfx')
  @store = store
  @prefix = prefix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



14
15
16
# File 'lib/prefixed_cache_store.rb', line 14

def prefix
  @prefix
end

#storeObject (readonly)

Returns the value of attribute store.



14
15
16
# File 'lib/prefixed_cache_store.rb', line 14

def store
  @store
end

Instance Method Details

#clear(options = nil) ⇒ Object

Bump the version prefix making all keys obsolete.



82
83
84
85
86
# File 'lib/prefixed_cache_store.rb', line 82

def clear(options=nil)
  bump_version! # First bump the version
  @last_prefix = nil # Then make sure the cached version number will not be used
  get_and_set_current_version
end

#current_version_numberObject

Returns the current version that has been set for this store



89
90
91
# File 'lib/prefixed_cache_store.rb', line 89

def current_version_number
  get_and_set_current_version
end

#decrement(name, amount = 1, options = nil) ⇒ Object

Decrement a cached value.



77
78
79
# File 'lib/prefixed_cache_store.rb', line 77

def decrement(name, amount = 1, options=nil)
  @store.decrement(prefix_key(name), amount, options)
end

#delete(name, options = nil) ⇒ Object



55
56
57
# File 'lib/prefixed_cache_store.rb', line 55

def delete(name, options=nil)
  @store.delete(prefix_key(name), options)
end

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/prefixed_cache_store.rb', line 51

def exist?(name, options=nil)
  @store.exist?(prefix_key(name), options)
end

#fetch(name, options = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/prefixed_cache_store.rb', line 27

def fetch(name, options=nil)
  if block_given?
    @store.fetch(prefix_key(name), options) { yield }
  else
    @store.fetch(prefix_key(name), options)
  end
end

#fetch_multi(*names) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/prefixed_cache_store.rb', line 35

def fetch_multi(*names)
  options = names.extract_options!
  prefixed_keys = names.map{|e| prefix_key(e) }
  @store.fetch_multi(*prefixed_keys, options) do | prefixed_key |
    yield(unprefix_key(prefixed_key))
  end
end

#increment(name, amount = 1, options = nil) ⇒ Object

Increment a cached value.



72
73
74
# File 'lib/prefixed_cache_store.rb', line 72

def increment(name, amount = 1, options=nil)
  @store.increment(prefix_key(name), amount, options)
end

#read(name, options = nil) ⇒ Object



43
44
45
# File 'lib/prefixed_cache_store.rb', line 43

def read(name, options=nil)
  @store.read(prefix_key(name), options)
end

#read_multi(*names) ⇒ Object

Reads multiple keys from the cache using a single call to the servers for all keys. Keys must be Strings.



61
62
63
64
65
66
67
68
69
# File 'lib/prefixed_cache_store.rb', line 61

def read_multi(*names)
  names.extract_options!
  prefixed_names = names.map{|e| prefix_key(e) }
  result = @store.read_multi(*prefixed_names)
  # Unprefix the keys received
  result.inject({}) do |memo, (prefixed_key, value)|
    memo.merge(unprefix_key(prefixed_key) => value)
  end
end

#write(name, value, options = nil) ⇒ Object



47
48
49
# File 'lib/prefixed_cache_store.rb', line 47

def write(name, value, options=nil)
  @store.write(prefix_key(name), value, options)
end