Class: Merb::Cache::SHA1Store

Inherits:
AbstractStrategyStore show all
Defined in:
merb-cache/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

- (SHA1Store) initialize(config = {})

A new instance of SHA1Store



10
11
12
13
# File 'merb-cache/lib/merb-cache/stores/strategy/sha1_store.rb', line 10

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

Instance Method Details

- (Object) delete(key, parameters = {})



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

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

- (Object) delete_all!



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

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

- (Object) digest(key, parameters = {})



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

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

- (Boolean) exists?(key, parameters = {})

Returns:

  • (Boolean)


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

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

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



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

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

- (Object) read(key, parameters = {})



23
24
25
# File 'merb-cache/lib/merb-cache/stores/strategy/sha1_store.rb', line 23

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

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

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'merb-cache/lib/merb-cache/stores/strategy/sha1_store.rb', line 15

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

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



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

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

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



33
34
35
36
37
# File 'merb-cache/lib/merb-cache/stores/strategy/sha1_store.rb', line 33

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