redis_store_jr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Nick Plante
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = Redis Store Jr.
2
+
3
+ This is a vastly simplified version of RedisStore.
4
+
5
+ Most of the code was lifted directly from an earlier version of Luca Guidi's
6
+ redis-store gem. It does *not* support Sinatra, Merb, or many of the other
7
+ options that his Gem does. So if you're looking for that stuff, you probably
8
+ want to take a look at the [the original
9
+ project](http://github.com/jodosha/redis-store).
10
+
11
+ On the other hand, if you're looking for a super-simple Redis store interface
12
+ that works reliably with Rails and Redis 1.2, without the complicated
13
+ (and often unnecessary) dependencies, look no further.
14
+
15
+ == Installation
16
+
17
+ We assume you already have [Redis](http://code.google.com/p/redis/) set up and
18
+ running. If not, you know what to do.
19
+
20
+ In your environment.rb:
21
+
22
+ config.gem 'redis_store_jr', :lib => 'redis_store'
23
+
24
+ Or your Gemfile:
25
+
26
+ gem 'redis_store_jr', :require => 'redis_store'
27
+
28
+ In your production (or staging) environment config:
29
+
30
+ require 'redis_store'
31
+ config.cache_store = :redis_store, 'localhost:6379'
32
+
33
+ This has only been tested on Rails 2.3.x. No guarantees about Rails 3.
34
+
35
+ == Note on Patches/Pull Requests
36
+
37
+ * Fork the project.
38
+ * Make your feature addition or bug fix.
39
+ * Add tests for it. This is important so I don't break it in a
40
+ future version unintentionally.
41
+ * Commit, do not mess with rakefile, version, or history.
42
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
43
+ * Send me a pull request. Bonus points for topic branches.
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2010 Nick Plante. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "redis_store_jr"
8
+ gem.summary = %Q{Simple Redis Cache Store for Rails}
9
+ gem.description = %Q{Simple Redis Cache Store for Rails}
10
+ gem.email = "nap@zerosum.org"
11
+ gem.homepage = "http://github.com/zapnap/redis_store_jr"
12
+ gem.authors = ["Nick Plante"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ gem.add_development_dependency "redis", "~>1.0"
16
+ gem.add_development_dependency "activesupport", "~>2.3"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ begin
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
43
+ rescue LoadError
44
+ task :yardoc do
45
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
46
+ end
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,81 @@
1
+ module ActiveSupport
2
+ module Cache
3
+ # A cache store implementation which stores data in Redis.
4
+ class RedisStore < Store
5
+ # Creates a new RedisStore client object, with the given Redis server
6
+ # address. The address should be given as a string. For example:
7
+ #
8
+ # ActiveSupport::Cache::RedisStore.new("localhost:6379/22")
9
+ #
10
+ # If no addresses are specified, then RedisStore will connect to the
11
+ # default Redis server port on localhost.
12
+ #
13
+ def initialize(address_or_options = {})
14
+ @data = ::RedisStore::MarshalledClient.new(::RedisStore::Config.new(address_or_options))
15
+ end
16
+
17
+ # Read a value from the cache.
18
+ def read(key, options = nil)
19
+ super
20
+ @data.marshalled_get(key, options)
21
+ end
22
+
23
+ # Write a value to the cache.
24
+ #
25
+ # Possible options:
26
+ # - +:unless_exist+ - set to true if you don't want to update the cache
27
+ # if the key is already set.
28
+ # - +:expires_in+ - the number of seconds that this value may stay in
29
+ # the cache. See ActiveSupport::Cache::Store#write for an example.
30
+ #
31
+ def write(key, value, options = nil)
32
+ super
33
+ method = options && options[:unless_exist] ? :marshalled_setnx : :marshalled_set
34
+ @data.send(method, key, value, options)
35
+ end
36
+
37
+ # Deleted a named value from the cache.
38
+ def delete(key, options = nil)
39
+ super
40
+ @data.del(key)
41
+ end
42
+
43
+ # Check for the existence of a particular key value.
44
+ def exist?(key, options = nil)
45
+ super
46
+ @data.exists(key)
47
+ end
48
+
49
+ # Note: to comply with the established cache store interface, matcher
50
+ # should be a Regexp. However, converting Regexps to globs (supported by
51
+ # Redis) is non-trivial.
52
+ #
53
+ # For now, we accept a string. Sorry about the conditional logic that is
54
+ # sure to follow :(.
55
+ #
56
+ def delete_matched(matcher, options = nil)
57
+ super
58
+ raise ArgumentError.new("Sorry, matcher must be a String") unless matcher.is_a?(String)
59
+ @data.keys(matcher).each { |key| @data.del(key) }
60
+ end
61
+
62
+ # Note: if key does not exist it will be initialized and return '1'.
63
+ def increment(key, amount = 1)
64
+ @data.incrby(key, amount)
65
+ end
66
+
67
+ # Note: if key does not exist it will be initialized and return '0'.
68
+ def decrement(key, amount = 1)
69
+ @data.decrby(key, amount)
70
+ end
71
+
72
+ def clear
73
+ @data.flushdb
74
+ end
75
+
76
+ def stats
77
+ @data.info
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,56 @@
1
+ class RedisStore
2
+ class MarshalledClient < Redis::Client
3
+ def marshalled_set(key, val, options = nil)
4
+ val = marshal_value(val, options)
5
+ if expires_in = expires_in(options)
6
+ set_with_expire(key, val, expires_in)
7
+ else
8
+ set(key, val)
9
+ end
10
+ end
11
+
12
+ def marshalled_setnx(key, val, options = nil)
13
+ val = marshal_value(val, options)
14
+ if expires_in = expires_in(options)
15
+ setnx_with_expire(key, val, expires_in)
16
+ else
17
+ setnx(key, val)
18
+ end
19
+ end
20
+
21
+ def setnx_with_expire(key, value, ttl)
22
+ multi do
23
+ setnx(key, val)
24
+ expire(key, expires_in)
25
+ end
26
+ end
27
+
28
+ def marshalled_get(key, options = nil)
29
+ result = call_command([:get, key])
30
+ result = Marshal.load result if unmarshal?(result, options)
31
+ result
32
+ end
33
+
34
+ private
35
+
36
+ def marshal_value(val, options)
37
+ raw?(options) ? val : Marshal.dump(val)
38
+ end
39
+
40
+ def unmarshal?(result, options)
41
+ result && result.size > 0 && !raw?(options)
42
+ end
43
+
44
+ def raw?(options)
45
+ options && options[:raw]
46
+ end
47
+
48
+ def expires_in(options)
49
+ if options
50
+ options[:expire_after] || options[:expire_in]
51
+ else
52
+ nil
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,26 @@
1
+ gem 'redis', '~> 1.0'
2
+ gem 'activesupport', '~> 2.3'
3
+
4
+ require 'redis'
5
+ require 'active_support'
6
+
7
+ require 'redis_store/marshalled_client'
8
+ require 'redis_store/cache/redis_store'
9
+
10
+ class RedisStore
11
+ class Config < Hash
12
+ def initialize(address_or_options)
13
+ options = if address_or_options.is_a?(Hash)
14
+ address_or_options
15
+ else
16
+ host, port = address_or_options.split(/\:/)
17
+ port, db = port.split(/\//) if port
18
+ { :host => host, :port => port, :db => db }
19
+ end
20
+
21
+ self[:host] = options[:host] if options[:host]
22
+ self[:port] = options[:port] if options[:port]
23
+ self[:db] = options[:db] if options[:db]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe ActiveSupport::Cache::RedisStore do
4
+ before(:each) do
5
+ @store = ActiveSupport::Cache::RedisStore.new('localhost:6379')
6
+ @store.clear
7
+
8
+ @obj = OpenStruct.new(:name => 'Test')
9
+ @store.write('abc', @obj)
10
+ end
11
+
12
+ it 'should read and write data' do
13
+ with_store do |store|
14
+ store.write('abc', @obj)
15
+ store.read('abc').should == @obj
16
+ end
17
+ end
18
+
19
+ it 'should read and write data with an expiration' do
20
+ with_store do |store|
21
+ store.write('abc', @obj, :expire_in => 1.second)
22
+ store.read('abc').should == @obj ; sleep 2
23
+ store.read('abc').should be_nil
24
+ end
25
+ end
26
+
27
+ it 'should only write data if not already set' do
28
+ with_store do |store|
29
+ new = OpenStruct.new(:name => 'New')
30
+ store.write('abc', new, :unless_exist => true)
31
+ store.read('abc').should == @obj
32
+ end
33
+ end
34
+
35
+ it 'should read raw data' do
36
+ with_store do |store|
37
+ store.read('abc', :raw => true).should == Marshal.dump(@obj)
38
+ end
39
+ end
40
+
41
+ it 'should write raw data' do
42
+ with_store do |store|
43
+ store.write('abc', @obj, :raw => true)
44
+ store.read('abc', :raw => true).should == %(#<OpenStruct name="Test">)
45
+ end
46
+ end
47
+
48
+ it 'should delete data' do
49
+ with_store do |store|
50
+ store.delete('abc')
51
+ store.read('abc').should be_nil
52
+ end
53
+ end
54
+
55
+ it 'should delete matched data given a string glob matcher' do
56
+ with_store do |store|
57
+ store.write('abd', @obj)
58
+ store.write('axe', @obj)
59
+
60
+ store.delete_matched('ab*')
61
+ store.read('abc').should be_nil
62
+ store.read('abd').should be_nil
63
+ store.read('axe').should_not be_nil
64
+ end
65
+ end
66
+
67
+ it 'should delete matched data given a regexp'
68
+
69
+ it 'should verify existence of an object' do
70
+ with_store do |store|
71
+ store.exist?('abd').should be_false
72
+ store.exist?('abc').should be_true
73
+ end
74
+ end
75
+
76
+ it 'should increment a key' do
77
+ with_store do |store|
78
+ 3.times { store.increment('ct') }
79
+ store.read('ct', :raw => true).to_i.should == 3
80
+ end
81
+ end
82
+
83
+ it 'should decrement a key' do
84
+ with_store do |store|
85
+ 3.times { store.increment('ct') }
86
+ 2.times { store.decrement('ct') }
87
+ store.read('ct', :raw => true).to_i.should == 1
88
+ end
89
+ end
90
+
91
+ it 'should increment a key by a given value' do
92
+ with_store do |store|
93
+ store.increment('ct')
94
+ store.increment('ct', 3)
95
+ store.read('ct', :raw => true).to_i.should == 4
96
+ end
97
+ end
98
+
99
+ it 'should decrement a key by a given value' do
100
+ with_store do |store|
101
+ 3.times { store.increment('ct') }
102
+ store.decrement('ct', 2)
103
+ store.read('ct', :raw => true).to_i.should == 1
104
+ end
105
+ end
106
+
107
+ it 'should clear the store' do
108
+ with_store do |store|
109
+ store.clear
110
+ store.read('abc').should be_nil
111
+ end
112
+ end
113
+
114
+ it 'should return store stats' do
115
+ with_store do |store|
116
+ store.stats.should_not be_nil
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ def with_store
123
+ yield @store
124
+ end
125
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe RedisStore::MarshalledClient do
4
+ before(:each) do
5
+ @store = RedisStore::MarshalledClient.new
6
+ @obj = OpenStruct.new(:name => 'Test')
7
+ @store.marshalled_set('abc', @obj)
8
+ end
9
+
10
+ after(:each) do
11
+ @store.quit
12
+ end
13
+
14
+ it 'should unmarshal an object on get' do
15
+ @store.marshalled_get('abc').should === @obj
16
+ end
17
+
18
+ it 'should marshal object on set' do
19
+ @store.marshalled_set('abc', @obj)
20
+ @store.marshalled_get('abc').should === @obj
21
+ end
22
+
23
+ it 'should not unmarshal object on get if raw option is true' do
24
+ @store.marshalled_get('abc', :raw => true).should == "\004\bU:\017OpenStruct{\006:\tname\"\tTest"
25
+ end
26
+
27
+ it 'should not marshal object on set if raw option is true' do
28
+ @store.marshalled_set('abc', @obj, :raw => true)
29
+ @store.marshalled_get('abc', :raw => true).should == %(#<OpenStruct name="Test">)
30
+ end
31
+
32
+ it 'should not unmarshal object if getting an empty string' do
33
+ @store.marshalled_set('empty_string', '')
34
+ lambda { @store.marshalled_get('empty_string').should == '' }.should_not raise_error
35
+ end
36
+
37
+ it 'should not set an object if it already exists' do
38
+ new = OpenStruct.new(:name => 'New')
39
+ @store.marshalled_setnx('abc', new)
40
+ @store.marshalled_get('abc').should === @obj
41
+ end
42
+
43
+ it 'should marshal object on set_unless_exists' do
44
+ @store.marshalled_setnx('def', @obj)
45
+ @store.marshalled_get('def').should === @obj
46
+ end
47
+
48
+ it 'should not marshal object on set_unless_exists if raw option is true' do
49
+ @store.marshalled_setnx('def', @obj, :raw => true)
50
+ @store.marshalled_get('def', :raw => true).should == "\004\bU:\017OpenStruct{\006:\tname\"\tTest"
51
+ end
52
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'redis_store'
5
+ require 'ostruct'
6
+
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_store_jr
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Nick Plante
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-04 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: redis
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 15
60
+ segments:
61
+ - 1
62
+ - 0
63
+ version: "1.0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: activesupport
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 5
75
+ segments:
76
+ - 2
77
+ - 3
78
+ version: "2.3"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description: Simple Redis Cache Store for Rails
82
+ email: nap@zerosum.org
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - .gitignore
93
+ - LICENSE
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/redis_store.rb
98
+ - lib/redis_store/cache/redis_store.rb
99
+ - lib/redis_store/marshalled_client.rb
100
+ - spec/cache_spec.rb
101
+ - spec/marshalled_client_spec.rb
102
+ - spec/spec.opts
103
+ - spec/spec_helper.rb
104
+ has_rdoc: true
105
+ homepage: http://github.com/zapnap/redis_store_jr
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options:
110
+ - --charset=UTF-8
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.3.7
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Simple Redis Cache Store for Rails
138
+ test_files:
139
+ - spec/cache_spec.rb
140
+ - spec/marshalled_client_spec.rb
141
+ - spec/spec_helper.rb