Permalink
Browse files

addressed issue with not returning value

  • Loading branch information...
1 parent 095f7db commit aef100cfcb817b64f5dbc55c5de4d2d8faa3f1a6 Karl committed May 25, 2011
Showing with 38 additions and 6 deletions.
  1. +25 −0 README.markdown
  2. +5 −2 lib/simple_redis_cache.rb
  3. +4 −4 simple_redis_cache.gemspec
  4. +4 −0 test/simple_redis_cache_test.rb
View
@@ -0,0 +1,25 @@
+SimpleRedisCache
+================
+
+_SimpeRedisCache_ is a simple ruby module allowing for caching the result of a block in redis for a given time period.
+
+Let's cache something for 1 minute
+
+```ruby
+ class MyClass
+
+ include SimpleRedisCache
+
+ REDIS = Redis.new
+
+ def redis
+ REDIS
+ end
+
+ def hello
+ cache('hello', :ttl=>60){ 'world' }
+ end
+ end
+```
+
+Amazing!
@@ -2,6 +2,8 @@
module SimpleRedisCache
+ extend self
+
def redis=(redis)
@redis = redis
end
@@ -11,10 +13,11 @@ def redis
end
def cache(key, opts={}, &block)
- unless redis.exists(key)
- redis[key]=block.call
+ unless (value = redis[key])
+ value = redis[key] = block.call
redis.expire(key, opts[:ttl]) if opts[:ttl]
end
+ value
end
end
@@ -6,11 +6,11 @@ Gem::Specification.new do |s|
s.name = "simple_redis_cache"
s.version = SimpleRedisCache::VERSION
s.platform = Gem::Platform::RUBY
- s.authors = ["TODO: Write your name"]
- s.email = ["TODO: Write your email address"]
+ s.authors = ["kbaum"]
+ s.email = ["karl.baum@gmail.com"]
s.homepage = ""
- s.summary = %q{TODO: Write a gem summary}
- s.description = %q{TODO: Write a gem description}
+ s.summary = %q{simple api for caching strings in redis and passing a time to live}
+ s.description = %q{simple api for caching strings in redis and passing a time to live}
s.rubyforge_project = "simple_redis_cache"
@@ -23,6 +23,10 @@ def test_cache
assert_equal(redis['hello'], 'world')
end
+ def test_cache_with_expire_block
+ assert_equal('world', cache('hello', :ttl=>1000){ 'world' })
+ end
+
def test_block_is_only_executed_once
@index = 0
2.times{ cache('hello'){ @index+=1 } }

0 comments on commit aef100c

Please sign in to comment.