garner 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -64,11 +64,15 @@ module Garner
64
64
  key(binding, ctx)
65
65
  end
66
66
  # attempt to do a read_multi if the cache supports it
67
- local_cache = keys.size > 1 && Garner.config.cache.respond_to?(:read_multi) ? Garner.config.cache.read_multi(keys) : {}
67
+ read_multi = keys.size > 1 && Garner.config.cache.respond_to?(:read_multi)
68
+ local_cache = read_multi ? Garner.config.cache.read_multi(keys) : {}
68
69
  # fetch all missing values
69
70
  bindings.each_with_index.map do |binding, index|
70
71
  key = keys[index]
71
- local_cache[key] || fetch(key, binding, cache_options, &block)
72
+ # just write the value if the key was not fetched in read_multi
73
+ local_cache[key] || (read_multi ?
74
+ write(key, binding, cache_options, &block) :
75
+ fetch(key, binding, cache_options, &block))
72
76
  end
73
77
  end
74
78
 
@@ -89,6 +93,7 @@ module Garner
89
93
 
90
94
  private
91
95
 
96
+ # fetch an object from cache, write if not present
92
97
  def fetch(key, binding, cache_options = {}, &block)
93
98
  result = Garner.config.cache.fetch(key, cache_options) do
94
99
  object = binding ? yield(binding) : yield
@@ -99,6 +104,17 @@ module Garner
99
104
  result
100
105
  end
101
106
 
107
+ # write an object to cache
108
+ def write(key, binding, cache_options = {}, &block)
109
+ object = binding ? yield(binding) : yield
110
+ if object
111
+ Garner.config.cache.write(key, object, cache_options)
112
+ reset_cache_metadata key, object
113
+ end
114
+ Garner.config.cache.delete(key) unless object
115
+ object
116
+ end
117
+
102
118
  # applied cache options
103
119
  def apply_cache_options(context)
104
120
  cache_options = context[:cache_options] || {}
@@ -1,3 +1,3 @@
1
1
  module Garner
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-24 00:00:00.000000000 Z
13
+ date: 2013-01-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -279,7 +279,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
279
279
  version: '0'
280
280
  segments:
281
281
  - 0
282
- hash: 4499796191639143111
282
+ hash: -2019166628533219668
283
283
  required_rubygems_version: !ruby/object:Gem::Requirement
284
284
  none: false
285
285
  requirements: