rails_redis_cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1 @@
1
+ redis cache for rails3
@@ -0,0 +1,72 @@
1
+ require 'active_support'
2
+ require 'base64'
3
+ require 'redis'
4
+ require 'time'
5
+
6
+ module ActiveSupport
7
+ module Cache
8
+ class RailsRedisCache < Store
9
+
10
+ TIME_PREF = "rails_redis_cache_time"
11
+ VALUE_PREF = "rails_redis_cache_value"
12
+
13
+ attr_reader :redis
14
+
15
+ def initialize(options={})
16
+ super()
17
+ @redis = Redis.connect(options)
18
+ end
19
+
20
+ # ============================= basic store impl ==============================
21
+
22
+ def read_entry(key, options)
23
+ raw_value = @redis.get "#{VALUE_PREF}_#{key}"
24
+ return nil unless raw_value
25
+
26
+ time = Time.parse @redis.get("#{TIME_PREF}_#{key}")
27
+ value = Marshal.load(Base64.decode64(raw_value))
28
+ ActiveSupport::Cache::Entry.create value, time
29
+ end
30
+
31
+ def write_entry(key, entry, options)
32
+ value = Base64.encode64(Marshal.dump(entry.value))
33
+ time = Time.now
34
+ @redis.mset "#{VALUE_PREF}_#{key}", value, "#{TIME_PREF}_#{key}", time
35
+ return unless expiry = options[:expires_in]
36
+ @redis.expire "#{VALUE_PREF}_#{key}", expiry
37
+ @redis.expire "#{TIME_PREF}_#{key}", expiry
38
+ end
39
+
40
+ def delete_entry(key, options)
41
+ @redis.del "#{VALUE_PREF}_#{key}", "#{TIME_PREF}_#{key}"
42
+ end
43
+
44
+ # ============================= optional store impl ==============================
45
+
46
+ def delete_matched(matcher, options = nil)
47
+ @redis.keys("#{VALUE_PREF}_*").map{|key| key[(VALUE_PREF.size + 1)..-1] }.grep(matcher).each do |key|
48
+ delete_entry(key, options)
49
+ end.size
50
+ end
51
+
52
+ def increment(name, amount = 1, options = nil)
53
+ write(name, amount + read(name, options).to_i, options)
54
+ end
55
+
56
+ def decrement(name, amount = 1, options = nil)
57
+ write(name, -1 * amount + read(name, options).to_i, options)
58
+ end
59
+
60
+ def cleanup(options = nil)
61
+ p value_keys = @redis.keys("#{VALUE_PREF}_*")
62
+ p time_keys = @redis.keys("#{TIME_PREF}_*")
63
+ @redis.del *(value_keys + time_keys)
64
+ end
65
+
66
+ def clear(options = nil)
67
+ cleanup(options)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','..','lib')
2
+ require 'test/unit'
@@ -0,0 +1,103 @@
1
+ require 'test_helper'
2
+ require 'rails_redis_cache'
3
+ require 'logger'
4
+
5
+ class TestRailsRedisCache < Test::Unit::TestCase
6
+
7
+ def setup
8
+ ActiveSupport::Cache::RailsRedisCache.logger = Logger.new(STDOUT)
9
+ @cache = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
10
+ @cache.redis.flushdb
11
+ @value = "entry"
12
+ @key = "key"
13
+ @options = {:option => 'any'}
14
+ end
15
+
16
+ def test_delete_no_error
17
+ @cache.delete(@key, @options)
18
+ end
19
+
20
+ def test_write_no_error
21
+ @cache.write(@key, @value, @options)
22
+ end
23
+
24
+ def test_read_no_error
25
+ @cache.read(@key, @options)
26
+ end
27
+
28
+ def test_read_write
29
+ @cache.delete(@key, @options)
30
+ assert_nil(@cache.read(@key, @options))
31
+ @cache.write(@key, @value, @options)
32
+ assert_equal(@value, @cache.read(@key, @options))
33
+ end
34
+
35
+ def test_expires_in
36
+ @cache.write(@key, @value, :expires_in=>2.seconds)
37
+ assert_equal(@value, @cache.read(@key, @options))
38
+ sleep(3)
39
+ assert_nil(@cache.read(@key, @options))
40
+ end
41
+
42
+ def test_fetch
43
+ count = 0
44
+ @cache.fetch(@key, :expires_in=>3.seconds) {count += 1}
45
+ @cache.fetch(@key) {count += 1}
46
+ @cache.fetch(@key) {count += 1}
47
+ assert_equal(count, 1)
48
+ @cache.delete(@key)
49
+ @cache.fetch(@key, :expires_in=>0.seconds) {count += 1}
50
+ assert_equal(count, 2)
51
+ end
52
+
53
+ def test_fetch_with_proc
54
+ @cache.fetch(@key, @options, &lambda{@value})
55
+ end
56
+
57
+ def test_fetch_with_array
58
+ @cache.fetch(@key, @options){[@value]}
59
+ assert_equal([@value], @cache.fetch(@key, @options))
60
+ end
61
+
62
+ def test_delete_matched
63
+ @cache.write('aaaa', @value, @options)
64
+ @cache.write('aaa', @value, @options)
65
+ @cache.write('aa', @value, @options)
66
+ assert_equal(3, @cache.delete_matched(/aa+/, @options))
67
+ assert_nil(@cache.read('aaaa', @options))
68
+ assert_nil(@cache.read('aaa', @options))
69
+ assert_nil(@cache.read('aa', @options))
70
+ end
71
+
72
+ def test_create_exist?
73
+ @cache.write(@key, @value, @options)
74
+ assert_equal(true, @cache.exist?(@key, @options))
75
+ end
76
+
77
+ def test_delete_exists
78
+ @cache.write(@key, @value, @options)
79
+ @cache.delete(@key, @options)
80
+ assert_equal(false, @cache.exist?(@key, @options))
81
+ end
82
+
83
+ def test_increment
84
+ @cache.write(@key, 1)
85
+ assert_equal(1, @cache.read(@key).to_i)
86
+ @cache.increment(@key)
87
+ assert_equal(2, @cache.read(@key).to_i)
88
+ end
89
+
90
+ def test_decrement
91
+ @cache.write(@key, 1)
92
+ assert_equal(1, @cache.read(@key).to_i)
93
+ @cache.decrement(@key)
94
+ assert_equal(0, @cache.read(@key).to_i)
95
+ end
96
+
97
+ def test_cleanup
98
+ @cache.write(@key, @value)
99
+ @cache.cleanup
100
+ assert_equal(0, @cache.redis.dbsize)
101
+ end
102
+
103
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_redis_cache
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Peter Schr\xC3\xB6der"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-10 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: -1442725791
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ - beta
35
+ version: 3.0.0.beta
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: redis
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 15
47
+ segments:
48
+ - 2
49
+ - 0
50
+ - 0
51
+ version: 2.0.0
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ description: Rails cache store implementation using Redis.
55
+ email: phoetmail@googlemail.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - lib/rails_redis_cache.rb
64
+ - README.rdoc
65
+ - test/test_helper.rb
66
+ - test/test_rails_redis_cache.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/phoet/rails_redis_cache
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - -a
74
+ - --inline-source
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.7
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Rails cache store implementation using Redis.
103
+ test_files:
104
+ - test/test_helper.rb
105
+ - test/test_rails_redis_cache.rb