cache 0.2.7 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +8 -0
- data/README.md +47 -28
- data/benchmarks/afterrefactor.txt +86 -0
- data/benchmarks/midrefactor.txt +89 -0
- data/cache.gemspec +0 -8
- data/lib/cache.rb +86 -65
- data/lib/cache/active_support_cache_dalli_store.rb +14 -0
- data/lib/cache/active_support_cache_memory_store.rb +10 -0
- data/lib/cache/active_support_cache_store.rb +37 -0
- data/lib/cache/config.rb +0 -32
- data/lib/cache/dalli_client.rb +46 -0
- data/lib/cache/mem_cache.rb +41 -0
- data/lib/cache/memcached.rb +47 -0
- data/lib/cache/memcached_rails.rb +33 -0
- data/lib/cache/redis.rb +38 -0
- data/lib/cache/redis_namespace.rb +6 -0
- data/lib/cache/version.rb +1 -1
- data/test/helper.rb +4 -0
- data/test/shared_tests.rb +1 -18
- data/test/test_dalli_storage.rb +1 -1
- data/test/test_dalli_store_storage.rb +1 -1
- data/test/test_default_storage.rb +5 -5
- data/test/test_memcache_storage.rb +1 -1
- data/test/test_memcached_rails_storage.rb +1 -1
- data/test/test_memcached_storage.rb +4 -1
- data/test/test_rails_cache_storage.rb +17 -15
- data/test/test_redis_storage.rb +1 -1
- metadata +13 -131
- data/lib/cache/storage.rb +0 -260
@@ -0,0 +1,37 @@
|
|
1
|
+
module Cache::ActiveSupportCacheStore
|
2
|
+
# native
|
3
|
+
def fetch(k, ttl = nil, &blk)
|
4
|
+
handle_fork
|
5
|
+
@metal.fetch k, { :expires_in => extract_ttl(ttl) }, &blk
|
6
|
+
end
|
7
|
+
# --
|
8
|
+
|
9
|
+
def _get(k)
|
10
|
+
@metal.read k
|
11
|
+
end
|
12
|
+
|
13
|
+
def _get_multi(ks)
|
14
|
+
@metal.read_multi *ks
|
15
|
+
end
|
16
|
+
|
17
|
+
def _set(k, v, ttl)
|
18
|
+
if ttl == 0
|
19
|
+
@metal.write k, v # never expire
|
20
|
+
else
|
21
|
+
@metal.write k, v, :expires_in => ttl
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def _delete(k)
|
26
|
+
@metal.delete k
|
27
|
+
end
|
28
|
+
|
29
|
+
def _flush
|
30
|
+
@metal.clear
|
31
|
+
end
|
32
|
+
|
33
|
+
def _exist?(k)
|
34
|
+
@metal.exist? k
|
35
|
+
# !get(k).nil?
|
36
|
+
end
|
37
|
+
end
|
data/lib/cache/config.rb
CHANGED
@@ -11,30 +11,6 @@ class Cache
|
|
11
11
|
@parent = parent
|
12
12
|
end
|
13
13
|
|
14
|
-
# The cache client to use.
|
15
|
-
#
|
16
|
-
# Note that you normally just set this when you initialize a Cache object.
|
17
|
-
#
|
18
|
-
# Example:
|
19
|
-
# cache.config.client = Memcached.new '127.0.0.1:11211'
|
20
|
-
def client=(client) #:nodoc:
|
21
|
-
@client = client.is_a?(::Cache) ? client.config.client : client
|
22
|
-
end
|
23
|
-
|
24
|
-
def client #:nodoc:
|
25
|
-
if @client.nil?
|
26
|
-
self.client = if defined?(::Rails) and ::Rails.respond_to?(:cache) and rails_cache = ::Rails.cache and not rails_cache.is_a?(::Cache)
|
27
|
-
rails_cache
|
28
|
-
else
|
29
|
-
require 'active_support/cache'
|
30
|
-
require 'active_support/cache/memory_store'
|
31
|
-
::ActiveSupport::Cache::MemoryStore.new
|
32
|
-
end
|
33
|
-
else
|
34
|
-
@client
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
14
|
# TTL for method caches. Defaults to 60 seconds.
|
39
15
|
#
|
40
16
|
# Example:
|
@@ -46,13 +22,5 @@ class Cache
|
|
46
22
|
def default_ttl #:nodoc:
|
47
23
|
@default_ttl || 60
|
48
24
|
end
|
49
|
-
|
50
|
-
def logger #:nodoc:
|
51
|
-
@logger
|
52
|
-
end
|
53
|
-
|
54
|
-
def logger=(logger) #:nodoc:
|
55
|
-
@logger = logger
|
56
|
-
end
|
57
25
|
end
|
58
26
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Cache::DalliClient
|
2
|
+
def after_fork
|
3
|
+
@metal.close
|
4
|
+
end
|
5
|
+
|
6
|
+
def _get(k)
|
7
|
+
@metal.get k
|
8
|
+
end
|
9
|
+
|
10
|
+
def _get_multi(ks)
|
11
|
+
@metal.get_multi ks
|
12
|
+
end
|
13
|
+
|
14
|
+
def _set(k, v, ttl)
|
15
|
+
@metal.set k, v, ttl
|
16
|
+
end
|
17
|
+
|
18
|
+
def _delete(k)
|
19
|
+
@metal.delete k
|
20
|
+
end
|
21
|
+
|
22
|
+
def _flush
|
23
|
+
@metal.flush
|
24
|
+
end
|
25
|
+
|
26
|
+
# sux
|
27
|
+
def _exist?(k)
|
28
|
+
!@metal.get(k).nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
def _stats
|
32
|
+
@metal.stats
|
33
|
+
end
|
34
|
+
|
35
|
+
# native
|
36
|
+
def fetch(k, ttl = nil, &blk)
|
37
|
+
handle_fork
|
38
|
+
@metal.fetch k, extract_ttl(ttl), &blk
|
39
|
+
end
|
40
|
+
|
41
|
+
def cas(k, ttl = nil, &blk)
|
42
|
+
handle_fork
|
43
|
+
@metal.cas k, extract_ttl(ttl), &blk
|
44
|
+
end
|
45
|
+
# --
|
46
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Cache::MemCache
|
2
|
+
def after_fork
|
3
|
+
@metal.reset
|
4
|
+
end
|
5
|
+
|
6
|
+
def _get(k)
|
7
|
+
@metal.get k
|
8
|
+
end
|
9
|
+
|
10
|
+
def _get_multi(ks)
|
11
|
+
@metal.get_multi ks
|
12
|
+
end
|
13
|
+
|
14
|
+
def _set(k, v, ttl)
|
15
|
+
@metal.set k, v, ttl
|
16
|
+
end
|
17
|
+
|
18
|
+
def _delete(k)
|
19
|
+
@metal.delete k
|
20
|
+
end
|
21
|
+
|
22
|
+
def _flush
|
23
|
+
@metal.flush_all
|
24
|
+
end
|
25
|
+
|
26
|
+
# sux
|
27
|
+
def _exist?(k)
|
28
|
+
!@metal.get(k).nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
def _stats
|
32
|
+
@metal.stats
|
33
|
+
end
|
34
|
+
|
35
|
+
# native
|
36
|
+
def fetch(k, ttl = nil, &blk)
|
37
|
+
handle_fork
|
38
|
+
@metal.fetch k, extract_ttl(ttl), &blk
|
39
|
+
end
|
40
|
+
# --
|
41
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Cache::Memcached
|
2
|
+
def thread_metal
|
3
|
+
::Thread.current["#{@pid}/#{self.class.name}/#{object_id}/thread_metal"] ||= @metal.clone
|
4
|
+
end
|
5
|
+
|
6
|
+
def _get(k)
|
7
|
+
thread_metal.get k
|
8
|
+
rescue ::Memcached::NotFound
|
9
|
+
# oh well
|
10
|
+
end
|
11
|
+
|
12
|
+
def _get_multi(ks)
|
13
|
+
thread_metal.get ks
|
14
|
+
end
|
15
|
+
|
16
|
+
def _set(k, v, ttl)
|
17
|
+
thread_metal.set k, v, ttl
|
18
|
+
end
|
19
|
+
|
20
|
+
def _delete(k)
|
21
|
+
thread_metal.delete k
|
22
|
+
rescue ::Memcached::NotFound
|
23
|
+
end
|
24
|
+
|
25
|
+
def _flush
|
26
|
+
thread_metal.flush
|
27
|
+
end
|
28
|
+
|
29
|
+
def _exist?(k)
|
30
|
+
thread_metal.get k
|
31
|
+
true
|
32
|
+
rescue ::Memcached::NotFound
|
33
|
+
false
|
34
|
+
end
|
35
|
+
|
36
|
+
def _stats
|
37
|
+
thread_metal.stats
|
38
|
+
end
|
39
|
+
|
40
|
+
# native
|
41
|
+
def cas(k, ttl = nil, &blk)
|
42
|
+
handle_fork
|
43
|
+
thread_metal.cas k, extract_ttl(ttl), &blk
|
44
|
+
rescue ::Memcached::NotFound
|
45
|
+
end
|
46
|
+
# --
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'cache/memcached'
|
2
|
+
module Cache::MemcachedRails
|
3
|
+
def self.extended(base)
|
4
|
+
base.extend Cache::Memcached
|
5
|
+
base.extend Override
|
6
|
+
end
|
7
|
+
|
8
|
+
module Override
|
9
|
+
def _exist?(k)
|
10
|
+
thread_metal.exist? k
|
11
|
+
# !get(k).nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def _get(k)
|
15
|
+
thread_metal.get k
|
16
|
+
end
|
17
|
+
|
18
|
+
def _get_multi(ks)
|
19
|
+
thread_metal.get_multi ks
|
20
|
+
end
|
21
|
+
|
22
|
+
def _delete(k)
|
23
|
+
thread_metal.delete k
|
24
|
+
end
|
25
|
+
|
26
|
+
# native
|
27
|
+
def cas(k, ttl = nil, &blk)
|
28
|
+
handle_fork
|
29
|
+
thread_metal.cas k, extract_ttl(ttl), &blk
|
30
|
+
end
|
31
|
+
# --
|
32
|
+
end
|
33
|
+
end
|
data/lib/cache/redis.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Cache::Redis
|
2
|
+
def _get(k)
|
3
|
+
if cached_v = @metal.get(k) and cached_v.is_a?(::String)
|
4
|
+
::Marshal.load cached_v
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def _get_multi(ks)
|
9
|
+
ks.inject({}) do |memo, k|
|
10
|
+
memo[k] = @metal.get(k) if @metal.exist?(k)
|
11
|
+
memo
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def _set(k, v, ttl)
|
16
|
+
if ttl == 0
|
17
|
+
@metal.set k, ::Marshal.dump(v)
|
18
|
+
else
|
19
|
+
@metal.setex k, ttl, ::Marshal.dump(v)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def _delete(k)
|
24
|
+
@metal.del k
|
25
|
+
end
|
26
|
+
|
27
|
+
def _flush
|
28
|
+
@metal.flush
|
29
|
+
end
|
30
|
+
|
31
|
+
def _exist?(k)
|
32
|
+
@metal.exists k
|
33
|
+
end
|
34
|
+
|
35
|
+
def _stats
|
36
|
+
@metal.stats
|
37
|
+
end
|
38
|
+
end
|
data/lib/cache/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -6,6 +6,10 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
6
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
7
|
require 'cache'
|
8
8
|
|
9
|
+
if ::Bundler.definition.specs['ruby-debug19'].first or ::Bundler.definition.specs['ruby-debug'].first
|
10
|
+
require 'ruby-debug'
|
11
|
+
end
|
12
|
+
|
9
13
|
require 'shared_tests'
|
10
14
|
|
11
15
|
class Test::Unit::TestCase
|
data/test/shared_tests.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'logger'
|
2
|
-
|
3
1
|
module SharedTests
|
4
2
|
def test_get
|
5
3
|
assert_equal nil, @cache.get('hello')
|
@@ -58,22 +56,7 @@ module SharedTests
|
|
58
56
|
@cache.stats
|
59
57
|
end
|
60
58
|
end
|
61
|
-
|
62
|
-
def test_logger
|
63
|
-
assert_equal nil, @cache.logger
|
64
|
-
l = Logger.new $stderr
|
65
|
-
@cache.logger = l
|
66
|
-
assert_equal l, @cache.logger
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_reset
|
70
|
-
@cache.set 'hello', 'world'
|
71
|
-
assert @cache.exist?('hello')
|
72
|
-
@cache.reset
|
73
|
-
# still there!
|
74
|
-
assert @cache.exist?('hello')
|
75
|
-
end
|
76
|
-
|
59
|
+
|
77
60
|
def test_fetch
|
78
61
|
assert_equal nil, @cache.fetch('hello')
|
79
62
|
assert_equal 'world', @cache.fetch('hello') { 'world' }
|
data/test/test_dalli_storage.rb
CHANGED
@@ -10,7 +10,7 @@ class TestDalliStorage < Test::Unit::TestCase
|
|
10
10
|
include SharedTests
|
11
11
|
|
12
12
|
def get_ring_object_id
|
13
|
-
@cache.
|
13
|
+
@cache.metal.instance_variable_get(:@ring).object_id
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_treats_as_thread_safe
|
@@ -12,7 +12,7 @@ class TestDalliStoreStorage < Test::Unit::TestCase
|
|
12
12
|
include SharedTests
|
13
13
|
|
14
14
|
def get_ring_object_id
|
15
|
-
hidden_dalli_client = @cache.
|
15
|
+
hidden_dalli_client = @cache.metal.instance_variable_get :@data
|
16
16
|
hidden_dalli_client.instance_variable_get(:@ring).object_id
|
17
17
|
end
|
18
18
|
|
@@ -6,27 +6,27 @@ class TestDefaultStorage < Test::Unit::TestCase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def test_query
|
9
|
-
|
9
|
+
assert_equal ActiveSupport::Cache::MemoryStore, @cache.metal.class
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_default
|
13
13
|
c = Cache.new
|
14
|
-
assert_equal ActiveSupport::Cache::MemoryStore, c.
|
14
|
+
assert_equal ActiveSupport::Cache::MemoryStore, c.metal.class
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_self_reference
|
18
18
|
c = Cache.new(Cache.new)
|
19
|
-
assert_equal ActiveSupport::Cache::MemoryStore, c.
|
19
|
+
assert_equal ActiveSupport::Cache::MemoryStore, c.metal.class
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_wrap_with_default
|
23
23
|
c = Cache.wrap(Cache.new)
|
24
|
-
assert_equal ActiveSupport::Cache::MemoryStore, c.
|
24
|
+
assert_equal ActiveSupport::Cache::MemoryStore, c.metal.class
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_absurd_wrap
|
28
28
|
c = Cache.new(Cache.wrap(Cache.new))
|
29
|
-
assert_equal ActiveSupport::Cache::MemoryStore, c.
|
29
|
+
assert_equal ActiveSupport::Cache::MemoryStore, c.metal.class
|
30
30
|
end
|
31
31
|
|
32
32
|
include SharedTests
|
@@ -11,7 +11,7 @@ class TestMemcacheStorage < Test::Unit::TestCase
|
|
11
11
|
include SharedTests
|
12
12
|
|
13
13
|
def get_server_status_ids
|
14
|
-
@cache.
|
14
|
+
@cache.metal.instance_variable_get(:@servers).map { |s| s.status.object_id }
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_treats_as_thread_safe
|