redis-kit 0.0.2 → 0.0.3

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/CHANGELOG.md CHANGED
@@ -1,16 +1,21 @@
1
- 0.0.2
2
- =====
1
+ 0.0.3 (March 8th, 2013)
2
+ -----------------------
3
3
 
4
- * Add support for Ruby 2.0.0.
5
- * Drop support for Rails < 3.2 and Ruby < 1.9.3.
6
- * Fix bug whereby redis-kit would clobber Resque hooks, replacing them with its
7
- own.
8
- * Allow MockRedis as a back-end (for tests).
4
+ * Add `RedisKit::Cache` with simple value caching helpers.
9
5
 
10
- 0.0.1
11
- =====
6
+ 0.0.2 (March 4th, 2013)
7
+ -----------------------
12
8
 
13
- * Initial release with support for:
9
+ * Add support for Ruby 2.0.0.
10
+ * Drop support for Rails < 3.2 and Ruby < 1.9.3.
11
+ * Fix bug whereby redis-kit would clobber Resque hooks, replacing them with
12
+ its own.
13
+ * Allow MockRedis as a back-end (for tests).
14
+
15
+ 0.0.1 (Unreleased)
16
+ ------------------
17
+
18
+ * Initial release with support for:
14
19
  * Rails 3.0, 3.1, 3.2
15
20
  * Ruby 1.9.1, 1.9.2, 1.9.3, 2.0
16
21
  * JRuby in 1.9 mode
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redis-kit (0.0.1)
4
+ redis-kit (0.0.2)
5
5
  hiredis (~> 0.4.0)
6
6
  mock_redis (~> 0.6.0)
7
7
  redis (~> 3.0.0)
@@ -16,11 +16,9 @@ GEM
16
16
  pry (>= 0.9.10)
17
17
  terminal-table (>= 1.4.3)
18
18
  thor (>= 0.14.6)
19
- guard-test (0.7.0)
20
- guard (>= 1.1)
21
- test-unit (~> 2.2)
19
+ guard-minitest (0.5.0)
20
+ guard (>= 0.4)
22
21
  hiredis (0.4.5)
23
- hiredis (0.4.5-java)
24
22
  listen (0.7.3)
25
23
  lumberjack (1.0.2)
26
24
  method_source (0.8.1)
@@ -40,7 +38,6 @@ GEM
40
38
  slop (3.4.3)
41
39
  spoon (0.0.1)
42
40
  terminal-table (1.4.5)
43
- test-unit (2.5.4)
44
41
  thor (0.17.0)
45
42
 
46
43
  PLATFORMS
@@ -48,8 +45,8 @@ PLATFORMS
48
45
  ruby
49
46
 
50
47
  DEPENDENCIES
51
- guard
52
- guard-test
53
- rake
54
- rb-fsevent
48
+ guard (~> 1.6.0)
49
+ guard-minitest (~> 0.5.0)
50
+ rake (~> 10.0.0)
51
+ rb-fsevent (~> 0.9.0)
55
52
  redis-kit!
data/Guardfile CHANGED
@@ -1,6 +1,8 @@
1
- guard :test do
2
- watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
3
- watch(%r{^test/.+_test\.rb$})
4
- watch('test/test_helper.rb') { "test" }
1
+ group 'test' do
2
+ guard 'minitest' do
3
+ watch(%r|^test/(.*)_test\.rb|)
4
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
5
+ watch(%r|^test/test_helper\.rb|) { "test" }
6
+ end
5
7
  end
6
8
 
data/README.md CHANGED
@@ -14,6 +14,11 @@ Ruby when used with any of the following:
14
14
  * Resque
15
15
  * *More soon...*
16
16
 
17
+ It also includes some (optional) classes that support common Redis uses:
18
+
19
+ * RedisKit::Cache (`require 'redis-kit/cache') - Cache the results of code
20
+ blocks.
21
+
17
22
  Using RedisKit
18
23
  --------------
19
24
 
@@ -81,8 +86,84 @@ If `$redis` is a different Redis connection than `Resque.redis`, RedisKit will
81
86
  handle reconnecting the connection after Resque forks. If they're the same
82
87
  connection, Resque will handle the connection as usual.
83
88
 
89
+ Extras
90
+ ======
91
+
92
+ RedisKit::Cache
93
+ ---------------
94
+
95
+ RedisKit::Cache is a simple caching helper that caches values in Redis with a
96
+ configurable timeout.
97
+
98
+ ```ruby
99
+ require 'redis-kit'
100
+ require 'redis-kit/cache'
101
+
102
+ # Result of block is cached for 10 seconds
103
+ RedisKit::Cache.cached("my-key", 10) do
104
+ # ...
105
+ end
106
+
107
+ # Manually expire the cached value:
108
+ RedisKit::Cache.expire("my-key")
109
+ ```
110
+
111
+ The RedisKit::Cache::Helper helper module makes working with cached values in a
112
+ class easy:
113
+
114
+ ```ruby
115
+ require 'redis-kit'
116
+ require 'redis-kit/cache'
117
+
118
+ class MyCachedClass
119
+ include RedisKit::Cache::Helper
120
+
121
+ # This method's result will be cached in Redis for 10 seconds.
122
+ def expensive_stuff
123
+ cached("#{self.id}", 10) do
124
+ # ...
125
+ end
126
+ end
127
+
128
+ # Manually expire cached values, if needed:
129
+ def reset_stuff
130
+ expire("${self.id}")
131
+ end
132
+ end
133
+ ```
134
+
135
+ (Both the `cached` method and the `expire` methods are available at the class and
136
+ instance levels.)
137
+
138
+ You can set a class-wide namespace and default timeout:
139
+
140
+ ```ruby
141
+ class MyCachedClass
142
+ include RedisKit::Cache::Helper
143
+ cache_timeout 600 # optional, default: 60 seconds
144
+ cache_namespace "my-cached-class" # optional, default: no namespace
145
+
146
+ # Cached for 10 minutes
147
+ def expensive_stuff
148
+ cached("#{self.id}") do
149
+ # ...
150
+ end
151
+ end
152
+ end
153
+ ```
154
+
155
+ Keep in mind that values are stored (and returned) by Redis as strings, so you
156
+ may need to serialize / deserialize your data manually:
157
+
158
+ ```ruby
159
+ value = cached("thing") do
160
+ JSON.dump(["cool", "values"])
161
+ end
162
+ value = JSON.load(value)
163
+ ```
164
+
84
165
  Support
85
- -------
166
+ =======
86
167
 
87
168
  RedisKit officially supports the following Rubies:
88
169
 
@@ -96,7 +177,7 @@ And the following libraries:
96
177
  * Resque >= 1.6
97
178
 
98
179
  Development
99
- -----------
180
+ ===========
100
181
 
101
182
  Set up and run tests with:
102
183
 
@@ -104,7 +185,7 @@ Set up and run tests with:
104
185
  make test
105
186
 
106
187
  TODO
107
- ----
188
+ ====
108
189
 
109
190
  * Add generators for common files
110
191
  * Resque initializer
data/Rakefile CHANGED
@@ -2,8 +2,9 @@ require "bundler/gem_tasks"
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
+ t.libs << "lib"
5
6
  t.libs << "test"
6
- t.test_files = FileList['test/*_test.rb']
7
+ t.test_files = FileList['test/*_test.rb'] + FileList['test/lib/**/*_test.rb']
7
8
  t.verbose = true
8
9
  end
9
10
  task :default => :test
@@ -0,0 +1,45 @@
1
+ module RedisKit
2
+ class Cache
3
+ module Helper
4
+ def self.included( base )
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def cache_namespace( value = nil )
10
+ @cache_namespace = value.to_s unless value == nil
11
+ @cache_namespace || ""
12
+ end
13
+
14
+ def cache_timeout( value = nil )
15
+ @cache_timeout = value if value
16
+ @cache_timeout || 60
17
+ end
18
+
19
+ def cached( key, timeout = nil, &block )
20
+ timeout ||= cache_timeout
21
+ RedisKit::Cache.get with_namespace( key ), timeout, &block
22
+ end
23
+
24
+ def expire( key )
25
+ RedisKit::Cache.expire with_namespace( key )
26
+ end
27
+
28
+ protected
29
+
30
+ def with_namespace( key )
31
+ cache_namespace != "" ? "#{cache_namespace}:#{key}" : key
32
+ end
33
+ end
34
+
35
+ def cached( key, timeout = nil, &block )
36
+ self.class.cached( key, timeout, &block )
37
+ end
38
+
39
+ def expire( key )
40
+ self.class.expire key
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,23 @@
1
+ module RedisKit
2
+ class Cache
3
+ class << self
4
+ def get( key, timeout, &block )
5
+ key = key.to_s
6
+ if value = RedisKit.redis.get( key )
7
+ value
8
+ else
9
+ value = yield
10
+ RedisKit.redis.setex key, timeout, value
11
+ value
12
+ end
13
+ end
14
+
15
+ def expire( key )
16
+ RedisKit.del key
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ require 'redis-kit/cache/helper'
23
+
@@ -1,3 +1,4 @@
1
1
  module RedisKit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
4
+
data/lib/redis-kit.rb CHANGED
@@ -10,6 +10,14 @@ module RedisKit
10
10
 
11
11
  CUSTOM_CONFIG_KEYS = [:mock]
12
12
 
13
+ class << self
14
+ attr_writer :redis
15
+
16
+ def redis
17
+ @redis || $redis
18
+ end
19
+ end
20
+
13
21
  def self.new_redis( path, env )
14
22
  config = load_config( path, env )
15
23
  if config[:mock]
data/redis-kit.gemspec CHANGED
@@ -21,8 +21,9 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency "redis", "~> 3.0.0"
22
22
  s.add_dependency "hiredis", "~> 0.4.0" if RUBY_ENGINE != "jruby"
23
23
  s.add_dependency "mock_redis", "~> 0.6.0"
24
- s.add_development_dependency "rake"
25
- s.add_development_dependency "rb-fsevent"
26
- s.add_development_dependency "guard"
27
- s.add_development_dependency "guard-test"
24
+ s.add_development_dependency "rake", "~> 10.0.0"
25
+ s.add_development_dependency "rb-fsevent", "~> 0.9.0"
26
+ s.add_development_dependency "guard", "~> 1.6.0"
27
+ s.add_development_dependency "guard-minitest", "~> 0.5.0"
28
28
  end
29
+
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+ require 'redis-kit/cache'
3
+
4
+ class CachedClass
5
+ include RedisKit::Cache::Helper
6
+ cache_timeout 600
7
+ cache_namespace "foo"
8
+
9
+ def self.run
10
+ one = cached( "bar", 10 ) do
11
+ "one"
12
+ end
13
+ two = cached( "biz" ) do
14
+ "two"
15
+ end
16
+ one + two
17
+ end
18
+
19
+ def run
20
+ one = cached( "baz", 10 ) do
21
+ "one"
22
+ end
23
+ two = cached( "bix" ) do
24
+ "two"
25
+ end
26
+ one + two
27
+ end
28
+ end
29
+
30
+ describe RedisKit::Cache::Helper do
31
+ before do
32
+ RedisKit.redis = RedisKit.new_redis( good_config_path, "test_mock" )
33
+ end
34
+
35
+ it "provides helpers" do
36
+ CachedClass.run.must_equal "onetwo"
37
+ RedisKit.redis.get( "foo:bar" ).must_equal "one"
38
+ RedisKit.redis.ttl( "foo:bar" ).must_equal 10
39
+ RedisKit.redis.get( "foo:biz" ).must_equal "two"
40
+ RedisKit.redis.ttl( "foo:biz" ).must_equal 600
41
+
42
+ CachedClass.new.run.must_equal "onetwo"
43
+ RedisKit.redis.get( "foo:baz" ).must_equal "one"
44
+ RedisKit.redis.ttl( "foo:baz" ).must_equal 10
45
+ RedisKit.redis.get( "foo:bix" ).must_equal "two"
46
+ RedisKit.redis.ttl( "foo:bix" ).must_equal 600
47
+ end
48
+ end
49
+
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+ require 'redis-kit/cache'
3
+
4
+ describe RedisKit::Cache do
5
+ before do
6
+ RedisKit.redis = RedisKit.new_redis( good_config_path, "test_mock" )
7
+ end
8
+
9
+ after do
10
+ RedisKit.redis.del "test:*"
11
+ end
12
+
13
+ it "caches values" do
14
+ calculated = false
15
+ value = RedisKit::Cache.get( "test:foo", 60 ) do
16
+ calculated = true
17
+ "yep"
18
+ end
19
+ value.must_equal "yep"
20
+ calculated.must_equal true
21
+ RedisKit.redis.ttl( "test:foo" ).must_equal 60
22
+
23
+ # cache primed
24
+ calculated = false
25
+ value = RedisKit::Cache.get( "test:foo", 60 ) do
26
+ calculated = true
27
+ "yep"
28
+ end
29
+ value.must_equal "yep"
30
+ calculated.must_equal false
31
+ end
32
+ end
33
+
@@ -1,4 +1,4 @@
1
- require_relative "test_helper"
1
+ require "test_helper"
2
2
 
3
3
  describe "RedisKit.load_config" do
4
4
  after do
@@ -70,3 +70,12 @@ describe "RedisKit.new_redis" do
70
70
  end
71
71
  end
72
72
 
73
+ describe "RedisKit.redis" do
74
+ it "stores a global Redis connection" do
75
+ redis = RedisKit.new_redis( good_config_path, "test" )
76
+ RedisKit.redis.must_be_nil
77
+ RedisKit.redis = redis
78
+ RedisKit.redis.must_equal redis
79
+ end
80
+ end
81
+
data/test/test_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
- require_relative "../lib/redis-kit"
2
1
  require 'minitest/spec'
3
2
  require 'minitest/autorun'
3
+ require 'redis-kit'
4
4
 
5
5
  def jruby?
6
6
  RUBY_ENGINE == "jruby"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-04 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -64,65 +64,65 @@ dependencies:
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
- - - ! '>='
67
+ - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: 10.0.0
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
- - - ! '>='
75
+ - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: '0'
77
+ version: 10.0.0
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: rb-fsevent
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - ~>
84
84
  - !ruby/object:Gem::Version
85
- version: '0'
85
+ version: 0.9.0
86
86
  type: :development
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - ~>
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: 0.9.0
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: guard
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
- - - ! '>='
99
+ - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: 1.6.0
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ! '>='
107
+ - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 1.6.0
110
110
  - !ruby/object:Gem::Dependency
111
- name: guard-test
111
+ name: guard-minitest
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
115
- - - ! '>='
115
+ - - ~>
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 0.5.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  none: false
122
122
  requirements:
123
- - - ! '>='
123
+ - - ~>
124
124
  - !ruby/object:Gem::Version
125
- version: '0'
125
+ version: 0.5.0
126
126
  description: simple redis init for rails projects
127
127
  email:
128
128
  - tyson@stovepipestudios.com
@@ -140,6 +140,8 @@ files:
140
140
  - README.md
141
141
  - Rakefile
142
142
  - lib/redis-kit.rb
143
+ - lib/redis-kit/cache.rb
144
+ - lib/redis-kit/cache/helper.rb
143
145
  - lib/redis-kit/railtie.rb
144
146
  - lib/redis-kit/resque.rb
145
147
  - lib/redis-kit/version.rb
@@ -186,6 +188,8 @@ files:
186
188
  - test/railsapi/test/test_helper.rb
187
189
  - test/railsapi/test/unit/.gitkeep
188
190
  - test/railsapi/test/unit/redis-kit_test.rb
191
+ - test/redis-kit/cache/helper_test.rb
192
+ - test/redis-kit/cache_test.rb
189
193
  - test/redis-kit_test.rb
190
194
  - test/support/redis.blank.yml
191
195
  - test/support/redis.good.yml
@@ -258,6 +262,8 @@ test_files:
258
262
  - test/railsapi/test/test_helper.rb
259
263
  - test/railsapi/test/unit/.gitkeep
260
264
  - test/railsapi/test/unit/redis-kit_test.rb
265
+ - test/redis-kit/cache/helper_test.rb
266
+ - test/redis-kit/cache_test.rb
261
267
  - test/redis-kit_test.rb
262
268
  - test/support/redis.blank.yml
263
269
  - test/support/redis.good.yml