Skip to content

Commit 315e7c6

Browse files
author
Daniel Leong
committed
Merge pull request #27 from Daniel-ltw/feature/race_condition_ttl_configuration
Feature/race condition ttl configuration
2 parents 6974266 + ce8ca15 commit 315e7c6

File tree

5 files changed

+10
-2
lines changed

5 files changed

+10
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ CachedResource accepts the following options:
3636

3737
* `:enabled` Default: `true`
3838
* `:ttl` The time in seconds until the cache should expire. Default: `604800`
39+
* `:race_condition_ttl` The race condition ttl, to prevent `dog pile effect(https://en.wikipedia.org/wiki/Cache_stampede)` or `cache stampede(https://en.wikipedia.org/wiki/Cache_stampede)`. Default: 86400
3940
* `:ttl_randomization` Enable ttl randomization. Default: `false`
4041
* `:ttl_randomization_scale` A Range from which a random value will be selected to scale the ttl. Default: `1..2`
4142
* `:collection_synchronize` Use collections to generate cache entries for individuals. Update the existing cached principal collection when retrieving subsets of the principal collection or individuals. Default: `false`

lib/cached_resource/caching.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def cache_read(key)
9898

9999
# Write an entry to the cache for the given key and value.
100100
def cache_write(key, object)
101-
result = cached_resource.cache.write(key, object, :expires_in => cached_resource.generate_ttl)
101+
result = cached_resource.cache.write(key, object, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
102102
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
103103
result
104104
end

lib/cached_resource/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Configuration < OpenStruct
2020
# defaults. The following options exist for cached resource:
2121
# :enabled, default: true
2222
# :ttl, default: 604800
23+
# :race_condition_ttl: 86400
2324
# :ttl_randomization, default: false,
2425
# :ttl_randomization_scale, default: 1..2,
2526
# :collection_synchronize, default: false,
@@ -29,6 +30,7 @@ class Configuration < OpenStruct
2930
def initialize(options={})
3031
super({
3132
:enabled => true,
33+
:race_condition_ttl => 86400,
3234
:ttl => 604800,
3335
:ttl_randomization => false,
3436
:ttl_randomization_scale => 1..2,

lib/cached_resource/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module CachedResource
2-
VERSION = "4.1.0"
2+
VERSION = "4.2.0"
33
end

spec/cached_resource/configuration_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
before(:each) do
6767
class Foo < ActiveResource::Base
6868
cached_resource :ttl => 1,
69+
:race_condition_ttl => 5,
6970
:cache => "cache",
7071
:logger => "logger",
7172
:enabled => false,
@@ -82,6 +83,7 @@ class Foo < ActiveResource::Base
8283
it "should relfect the specified options" do
8384
cr = Foo.cached_resource
8485
cr.ttl.should == 1
86+
expect(cr.race_condition_ttl).to eq(5)
8587
cr.cache.should == "cache"
8688
cr.logger.should == "logger"
8789
cr.enabled.should == false
@@ -122,6 +124,7 @@ class Bar < ActiveResource::Base
122124
before(:each) do
123125
class Bar < ActiveResource::Base
124126
cached_resource :ttl => 1,
127+
:race_condition_ttl => 5,
125128
:cache => "cache",
126129
:logger => "logger",
127130
:enabled => false,
@@ -149,6 +152,7 @@ class Foo < Bar
149152
before(:each) do
150153
class Bar < ActiveResource::Base
151154
cached_resource :ttl => 1,
155+
:race_condition_ttl => 5,
152156
:cache => "cache",
153157
:logger => "logger",
154158
:enabled => false,
@@ -182,6 +186,7 @@ class Foo < Bar
182186
cr.custom.should == nil
183187
cr.ttl_randomization.should == false
184188
cr.ttl_randomization_scale.should == (1..2)
189+
expect(cr.race_condition_ttl).to eq(86400)
185190
end
186191

187192
end

0 commit comments

Comments
 (0)