From 9ec156874c3519846b90752d1bb4058fd5bff58a Mon Sep 17 00:00:00 2001 From: Josh Lewis Date: Wed, 26 Dec 2012 16:05:01 -0600 Subject: [PATCH 1/4] Add support for lambda based namespaces. --- lib/dalli/client.rb | 10 +++++++--- test/test_active_support.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/dalli/client.rb b/lib/dalli/client.rb index febbc87b..546e66ee 100644 --- a/lib/dalli/client.rb +++ b/lib/dalli/client.rb @@ -340,7 +340,7 @@ def validate_key(key) raise ArgumentError, "key cannot be blank" if !key || key.length == 0 key = key_with_namespace(key) if key.length > 250 - namespace_length = @options[:namespace] ? @options[:namespace].size : 0 + namespace_length = namespace ? namespace.size : 0 max_length_before_namespace = 212 - namespace_length key = "#{key[0, max_length_before_namespace]}:md5:#{Digest::MD5.hexdigest(key)}" end @@ -348,11 +348,15 @@ def validate_key(key) end def key_with_namespace(key) - @options[:namespace] ? "#{@options[:namespace]}:#{key}" : key + namespace ? "#{namespace}:#{key}" : key end def key_without_namespace(key) - @options[:namespace] ? key.sub(%r(\A#{@options[:namespace]}:), '') : key + namespace ? key.sub(%r(\A#{namespace}:), '') : key + end + + def namespace + @options[:namespace].is_a?(Proc) ? @options[:namespace].call : @options[:namespace] end def normalize_options(opts) diff --git a/test/test_active_support.rb b/test/test_active_support.rb index 089aa491..4c0c7647 100644 --- a/test/test_active_support.rb +++ b/test/test_active_support.rb @@ -329,7 +329,7 @@ def cache_key end def connect - @dalli = ActiveSupport::Cache.lookup_store(:dalli_store, 'localhost:19122', :expires_in => 10.seconds, :namespace => 'x') + @dalli = ActiveSupport::Cache.lookup_store(:dalli_store, 'localhost:19122', :expires_in => 10.seconds, :namespace => lambda{33.to_s(36)}) @dalli.clear end From 1173289f467c7909f12e62b8383b9a6c926681d2 Mon Sep 17 00:00:00 2001 From: Josh Lewis Date: Thu, 27 Dec 2012 08:40:18 -0600 Subject: [PATCH 2/4] Add lambda namespace support to history. --- History.md | 1 + 1 file changed, 1 insertion(+) diff --git a/History.md b/History.md index 46e3ba4a..7fb48c4d 100644 --- a/History.md +++ b/History.md @@ -4,6 +4,7 @@ Dalli Changelog 2.6.0 ======= +- Add lambda support for cache namespaces [josh.w.lewis, #311] - read_multi optimization, now checks local_cache [chendo, #306] - Re-implement get_multi to be non-blocking [tmm1, #295] - Add `dalli` accessor to dalli_store to access the underlying From dfeb4e6b60febfff71b94bb09042ab6725843ab9 Mon Sep 17 00:00:00 2001 From: Josh Lewis Date: Thu, 27 Dec 2012 08:50:35 -0600 Subject: [PATCH 3/4] Fix misspelled user name in History.md. --- History.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/History.md b/History.md index 7fb48c4d..b75116dd 100644 --- a/History.md +++ b/History.md @@ -4,7 +4,7 @@ Dalli Changelog 2.6.0 ======= -- Add lambda support for cache namespaces [josh.w.lewis, #311] +- Add lambda support for cache namespaces [joshwlewis, #311] - read_multi optimization, now checks local_cache [chendo, #306] - Re-implement get_multi to be non-blocking [tmm1, #295] - Add `dalli` accessor to dalli_store to access the underlying From 16d29f482fb6f2712e67df7b3ad3e124b3e2d46f Mon Sep 17 00:00:00 2001 From: Josh Lewis Date: Tue, 1 Jan 2013 11:48:33 -0600 Subject: [PATCH 4/4] Remove redundant calls to namespace calculation and revise history. --- History.md | 6 +++++- lib/dalli/client.rb | 7 +++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/History.md b/History.md index b75116dd..ae06e9ab 100644 --- a/History.md +++ b/History.md @@ -1,10 +1,14 @@ Dalli Changelog ===================== -2.6.0 +HEAD ======= - Add lambda support for cache namespaces [joshwlewis, #311] + +2.6.0 +======= + - read_multi optimization, now checks local_cache [chendo, #306] - Re-implement get_multi to be non-blocking [tmm1, #295] - Add `dalli` accessor to dalli_store to access the underlying diff --git a/lib/dalli/client.rb b/lib/dalli/client.rb index 546e66ee..b92d4795 100644 --- a/lib/dalli/client.rb +++ b/lib/dalli/client.rb @@ -340,19 +340,18 @@ def validate_key(key) raise ArgumentError, "key cannot be blank" if !key || key.length == 0 key = key_with_namespace(key) if key.length > 250 - namespace_length = namespace ? namespace.size : 0 - max_length_before_namespace = 212 - namespace_length + max_length_before_namespace = 212 - (namespace || '').size key = "#{key[0, max_length_before_namespace]}:md5:#{Digest::MD5.hexdigest(key)}" end return key end def key_with_namespace(key) - namespace ? "#{namespace}:#{key}" : key + (ns = namespace) ? "#{ns}:#{key}" : key end def key_without_namespace(key) - namespace ? key.sub(%r(\A#{namespace}:), '') : key + (ns = namespace) ? key.sub(%r(\A#{ns}:), '') : key end def namespace