garner 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -196,6 +196,7 @@ The full list of `Garner.config` attributes is:
196
196
  * `:binding_invalidation_strategy`: Binding invalidation strategy. Defaults to `Garner::Strategies::Binding::Invalidation::Touch`.
197
197
  * `:mongoid_identity_fields`: Identity fields considered legal for the `identity` method. Defaults to `[:_id]`.
198
198
  * `:caller_root`: Root path of application, to be stripped out of value strings generated by the `Caller` context key strategy. Defaults to `Rails.root` if in a Rails environment; otherwise to the nearest ancestor directory containing a Gemfile.
199
+ * `:invalidate_mongoid_root`: If set to true, invalidates the `_root` document along with any embedded Mongoid document binding. Defaults to `true`.
199
200
 
200
201
  Contributing
201
202
  ------------
@@ -8,6 +8,9 @@ Garner.config.option(:binding_invalidation_strategy, {
8
8
  Garner.config.option(:mongoid_identity_fields, {
9
9
  :default => [:_id]
10
10
  })
11
+ Garner.config.option(:invalidate_mongoid_root, {
12
+ :default => true
13
+ })
11
14
 
12
15
  module Garner
13
16
  module Cache
@@ -38,27 +41,25 @@ module Garner
38
41
  #
39
42
  # @return [Boolean] Returns true on success.
40
43
  def invalidate_garner_caches
41
- invalidation_strategy.apply(self)
44
+ _invalidate
42
45
  true
43
46
  end
44
47
 
45
48
  protected
49
+ def _invalidate
50
+ invalidation_strategy.apply(self)
51
+ end
52
+
46
53
  def _garner_after_create
47
- if invalidation_strategy.apply_on_callback?(:create)
48
- invalidation_strategy.apply(self)
49
- end
54
+ _invalidate if invalidation_strategy.apply_on_callback?(:create)
50
55
  end
51
56
 
52
57
  def _garner_after_update
53
- if invalidation_strategy.apply_on_callback?(:update)
54
- invalidation_strategy.apply(self)
55
- end
58
+ _invalidate if invalidation_strategy.apply_on_callback?(:update)
56
59
  end
57
60
 
58
61
  def _garner_after_destroy
59
- if invalidation_strategy.apply_on_callback?(:destroy)
60
- invalidation_strategy.apply(self)
61
- end
62
+ _invalidate if invalidation_strategy.apply_on_callback?(:destroy)
62
63
  end
63
64
 
64
65
  end
@@ -23,7 +23,7 @@ module Garner
23
23
  # end
24
24
  # @return [Garner::Cache::Identity] The cache identity.
25
25
  def garner(&block)
26
- identity = Garner::Cache::Identity.new
26
+ identity = Garner::Cache::Identity.new(self)
27
27
  Garner.config.context_key_strategies.each do |strategy|
28
28
  identity = strategy.apply(identity, self)
29
29
  end
@@ -1,9 +1,12 @@
1
1
  module Garner
2
2
  module Cache
3
3
  class Identity
4
+ attr_accessor :ruby_context
4
5
  attr_accessor :bindings, :key_hash, :options_hash
5
6
 
6
- def initialize
7
+ def initialize(ruby_context = nil)
8
+ @ruby_context = ruby_context
9
+
7
10
  @bindings = []
8
11
  @key_hash = {}
9
12
 
@@ -13,7 +16,17 @@ module Garner
13
16
  end
14
17
 
15
18
  def fetch(&block)
16
- Garner::Cache.fetch(@bindings, @key_hash, @options_hash, &block)
19
+ if @nocache
20
+ yield
21
+ else
22
+ Garner::Cache.fetch(@bindings, @key_hash, @options_hash, &block)
23
+ end
24
+ end
25
+
26
+ # Disable caching for this identity.
27
+ def nocache
28
+ @nocache = true
29
+ block_given? ? fetch(&block) : self
17
30
  end
18
31
 
19
32
  # Bind this cache identity to a (bindable) object.
@@ -62,8 +62,16 @@ module Garner
62
62
  :updated_at => :desc
63
63
  }).first
64
64
  end
65
- end
66
65
 
66
+ def _invalidate
67
+ invalidation_strategy.apply(self)
68
+
69
+ if _root != self && Garner.config.invalidate_mongoid_root
70
+ invalidation_strategy.apply(_root)
71
+ end
72
+ end
73
+
74
+ end
67
75
  end
68
76
  end
69
77
  end
@@ -19,14 +19,12 @@ module Garner
19
19
  # end
20
20
  # @return [Garner::Cache::Identity] The cache identity.
21
21
  def garner(&block)
22
- identity = Garner::Cache::Identity.new
22
+ identity = Garner::Cache::Identity.new(self)
23
23
  Garner.config.rack_context_key_strategies.each do |strategy|
24
24
  identity = strategy.apply(identity, self)
25
25
  end
26
26
 
27
- unless cache_enabled?
28
- identity.options({ :force_miss => true })
29
- end
27
+ identity = identity.nocache unless cache_enabled?
30
28
 
31
29
  block_given? ? identity.fetch(&block) : identity
32
30
  end
@@ -26,6 +26,7 @@ module Garner
26
26
  # @return [String] A cache key string.
27
27
  def self.fetch_cache_key_for(binding)
28
28
  canonical_binding = fetch_canonical_binding_for(binding)
29
+ return nil unless canonical_binding
29
30
  key = index_key_for(canonical_binding)
30
31
  Garner.config.cache.fetch(key) { new_cache_key_for(canonical_binding) }
31
32
  end
@@ -36,6 +37,7 @@ module Garner
36
37
  # @return [String] A cache key string.
37
38
  def self.write_cache_key_for(binding)
38
39
  canonical_binding = fetch_canonical_binding_for(binding)
40
+ return nil unless canonical_binding
39
41
  key = index_key_for(canonical_binding)
40
42
  value = new_cache_key_for(canonical_binding)
41
43
  value.tap { |v| Garner.config.cache.write(key, v) }
@@ -10,7 +10,7 @@ module Garner
10
10
  # @param identity [Garner::Cache::Identity] The cache identity.
11
11
  # @param ruby_context [Object] An optional Ruby context.
12
12
  # @return [Garner::Cache::Identity] The modified identity.
13
- def self.apply(identity, ruby_context = self)
13
+ def self.apply(identity, ruby_context = nil)
14
14
  identity
15
15
  end
16
16
 
@@ -36,7 +36,7 @@ module Garner
36
36
  # @param identity [Garner::Cache::Identity] The cache identity.
37
37
  # @param ruby_context [Object] An optional Ruby context.
38
38
  # @return [Garner::Cache::Identity] The modified identity.
39
- def self.apply(identity, ruby_context = self)
39
+ def self.apply(identity, ruby_context = nil)
40
40
  value = nil
41
41
 
42
42
  if ruby_context.send(:caller)
@@ -13,7 +13,7 @@ module Garner
13
13
  # @param identity [Garner::Cache::Identity] The cache identity.
14
14
  # @param ruby_context [Object] An optional Ruby context.
15
15
  # @return [Garner::Cache::Identity] The modified identity.
16
- def self.apply(identity, ruby_context = self)
16
+ def self.apply(identity, ruby_context = nil)
17
17
  key_hash = identity.key_hash
18
18
  return identity unless key_hash[field]
19
19
 
@@ -13,7 +13,7 @@ module Garner
13
13
  # @param identity [Garner::Cache::Identity] The cache identity.
14
14
  # @param ruby_context [Object] An optional Ruby context.
15
15
  # @return [Garner::Cache::Identity] The modified identity.
16
- def self.apply(identity, ruby_context = self)
16
+ def self.apply(identity, ruby_context = nil)
17
17
  return identity unless (ruby_context.respond_to?(:request))
18
18
 
19
19
  request = ruby_context.request
@@ -13,7 +13,7 @@ module Garner
13
13
  # @param identity [Garner::Cache::Identity] The cache identity.
14
14
  # @param ruby_context [Object] An optional Ruby context.
15
15
  # @return [Garner::Cache::Identity] The modified identity.
16
- def self.apply(identity, ruby_context = self)
16
+ def self.apply(identity, ruby_context = nil)
17
17
  return identity unless (ruby_context.respond_to?(:request))
18
18
 
19
19
  request = ruby_context.request
@@ -13,7 +13,7 @@ module Garner
13
13
  # @param identity [Garner::Cache::Identity] The cache identity.
14
14
  # @param ruby_context [Object] An optional Ruby context.
15
15
  # @return [Garner::Cache::Identity] The modified identity.
16
- def self.apply(identity, ruby_context = self)
16
+ def self.apply(identity, ruby_context = nil)
17
17
  return identity unless (ruby_context.respond_to?(:request))
18
18
 
19
19
  request = ruby_context.request
@@ -1,3 +1,3 @@
1
1
  module Garner
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
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.4.2
4
+ version: 0.4.3
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-06-29 00:00:00.000000000 Z
13
+ date: 2013-07-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -400,7 +400,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
400
400
  version: '0'
401
401
  segments:
402
402
  - 0
403
- hash: -3941759339278993047
403
+ hash: -4381547871951397125
404
404
  required_rubygems_version: !ruby/object:Gem::Requirement
405
405
  none: false
406
406
  requirements: