simple_redis_cache 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.markdown +9 -0
- data/Rakefile +11 -0
- data/lib/simple_redis_cache/version.rb +3 -0
- data/lib/simple_redis_cache.rb +21 -0
- data/simple_redis_cache.gemspec +23 -0
- data/test/simple_redis_cache_test.rb +37 -0
- data/test/test_helper.rb +11 -0
- metadata +86 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ruby-1.9.2@simple_redis_cache
|
data/Gemfile
ADDED
data/README.markdown
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'redis'
|
2
|
+
|
3
|
+
module SimpleRedisCache
|
4
|
+
|
5
|
+
def redis=(redis)
|
6
|
+
@redis = redis
|
7
|
+
end
|
8
|
+
|
9
|
+
def redis
|
10
|
+
@redis ||= Redis.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def cache(key, opts={}, &block)
|
14
|
+
unless (value = redis[key])
|
15
|
+
value = redis[key] = block.call
|
16
|
+
redis.expire(key, opts[:ttl]) if opts[:ttl]
|
17
|
+
end
|
18
|
+
value
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "simple_redis_cache/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "simple_redis_cache"
|
7
|
+
s.version = SimpleRedisCache::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["kbaum"]
|
10
|
+
s.email = ["karl.baum@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{simple api for caching strings in redis and passing a time to live}
|
13
|
+
s.description = %q{simple api for caching strings in redis and passing a time to live}
|
14
|
+
|
15
|
+
s.rubyforge_project = "simple_redis_cache"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_dependency('redis')
|
22
|
+
s.add_development_dependency('ruby-debug19')
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
|
+
|
3
|
+
class SimpleRedisCacheTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include SimpleRedisCache
|
6
|
+
|
7
|
+
# Called before every test method runs. Can be used
|
8
|
+
# to set up fixture information.
|
9
|
+
def setup
|
10
|
+
redis.flushdb
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def test_expire_block
|
16
|
+
cache('hello', :ttl=>1){ 'world' }
|
17
|
+
sleep(2)
|
18
|
+
assert_nil(redis['hello'])
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_cache
|
22
|
+
cache('hello'){ 'world' }
|
23
|
+
assert_equal(redis['hello'], 'world')
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_cache_with_expire_block
|
27
|
+
assert_equal('world', cache('hello', :ttl=>1000){ 'world' })
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_block_is_only_executed_once
|
31
|
+
@index = 0
|
32
|
+
2.times{ cache('hello'){ @index+=1 } }
|
33
|
+
assert_equal(@index, 1)
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_redis_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- kbaum
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-20 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: redis
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: ruby-debug19
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: simple api for caching strings in redis and passing a time to live
|
38
|
+
email:
|
39
|
+
- karl.baum@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- .rvmrc
|
49
|
+
- Gemfile
|
50
|
+
- README.markdown
|
51
|
+
- Rakefile
|
52
|
+
- lib/simple_redis_cache.rb
|
53
|
+
- lib/simple_redis_cache/version.rb
|
54
|
+
- simple_redis_cache.gemspec
|
55
|
+
- test/simple_redis_cache_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
homepage: ""
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project: simple_redis_cache
|
80
|
+
rubygems_version: 1.7.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: simple api for caching strings in redis and passing a time to live
|
84
|
+
test_files:
|
85
|
+
- test/simple_redis_cache_test.rb
|
86
|
+
- test/test_helper.rb
|