Permalink
Browse files

Add support for lambda based namespaces.

  • Loading branch information...
1 parent 9a36942 commit 9ec156874c3519846b90752d1bb4058fd5bff58a @joshwlewis joshwlewis committed Dec 26, 2012
Showing with 8 additions and 4 deletions.
  1. +7 −3 lib/dalli/client.rb
  2. +1 −1 test/test_active_support.rb
View
@@ -340,19 +340,23 @@ 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
return 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)
@@ -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

0 comments on commit 9ec1568

Please sign in to comment.