Class: Merb::Cache::SHA1Store

Inherits:
AbstractStrategyStore show all
Defined in:
lib/merb-cache/stores/strategy/sha1_store.rb

Overview

Strategy store that uses SHA1 hex of base cache key and parameters as cache key.

It is good for caching of expensive search queries that use multiple parameters passed via query string of request.

Instance Attribute Summary

Attributes inherited from AbstractStrategyStore

#stores

Instance Method Summary collapse

Methods inherited from AbstractStrategyStore

#clone, contextualize

Methods inherited from AbstractStore

#delete_all

Constructor Details

#initialize(config = {}) ⇒ SHA1Store

Returns a new instance of SHA1Store.



13
14
15
16
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 13

def initialize(config = {})
  super(config)
  @map = {}
end

Instance Method Details

#delete(key, parameters = {}) ⇒ Object



50
51
52
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 50

def delete(key, parameters = {})
  @stores.map {|c| c.delete(digest(key, parameters))}.any?
end

#delete_all!Object



54
55
56
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 54

def delete_all!
  @stores.map {|c| c.delete_all! }.all?
end

#digest(key, parameters = {}) ⇒ Object



58
59
60
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 58

def digest(key, parameters = {})
  @map[[key, parameters]] ||= Digest::SHA1.hexdigest("#{key}#{parameters.to_sha2}")
end

#exists?(key, parameters = {}) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 46

def exists?(key, parameters = {})
  @stores.capture_first {|c| c.exists?(digest(key, parameters))}
end

#fetch(key, parameters = {}, conditions = {}, &blk) ⇒ Object



42
43
44
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 42

def fetch(key, parameters = {}, conditions = {}, &blk)
  read(key, parameters) || (writable?(key, parameters, conditions) && @stores.capture_first {|c| c.fetch(digest(key, parameters), {}, conditions, &blk)})
end

#read(key, parameters = {}) ⇒ Object



26
27
28
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 26

def read(key, parameters = {})
  @stores.capture_first {|c| c.read(digest(key, parameters))}
end

#writable?(key, parameters = {}, conditions = {}) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 18

def writable?(key, parameters = {}, conditions = {})
  case key
  when String, Numeric, Symbol
    @stores.any? {|c| c.writable?(digest(key, parameters), {}, conditions)}
  else nil
  end
end

#write(key, data = nil, parameters = {}, conditions = {}) ⇒ Object



30
31
32
33
34
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 30

def write(key, data = nil, parameters = {}, conditions = {})
  if writable?(key, parameters, conditions)
    @stores.capture_first {|c| c.write(digest(key, parameters), data, {}, conditions)}
  end
end

#write_all(key, data = nil, parameters = {}, conditions = {}) ⇒ Object



36
37
38
39
40
# File 'lib/merb-cache/stores/strategy/sha1_store.rb', line 36

def write_all(key, data = nil, parameters = {}, conditions = {})
  if writable?(key, parameters, conditions)
    @stores.map {|c| c.write_all(digest(key, parameters), data, {}, conditions)}.all?
  end
end