Skip to content

Commit

Permalink
Merge pull request #311 from joshwlewis/master
Browse files Browse the repository at this point in the history
Add lambda support for namespacing
  • Loading branch information
mperham committed Jan 2, 2013
2 parents 2f0a0d6 + 16d29f4 commit d3c2ed2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions History.md
@@ -1,6 +1,11 @@
Dalli Changelog
=====================

HEAD
=======

- Add lambda support for cache namespaces [joshwlewis, #311]

2.6.0
=======

Expand Down
11 changes: 7 additions & 4 deletions lib/dalli/client.rb
Expand Up @@ -340,19 +340,22 @@ 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
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)
@options[:namespace] ? "#{@options[:namespace]}:#{key}" : key
(ns = namespace) ? "#{ns}:#{key}" : key
end

def key_without_namespace(key)
@options[:namespace] ? key.sub(%r(\A#{@options[:namespace]}:), '') : key
(ns = namespace) ? key.sub(%r(\A#{ns}:), '') : key
end

def namespace
@options[:namespace].is_a?(Proc) ? @options[:namespace].call : @options[:namespace]
end

def normalize_options(opts)
Expand Down
2 changes: 1 addition & 1 deletion test/test_active_support.rb
Expand Up @@ -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

Expand Down

0 comments on commit d3c2ed2

Please sign in to comment.