From c0fa2a255e7cdb5e2ece7fe35f90f2e12f727b56 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Fri, 30 Oct 2015 09:00:29 +1300 Subject: [PATCH 1/8] - place in race_condition_ttl option when caching - to handle dog pile effect --- lib/cached_resource/caching.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cached_resource/caching.rb b/lib/cached_resource/caching.rb index c9e7439..72fe75d 100644 --- a/lib/cached_resource/caching.rb +++ b/lib/cached_resource/caching.rb @@ -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 From f77c571af005c3aac336b73b36c1a2672e2d07be Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Fri, 30 Oct 2015 09:01:25 +1300 Subject: [PATCH 2/8] - default race_condition_ttl to a similar value as ttl --- lib/cached_resource/configuration.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cached_resource/configuration.rb b/lib/cached_resource/configuration.rb index d77ab37..fdff638 100644 --- a/lib/cached_resource/configuration.rb +++ b/lib/cached_resource/configuration.rb @@ -29,6 +29,7 @@ class Configuration < OpenStruct def initialize(options={}) super({ :enabled => true, + :race_condition_ttl => 604800, :ttl => 604800, :ttl_randomization => false, :ttl_randomization_scale => 1..2, From ede2f2b4c3a8eb65ed75d06d894303a34686be99 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Thu, 12 Nov 2015 14:14:19 +1300 Subject: [PATCH 3/8] - change race condition to a day, no query would be longer than a day --- lib/cached_resource/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cached_resource/configuration.rb b/lib/cached_resource/configuration.rb index fdff638..e9afb82 100644 --- a/lib/cached_resource/configuration.rb +++ b/lib/cached_resource/configuration.rb @@ -29,7 +29,7 @@ class Configuration < OpenStruct def initialize(options={}) super({ :enabled => true, - :race_condition_ttl => 604800, + :race_condition_ttl => 86400, :ttl => 604800, :ttl_randomization => false, :ttl_randomization_scale => 1..2, From 69c72acf2c922378f83f0e0335ceae9907fd1dcb Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Thu, 12 Nov 2015 14:31:19 +1300 Subject: [PATCH 4/8] - add race_condition_ttl into spec test --- spec/cached_resource/configuration_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/cached_resource/configuration_spec.rb b/spec/cached_resource/configuration_spec.rb index ce35c63..dc9bc2b 100644 --- a/spec/cached_resource/configuration_spec.rb +++ b/spec/cached_resource/configuration_spec.rb @@ -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, @@ -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 @@ -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, @@ -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, @@ -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(5) end end From 5961e2da840cbb46f558e107d0740462e3ed5f1c Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Thu, 12 Nov 2015 14:42:00 +1300 Subject: [PATCH 5/8] - finally got the meaning of this test action - it calls a new cachedresource configuration without inheriting it from Bar - race_condition_ttl would use the default settings --- spec/cached_resource/configuration_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/cached_resource/configuration_spec.rb b/spec/cached_resource/configuration_spec.rb index dc9bc2b..5f72033 100644 --- a/spec/cached_resource/configuration_spec.rb +++ b/spec/cached_resource/configuration_spec.rb @@ -186,7 +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(5) + expect(cr.race_condition_ttl).to eq(86400) end end From 1ba2c14d72627fbf0a9ff0fa95312e0cdc8d6708 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Thu, 19 Nov 2015 15:40:39 +1300 Subject: [PATCH 6/8] - update version number --- lib/cached_resource/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cached_resource/version.rb b/lib/cached_resource/version.rb index c068f7a..ed74c30 100644 --- a/lib/cached_resource/version.rb +++ b/lib/cached_resource/version.rb @@ -1,3 +1,3 @@ module CachedResource - VERSION = "4.1.0" + VERSION = "4.2.0" end From 3c0a999e56a296e9200876d98eb2c3d0f91d0b70 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Thu, 19 Nov 2015 15:42:57 +1300 Subject: [PATCH 7/8] - default configuration for race_condition_ttl added to comments --- lib/cached_resource/configuration.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cached_resource/configuration.rb b/lib/cached_resource/configuration.rb index e9afb82..d69d812 100644 --- a/lib/cached_resource/configuration.rb +++ b/lib/cached_resource/configuration.rb @@ -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, From ce8ca151cfd7fef5ce4168117fb33302d713cea4 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Thu, 19 Nov 2015 15:48:17 +1300 Subject: [PATCH 8/8] - add race condition ttl into readme doc --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3d038c3..f5ec9e2 100644 --- a/README.md +++ b/README.md @@ -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`