simple_redis_cache 0.0.1 → 0.0.2

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.
@@ -1,9 +1,54 @@
1
1
  SimpleRedisCache
2
2
  ================
3
3
 
4
- _SimpeRedisCache_ is a simple ruby module allowing for caching the result of a block in redis for a given time period.
4
+ Easily cache the result of a block within redis for a given time period.
5
5
 
6
- Let's cache something for 1 minut
6
+ Let's cache something for 1 minute
7
7
 
8
8
  ```ruby
9
- cache('hello', :ttl=>1){ 'world' }
9
+ class MyClass
10
+
11
+ include SimpleRedisCache
12
+
13
+ def hello
14
+ cache('hello', :ttl=>60){ 'world' }
15
+ end
16
+ end
17
+ ```
18
+
19
+ Call it straight from the module instead and this time cache it forever.
20
+
21
+ ```ruby
22
+ SimpleRedisCache.cache('hello') { 'world' }
23
+ ```
24
+
25
+ Maybe we should cache something worth while. If the result of the block is not a string, make sure to convert it to one.
26
+
27
+ ```ruby
28
+ require 'json'
29
+
30
+ JSON.parse(cache('20_perm_4', :ttl=>120){ (0..20).to_a.permutation(4).to_a.to_json })
31
+ ```
32
+
33
+ Notice we serialize to json inside the block and from json outside the block. Of course converting an enormous array
34
+ to and from JSON is slower than the permutation so maybe it's not the best example. Replace the above with an expensive
35
+ databse calculation and it makes more sense.
36
+
37
+
38
+ The above uses the default _Redis.new_ instance running on _127.0.0.1:6379_
39
+
40
+ Let's configure a different redis instance.
41
+
42
+ ```ruby
43
+ SimpleRedisCache::Config.redis = Redis.new(:host => "10.0.1.1", :port => 6380)
44
+ ```
45
+
46
+ My benchmarks show this is faster than your current cache.
47
+
48
+
49
+ But why would I use this?
50
+ ------------------------
51
+ Because you already depend on redis through _Resque_.
52
+
53
+
54
+
@@ -2,12 +2,11 @@ require 'redis'
2
2
 
3
3
  module SimpleRedisCache
4
4
 
5
- def redis=(redis)
6
- @redis = redis
7
- end
5
+ extend self
6
+
8
7
 
9
8
  def redis
10
- @redis ||= Redis.new
9
+ Config.redis
11
10
  end
12
11
 
13
12
  def cache(key, opts={}, &block)
@@ -18,4 +17,15 @@ module SimpleRedisCache
18
17
  value
19
18
  end
20
19
 
20
+ module Config
21
+
22
+ def self.redis=(redis)
23
+ @redis = redis
24
+ end
25
+
26
+ def self.redis
27
+ @redis ||= Redis.new
28
+ end
29
+ end
30
+
21
31
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleRedisCache
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -11,27 +11,48 @@ class SimpleRedisCacheTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
 
14
-
15
14
  def test_expire_block
16
- cache('hello', :ttl=>1){ 'world' }
15
+ cache('hello', :ttl=>1) { 'world' }
17
16
  sleep(2)
18
17
  assert_nil(redis['hello'])
19
18
  end
20
19
 
21
20
  def test_cache
22
- cache('hello'){ 'world' }
21
+ cache('hello') { 'world' }
23
22
  assert_equal(redis['hello'], 'world')
24
23
  end
25
24
 
26
25
  def test_cache_with_expire_block
27
- assert_equal('world', cache('hello', :ttl=>1000){ 'world' })
26
+ assert_equal('world', cache('hello', :ttl=>1000) { 'world' })
28
27
  end
29
28
 
30
29
  def test_block_is_only_executed_once
31
30
  @index = 0
32
- 2.times{ cache('hello'){ @index+=1 } }
31
+ 2.times { cache('hello') { @index+=1 } }
33
32
  assert_equal(@index, 1)
34
33
  end
35
34
 
35
+ class MyClass
36
+ include SimpleRedisCache
37
+ end
38
+
39
+
40
+ def test_config_singleton_redis
41
+ shared_redis = Redis.new
42
+ SimpleRedisCache::Config.redis = shared_redis
43
+ assert_same(shared_redis.object_id, MyClass.new.redis.object_id)
44
+ end
45
+
46
+ def test_cache_on_module_method
47
+ SimpleRedisCache.cache('hello') { 'world' }
48
+ assert_equal(redis['hello'], 'world')
49
+ end
50
+
51
+ require 'json'
52
+ def test_cache_permutations
53
+ assert_equal( (0..20).to_a.permutation(3).to_a, JSON.parse(cache('20_perm_4'){ (0..20).to_a.permutation(3).to_a.to_json }) )
54
+ end
55
+
56
+
36
57
 
37
58
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_redis_cache
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - kbaum
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-20 00:00:00 Z
13
+ date: 2011-05-25 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: redis
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  requirements: []
78
78
 
79
79
  rubyforge_project: simple_redis_cache
80
- rubygems_version: 1.7.2
80
+ rubygems_version: 1.8.3
81
81
  signing_key:
82
82
  specification_version: 3
83
83
  summary: simple api for caching strings in redis and passing a time to live