rails_redis_cache 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/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  == About
2
2
 
3
- Cache store implementation for Rails 3 using the key value store Redis.
3
+ Cache store implementation for Rails 3 using the key value store Redis[http://code.google.com/p/redis].
4
4
 
5
5
  == Usage
6
6
 
@@ -12,11 +12,17 @@ In the environment.rb or environments-files write:
12
12
 
13
13
  config.cache_store = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
14
14
 
15
- == Installing Redis
15
+ Using the cache is simple:
16
+
17
+ @tweets = cache("tweets", :expires_in => 30.seconds){ Twitter::Search.new(...) }
18
+
19
+ == Installing Redis with homebrew
16
20
 
17
21
  Using a local Redis server for testing is simple:
18
22
 
19
23
  brew install redis
20
24
  redis-server
21
25
 
26
+ == Changelog
22
27
 
28
+ See CHANGELOG file for further information.
@@ -4,7 +4,22 @@ require 'redis'
4
4
  require 'time'
5
5
 
6
6
  module ActiveSupport
7
- module Cache
7
+ module Cache
8
+
9
+ # RailsRedisCache is Rails 3 cache store implementation using the key value store Redis.
10
+ #
11
+ # Author:: Peter Schröder (mailto:phoetmail@googlemail.com)
12
+ #
13
+ # ==Usage
14
+ #
15
+ # Add the gem to your Gemfile
16
+ #
17
+ # gem "rails_redis_cache"
18
+ #
19
+ # and configure the cache store
20
+ #
21
+ # config.cache_store = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
22
+ #
8
23
  class RailsRedisCache < Store
9
24
 
10
25
  TIME_PREF = "rails_redis_cache_time"
@@ -12,35 +27,23 @@ module ActiveSupport
12
27
 
13
28
  attr_reader :redis
14
29
 
30
+ # Initializes the cache store and opens a connection to Redis.
31
+ #
32
+ # ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
33
+ #
34
+ # ==== Options:
35
+ #
36
+ # [url] the url to the Redis instance
37
+ #
38
+ # ==== More Options:
39
+ #
40
+ # Have a look at redis-rb and Rails docs for further options
41
+ #
15
42
  def initialize(options={})
16
43
  super(options)
17
44
  @redis = Redis.connect(options)
18
45
  end
19
46
 
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
47
  # ============================= optional store impl ==============================
45
48
 
46
49
  def delete_matched(matcher, options = nil)
@@ -67,6 +70,33 @@ module ActiveSupport
67
70
  cleanup(options)
68
71
  end
69
72
 
73
+ # ============================= basic store impl ==============================
74
+
75
+ protected
76
+
77
+ def read_entry(key, options)
78
+ raw_value = @redis.get "#{VALUE_PREF}_#{key}"
79
+ return nil unless raw_value
80
+
81
+ raw_time = @redis.get("#{TIME_PREF}_#{key}")
82
+ time = raw_time.nil? ? nil : Time.parse(raw_time)
83
+ value = Marshal.load(Base64.decode64(raw_value))
84
+ ActiveSupport::Cache::Entry.create value, time
85
+ end
86
+
87
+ def write_entry(key, entry, options)
88
+ value = Base64.encode64(Marshal.dump(entry.value))
89
+ time = Time.now
90
+ @redis.mset "#{VALUE_PREF}_#{key}", value, "#{TIME_PREF}_#{key}", time
91
+ return unless expiry = options[:expires_in]
92
+ @redis.expire "#{VALUE_PREF}_#{key}", expiry
93
+ @redis.expire "#{TIME_PREF}_#{key}", expiry
94
+ end
95
+
96
+ def delete_entry(key, options)
97
+ @redis.del "#{VALUE_PREF}_#{key}", "#{TIME_PREF}_#{key}"
98
+ end
99
+
70
100
  end
71
101
  end
72
102
  end
@@ -99,5 +99,11 @@ class TestRailsRedisCache < Test::Unit::TestCase
99
99
  @cache.cleanup
100
100
  assert_equal(0, @cache.redis.dbsize)
101
101
  end
102
+
103
+ def test_edge_case_with_missing_time_key
104
+ @cache.write(@key, @value)
105
+ @cache.redis.del "#{ActiveSupport::Cache::RailsRedisCache::TIME_PREF}_#{@key}"
106
+ assert_equal(@value, @cache.read(@key))
107
+ end
102
108
 
103
109
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_redis_cache
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Peter Schr\xC3\xB6der"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-10 00:00:00 +02:00
18
+ date: 2010-08-13 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 867219037
29
+ hash: 680265570
30
30
  segments:
31
31
  - 3
32
32
  - 0