Skip to content

Commit

Permalink
use marshalling for data
Browse files Browse the repository at this point in the history
  • Loading branch information
phoet committed Aug 10, 2010
1 parent 27ded75 commit 7ef423f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
html
pkg
*.gem
.DS_Store
File renamed without changes.
11 changes: 7 additions & 4 deletions lib/rails_redis_cache.rb
@@ -1,4 +1,5 @@
require 'active_support'
require 'base64'
require 'redis'
require 'time'

Expand All @@ -12,6 +13,7 @@ class RailsRedisCache < Store
attr_reader :redis

def initialize(options={})
super()
@redis = Redis.connect(options)
end

Expand All @@ -22,11 +24,12 @@ def read_entry(key, options)
return nil unless raw_value

time = Time.parse @redis.get("#{TIME_PREF}_#{key}")
ActiveSupport::Cache::Entry.create raw_value, time
puts "#{raw_value} #{raw_value.class}"
ActiveSupport::Cache::Entry.create Marshal.load(Base64.decode64(raw_value)), time
end

def write_entry(key, entry, options)
@redis.mset "#{VALUE_PREF}_#{key}", entry.value, "#{TIME_PREF}_#{key}", Time.now
@redis.mset "#{VALUE_PREF}_#{key}", Base64.encode64(Marshal.dump(entry.value.to_s)), "#{TIME_PREF}_#{key}", Time.now
return unless expiry = options[:expires_in]
@redis.expire "#{VALUE_PREF}_#{key}", expiry
@redis.expire "#{TIME_PREF}_#{key}", expiry
Expand All @@ -45,11 +48,11 @@ def delete_matched(matcher, options = nil)
end

def increment(name, amount = 1, options = nil)
@redis.incr "#{VALUE_PREF}_#{name}"
write(name, amount + read(name, options).to_i, options)
end

def decrement(name, amount = 1, options = nil)
@redis.decr "#{VALUE_PREF}_#{name}"
write(name, -1 * amount + read(name, options).to_i, options)
end

def cleanup(options = nil)
Expand Down
16 changes: 9 additions & 7 deletions rails_redis_cache.gemspec
@@ -1,7 +1,8 @@
Gem::Specification.new do |s|
# coding: utf-8

spec = Gem::Specification.new do |s|
s.name = 'rails_redis_cache'
s.version = '0.0.1'
s.rubyforge_project = 'none'

s.author = 'Peter Schröder'
s.description = 'Rails cache store implementation using Redis.'
Expand All @@ -11,10 +12,11 @@ Gem::Specification.new do |s|

s.has_rdoc = true
s.rdoc_options = ['-a', '--inline-source', '--charset=UTF-8']
s.extra_rdoc_files = ['README.textile']
s.files = [ 'README.rdoc', 'lib/rails_redis_cache.rb' ]
s.test_files = [ 'test/test_rails_redis_cache.rb' ]

s.add_dependency('activesupport', '>= 3.0.0')
s.files = Dir.glob('lib/*.rb') + %w(README.rdoc)
s.test_files = Dir.glob('test/test_*.rb')

s.add_dependency('activesupport', '>= 3.0.0.beta')
s.add_dependency('redis', '>= 2.0.0')
end
end

28 changes: 16 additions & 12 deletions test/test_rails_redis_cache.rb
Expand Up @@ -8,7 +8,7 @@ def setup
ActiveSupport::Cache::RailsRedisCache.logger = Logger.new(STDOUT)
@cache = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
@cache.redis.flushdb
@entry = "entry"
@value = "entry"
@key = "key"
@options = {:option => 'any'}
end
Expand All @@ -18,7 +18,7 @@ def test_delete_no_error
end

def test_write_no_error
@cache.write(@key, @entry, @options)
@cache.write(@key, @value, @options)
end

def test_read_no_error
Expand All @@ -28,13 +28,13 @@ def test_read_no_error
def test_read_write
@cache.delete(@key, @options)
assert_nil(@cache.read(@key, @options))
@cache.write(@key, @entry, @options)
assert_equal(@entry, @cache.read(@key, @options))
@cache.write(@key, @value, @options)
assert_equal(@value, @cache.read(@key, @options))
end

def test_expires_in
@cache.write(@key, @entry, :expires_in=>2.seconds)
assert_equal(@entry, @cache.read(@key, @options))
@cache.write(@key, @value, :expires_in=>2.seconds)
assert_equal(@value, @cache.read(@key, @options))
sleep(3)
assert_nil(@cache.read(@key, @options))
end
Expand All @@ -50,23 +50,27 @@ def test_fetch
assert_equal(count, 2)
end

def test_with_proc
@cache.fetch(@key, @options, &lambda{@value})
end

def test_delete_matched
@cache.write('aaaa', @entry, @options)
@cache.write('aaa', @entry, @options)
@cache.write('aa', @entry, @options)
@cache.write('aaaa', @value, @options)
@cache.write('aaa', @value, @options)
@cache.write('aa', @value, @options)
assert_equal(3, @cache.delete_matched(/aa+/, @options))
assert_nil(@cache.read('aaaa', @options))
assert_nil(@cache.read('aaa', @options))
assert_nil(@cache.read('aa', @options))
end

def test_create_exist?
@cache.write(@key, @entry, @options)
@cache.write(@key, @value, @options)
assert_equal(true, @cache.exist?(@key, @options))
end

def test_delete_exists
@cache.write(@key, @entry, @options)
@cache.write(@key, @value, @options)
@cache.delete(@key, @options)
assert_equal(false, @cache.exist?(@key, @options))
end
Expand All @@ -86,7 +90,7 @@ def test_decrement
end

def test_cleanup
@cache.write(@key, @entry)
@cache.write(@key, @value)
@cache.cleanup
assert_equal(0, @cache.redis.dbsize)
end
Expand Down

0 comments on commit 7ef423f

Please sign in to comment.