rails_redis_cache 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ lib/*.rb
2
+ README.rdoc
3
+ CHANGELOG
4
+ LICENSE
@@ -0,0 +1,6 @@
1
+ html
2
+ pkg
3
+ *.gem
4
+ .DS_Store
5
+ .bundle
6
+ *.rdb
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@rails_redis_cache --create
@@ -0,0 +1,13 @@
1
+ script: "bundle exec rake"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - rbx
7
+ - rbx-2.0
8
+ - ree
9
+ - ruby-head
10
+ - jruby
11
+ branches:
12
+ only:
13
+ - develop
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 0.2.0
2
+
3
+ * renamed cache
4
+ * less restrictions to dependencies
5
+ * rspec instead of test-unit
6
+
1
7
  0.1.0
2
8
 
3
9
  * support for Rails 3.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # dependencies in rails_redis_cache.gemspec
4
+ gemspec
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_redis_cache (0.2.0)
5
+ activesupport (~> 3.1)
6
+ i18n (~> 0.6)
7
+ redis (~> 2.0)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ activesupport (3.1.3)
13
+ multi_json (~> 1.0)
14
+ diff-lcs (1.1.3)
15
+ fuubar (0.0.6)
16
+ rspec (~> 2.0)
17
+ rspec-instafail (~> 0.1.8)
18
+ ruby-progressbar (~> 0.0.10)
19
+ i18n (0.6.0)
20
+ multi_json (1.0.3)
21
+ rake (0.9.2.2)
22
+ redis (2.2.2)
23
+ rspec (2.7.0)
24
+ rspec-core (~> 2.7.0)
25
+ rspec-expectations (~> 2.7.0)
26
+ rspec-mocks (~> 2.7.0)
27
+ rspec-core (2.7.1)
28
+ rspec-expectations (2.7.0)
29
+ diff-lcs (~> 1.1.2)
30
+ rspec-instafail (0.1.9)
31
+ rspec-mocks (2.7.0)
32
+ ruby-progressbar (0.0.10)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ fuubar (~> 0.0.6)
39
+ rails_redis_cache!
40
+ rake (~> 0.9)
41
+ rspec (~> 2.7)
data/LICENSE ADDED
@@ -0,0 +1,4 @@
1
+ "THE BEER-WARE LICENSE" (Revision 42):
2
+ ps@nofail.de[mailto:ps@nofail.de] wrote this file. As long as you retain this notice you
3
+ can do whatever you want with this stuff. If we meet some day, and you think
4
+ this stuff is worth it, you can buy me a beer in return Peter Schröder
@@ -1,5 +1,8 @@
1
1
  == About
2
2
 
3
+ Status: http://stillmaintained.com/phoet/rails_redis_cache.png
4
+ Build: http://travis-ci.org/phoet/rails_redis_cache.png
5
+
3
6
  Cache store implementation for Rails 3.1 using the key value store Redis[http://code.google.com/p/redis].
4
7
 
5
8
  If you are looking for Rails 3 implementation please have a look at latest stable release version 0.0.4.
@@ -8,11 +11,11 @@ If you are looking for Rails 3 implementation please have a look at latest stabl
8
11
 
9
12
  In the environment.rb or environments-files write:
10
13
 
11
- ActionController::Base.cache_store = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
14
+ ActionController::Base.cache_store = ActiveSupport::Cache::RailsRedisCacheStore.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
12
15
 
13
16
  ... or ...
14
17
 
15
- config.cache_store = ActiveSupport::Cache::RailsRedisCache.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
18
+ config.cache_store = ActiveSupport::Cache::RailsRedisCacheStore.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
16
19
 
17
20
  Using the cache is simple:
18
21
 
@@ -28,3 +31,7 @@ Using a local Redis server for testing is simple:
28
31
  == Changelog
29
32
 
30
33
  See CHANGELOG file for further information.
34
+
35
+ == License
36
+
37
+ See LICENSE file for further information.
@@ -1,102 +1,2 @@
1
- require 'active_support'
2
- require 'base64'
3
- require 'redis'
4
- require 'time'
5
-
6
- module ActiveSupport
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
- #
23
- class RailsRedisCache < Store
24
-
25
- TIME_PREF = "rails_redis_cache_time"
26
- VALUE_PREF = "rails_redis_cache_value"
27
-
28
- attr_reader :redis
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
- #
42
- def initialize(options={})
43
- super(options)
44
- @redis = Redis.connect(options)
45
- end
46
-
47
- # ============================= optional store impl ==============================
48
-
49
- def delete_matched(matcher, options = nil)
50
- @redis.keys("#{VALUE_PREF}_*").map{|key| key[(VALUE_PREF.size + 1)..-1] }.grep(matcher).each do |key|
51
- delete_entry(key, options)
52
- end.size
53
- end
54
-
55
- def increment(name, amount = 1, options = nil)
56
- write(name, amount + read(name, options).to_i, options)
57
- end
58
-
59
- def decrement(name, amount = 1, options = nil)
60
- increment(name, amount * -1, options)
61
- end
62
-
63
- def cleanup(options = nil)
64
- value_keys = @redis.keys("#{VALUE_PREF}_*")
65
- time_keys = @redis.keys("#{TIME_PREF}_*")
66
- @redis.del *(value_keys + time_keys)
67
- end
68
-
69
- def clear(options = nil)
70
- cleanup(options)
71
- end
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
-
100
- end
101
- end
102
- end
1
+ require 'rails_redis_cache/rails_redis_cache_store'
2
+ require 'rails_redis_cache/version'
@@ -0,0 +1,101 @@
1
+ require 'active_support'
2
+ require 'base64'
3
+ require 'redis'
4
+ require 'time'
5
+
6
+ module ActiveSupport
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::RailsRedisCacheStore.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
22
+ #
23
+ class RailsRedisCacheStore < Store
24
+
25
+ TIME_PREF = "rails_redis_cache_time"
26
+ VALUE_PREF = "rails_redis_cache_value"
27
+
28
+ attr_reader :redis
29
+
30
+ # Initializes the cache store and opens a connection to Redis.
31
+ #
32
+ # ActiveSupport::Cache::RailsRedisCacheStore.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
+ #
42
+ def initialize(options={})
43
+ super(options)
44
+ @redis = Redis.connect(options)
45
+ end
46
+
47
+ # ============================= optional store impl ==============================
48
+
49
+ def delete_matched(matcher, options = nil)
50
+ @redis.keys("#{VALUE_PREF}_*").map{|key| key[(VALUE_PREF.size + 1)..-1] }.grep(matcher).each do |key|
51
+ delete_entry(key, options)
52
+ end.size
53
+ end
54
+
55
+ def increment(name, amount = 1, options = nil)
56
+ write(name, amount + read(name, options).to_i, options)
57
+ end
58
+
59
+ def decrement(name, amount = 1, options = nil)
60
+ increment(name, amount * -1, options)
61
+ end
62
+
63
+ def cleanup(options = nil)
64
+ value_keys = @redis.keys("#{VALUE_PREF}_*")
65
+ time_keys = @redis.keys("#{TIME_PREF}_*")
66
+ @redis.del(*(value_keys + time_keys))
67
+ end
68
+
69
+ def clear(options = nil)
70
+ cleanup(options)
71
+ end
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
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module RailsRedisCache
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rails_redis_cache/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rails_redis_cache"
7
+ s.version = RailsRedisCache::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Peter Schröder']
10
+ s.email = ['phoetmail@googlemail.com']
11
+ s.homepage = 'http://github.com/phoet/rails_redis_cache'
12
+ s.summary = 'Simple interface to AWS Lookup, Search and Cart operations.'
13
+ s.description = 'Rails 3.1 cache store implementation using Redis. See http://github.com/phoet/rails_redis_cache for more information.'
14
+
15
+ s.rubyforge_project = "rails_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
+
22
+ s.add_dependency('activesupport', '~> 3.1')
23
+ s.add_dependency('i18n', '~> 0.6')
24
+ s.add_dependency('redis', '~> 2.0')
25
+
26
+ s.add_development_dependency('rake', '~> 0.9')
27
+ s.add_development_dependency('rspec', '~> 2.7')
28
+ s.add_development_dependency('fuubar', '~> 0.0.6')
29
+ end
@@ -0,0 +1,11 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = ["--format Fuubar", "--color"]
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ActiveSupport::Cache::RailsRedisCacheStore do
4
+ before(:each) do
5
+ ActiveSupport::Cache::RailsRedisCacheStore.logger = Logger.new(STDOUT)
6
+ @cache = ActiveSupport::Cache::RailsRedisCacheStore.new(:url => ENV['RAILS_REDIS_CACHE_URL'])
7
+ @cache.redis.flushdb
8
+ @value = "entry"
9
+ @key = "key"
10
+ @options = {:option => 'any'}
11
+ end
12
+
13
+ it "test_delete_no_error" do
14
+ @cache.delete(@key, @options)
15
+ end
16
+
17
+ it "test_write_no_error" do
18
+ @cache.write(@key, @value, @options)
19
+ end
20
+
21
+ it "test_read_no_error" do
22
+ @cache.read(@key, @options)
23
+ end
24
+
25
+ it "test_read_write" do
26
+ @cache.delete(@key, @options)
27
+ @cache.read(@key, @options).should be_nil
28
+ @cache.write(@key, @value, @options)
29
+ @cache.read(@key, @options).should eql(@value)
30
+ end
31
+
32
+ it "test_expires_in" do
33
+ @cache.write(@key, @value, :expires_in=>2.seconds)
34
+ @cache.read(@key, @options).should eql(@value)
35
+ sleep(3)
36
+ @cache.read(@key, @options).should be_nil
37
+ end
38
+
39
+ it "test_fetch" do
40
+ count = 0
41
+ @cache.fetch(@key, :expires_in=>3.seconds) {count += 1}
42
+ @cache.fetch(@key) {count += 1}
43
+ @cache.fetch(@key) {count += 1}
44
+ count.should be(1)
45
+ @cache.delete(@key)
46
+ @cache.fetch(@key, :expires_in=>0.seconds) {count += 1}
47
+ count.should be(2)
48
+ end
49
+
50
+ it "test_fetch_with_proc" do
51
+ @cache.fetch(@key, @options, &lambda{@value})
52
+ end
53
+
54
+ it "test_fetch_with_array" do
55
+ @cache.fetch(@key, @options){[@value]}
56
+ @cache.fetch(@key, @options).should eql([@value])
57
+ end
58
+
59
+ it "test_delete_matched" do
60
+ @cache.write('aaaa', @value, @options)
61
+ @cache.write('aaa', @value, @options)
62
+ @cache.write('aa', @value, @options)
63
+ @cache.delete_matched(/aa+/, @options).should be(3)
64
+ @cache.read('aaaa', @options).should be_nil
65
+ @cache.read('aaa', @options).should be_nil
66
+ @cache.read('aa', @options).should be_nil
67
+ end
68
+
69
+ it "test_create_exist?" do
70
+ @cache.write(@key, @value, @options)
71
+ @cache.exist?(@key, @options).should be_true
72
+ end
73
+
74
+ it "test_delete_exists" do
75
+ @cache.write(@key, @value, @options)
76
+ @cache.delete(@key, @options)
77
+ @cache.exist?(@key, @options).should be_false
78
+ end
79
+
80
+ it "test_increment" do
81
+ @cache.write(@key, 1)
82
+ @cache.read(@key).should eql(1)
83
+ @cache.increment(@key)
84
+ @cache.read(@key).should eql(2)
85
+ end
86
+
87
+ it "test_decrement" do
88
+ @cache.write(@key, 1)
89
+ @cache.read(@key).should be(1)
90
+ @cache.decrement(@key)
91
+ @cache.read(@key).should be(0)
92
+ end
93
+
94
+ it "test_cleanup" do
95
+ @cache.write(@key, @value)
96
+ @cache.cleanup
97
+ @cache.redis.dbsize.should be(0)
98
+ end
99
+
100
+ it "test_edge_case_with_missing_time_key" do
101
+ @cache.write(@key, @value)
102
+ @cache.redis.del "#{ActiveSupport::Cache::RailsRedisCacheStore::TIME_PREF}_#{@key}"
103
+ @cache.read(@key).should eql(@value)
104
+ end
105
+ end
@@ -1,2 +1,4 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__),'..','..','lib')
2
- require 'test/unit'
2
+ require 'rspec'
3
+ require 'rails_redis_cache'
4
+ require 'logger'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_redis_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,72 +9,102 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-18 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2011-12-14 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activesupport
17
- requirement: &2153627500 !ruby/object:Gem::Requirement
16
+ requirement: &2159856240 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
21
20
  - !ruby/object:Gem::Version
22
- version: 3.1.0.rc5
21
+ version: '3.1'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *2153627500
24
+ version_requirements: *2159856240
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: i18n
28
- requirement: &2153627020 !ruby/object:Gem::Requirement
27
+ requirement: &2159855740 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ~>
32
31
  - !ruby/object:Gem::Version
33
- version: 0.6.0
32
+ version: '0.6'
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *2153627020
35
+ version_requirements: *2159855740
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: redis
39
- requirement: &2153626560 !ruby/object:Gem::Requirement
38
+ requirement: &2159855280 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ~>
43
42
  - !ruby/object:Gem::Version
44
- version: 2.0.0
43
+ version: '2.0'
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *2153626560
46
+ version_requirements: *2159855280
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: rake
50
- requirement: &2153626100 !ruby/object:Gem::Requirement
49
+ requirement: &2159854820 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ~>
54
53
  - !ruby/object:Gem::Version
55
- version: 0.9.2
54
+ version: '0.9'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *2153626100
59
- description: Rails 3.1 cache store implementation using Redis.
60
- email: phoetmail@googlemail.com
57
+ version_requirements: *2159854820
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &2159854360 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '2.7'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2159854360
69
+ - !ruby/object:Gem::Dependency
70
+ name: fuubar
71
+ requirement: &2159853900 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.0.6
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2159853900
80
+ description: Rails 3.1 cache store implementation using Redis. See http://github.com/phoet/rails_redis_cache
81
+ for more information.
82
+ email:
83
+ - phoetmail@googlemail.com
61
84
  executables: []
62
85
  extensions: []
63
86
  extra_rdoc_files: []
64
87
  files:
65
- - lib/rails_redis_cache.rb
66
- - README.rdoc
88
+ - .document
89
+ - .gitignore
90
+ - .rvmrc
91
+ - .travis.yml
67
92
  - CHANGELOG
68
- - test/test_helper.rb
69
- - test/test_rails_redis_cache.rb
70
- has_rdoc: true
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - README.rdoc
97
+ - lib/rails_redis_cache.rb
98
+ - lib/rails_redis_cache/rails_redis_cache_store.rb
99
+ - lib/rails_redis_cache/version.rb
100
+ - rails_redis_cache.gemspec
101
+ - rakefile.rb
102
+ - spec/rails_redis_cache/rails_redis_cache_store_spec.rb
103
+ - spec/spec_helper.rb
71
104
  homepage: http://github.com/phoet/rails_redis_cache
72
105
  licenses: []
73
106
  post_install_message:
74
- rdoc_options:
75
- - -a
76
- - --inline-source
77
- - --charset=UTF-8
107
+ rdoc_options: []
78
108
  require_paths:
79
109
  - lib
80
110
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -90,12 +120,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
120
  - !ruby/object:Gem::Version
91
121
  version: '0'
92
122
  requirements: []
93
- rubyforge_project: http://github.com/phoet/rails_redis_cache
94
- rubygems_version: 1.6.2
123
+ rubyforge_project: rails_redis_cache
124
+ rubygems_version: 1.8.10
95
125
  signing_key:
96
126
  specification_version: 3
97
- summary: Rails 3.1 cache store implementation using Redis. See http://github.com/phoet/rails_redis_cache
98
- for more information.
127
+ summary: Simple interface to AWS Lookup, Search and Cart operations.
99
128
  test_files:
100
- - test/test_helper.rb
101
- - test/test_rails_redis_cache.rb
129
+ - spec/rails_redis_cache/rails_redis_cache_store_spec.rb
130
+ - spec/spec_helper.rb
@@ -1,109 +0,0 @@
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
- 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
108
-
109
- end