Skip to content

Commit

Permalink
Merge pull request #27 from Daniel-ltw/feature/race_condition_ttl_con…
Browse files Browse the repository at this point in the history
…figuration

Feature/race condition ttl configuration
  • Loading branch information
Daniel Leong committed Nov 19, 2015
2 parents 6974266 + ce8ca15 commit 315e7c6
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CachedResource accepts the following options:

* `:enabled` Default: `true`
* `:ttl` The time in seconds until the cache should expire. Default: `604800`
* `: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
* `:ttl_randomization` Enable ttl randomization. Default: `false`
* `:ttl_randomization_scale` A Range from which a random value will be selected to scale the ttl. Default: `1..2`
* `: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`
Expand Down
2 changes: 1 addition & 1 deletion lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def cache_read(key)

# Write an entry to the cache for the given key and value.
def cache_write(key, object)
result = cached_resource.cache.write(key, object, :expires_in => cached_resource.generate_ttl)
result = cached_resource.cache.write(key, object, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
result
end
Expand Down
2 changes: 2 additions & 0 deletions lib/cached_resource/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Configuration < OpenStruct
# defaults. The following options exist for cached resource:
# :enabled, default: true
# :ttl, default: 604800
# :race_condition_ttl: 86400
# :ttl_randomization, default: false,
# :ttl_randomization_scale, default: 1..2,
# :collection_synchronize, default: false,
Expand All @@ -29,6 +30,7 @@ class Configuration < OpenStruct
def initialize(options={})
super({
:enabled => true,
:race_condition_ttl => 86400,
:ttl => 604800,
:ttl_randomization => false,
:ttl_randomization_scale => 1..2,
Expand Down
2 changes: 1 addition & 1 deletion lib/cached_resource/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CachedResource
VERSION = "4.1.0"
VERSION = "4.2.0"
end
5 changes: 5 additions & 0 deletions spec/cached_resource/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
before(:each) do
class Foo < ActiveResource::Base
cached_resource :ttl => 1,
:race_condition_ttl => 5,
:cache => "cache",
:logger => "logger",
:enabled => false,
Expand All @@ -82,6 +83,7 @@ class Foo < ActiveResource::Base
it "should relfect the specified options" do
cr = Foo.cached_resource
cr.ttl.should == 1
expect(cr.race_condition_ttl).to eq(5)
cr.cache.should == "cache"
cr.logger.should == "logger"
cr.enabled.should == false
Expand Down Expand Up @@ -122,6 +124,7 @@ class Bar < ActiveResource::Base
before(:each) do
class Bar < ActiveResource::Base
cached_resource :ttl => 1,
:race_condition_ttl => 5,
:cache => "cache",
:logger => "logger",
:enabled => false,
Expand Down Expand Up @@ -149,6 +152,7 @@ class Foo < Bar
before(:each) do
class Bar < ActiveResource::Base
cached_resource :ttl => 1,
:race_condition_ttl => 5,
:cache => "cache",
:logger => "logger",
:enabled => false,
Expand Down Expand Up @@ -182,6 +186,7 @@ class Foo < Bar
cr.custom.should == nil
cr.ttl_randomization.should == false
cr.ttl_randomization_scale.should == (1..2)
expect(cr.race_condition_ttl).to eq(86400)
end

end
Expand Down

0 comments on commit 315e7c6

Please sign in to comment.