redis-sinatra 0.0.0 → 1.3.1.rc

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/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ tmp/*
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
+
4
+ gem 'redis-store', '1.1.0.rc', :path => File.expand_path('../../redis-store', __FILE__)
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 - 2011 Luca Guidi
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.md ADDED
@@ -0,0 +1,57 @@
1
+ # Redis stores for Sinatra
2
+
3
+ __`redis-sinatra`__ provides a Redis backed cache store for __Sinatra__. It natively supports object marshalling, timeouts, single or multiple nodes and namespaces.
4
+
5
+ ## Redis Installation
6
+
7
+ ### Option 1: Homebrew
8
+
9
+ MacOS X users should use [Homebrew](https://github.com/mxcl/homebrew) to install Redis:
10
+
11
+ brew install redis
12
+
13
+ ### Option 2: From Source
14
+
15
+ Download and install Redis from [http://redis.io](http://redis.io/)
16
+
17
+ wget http://redis.googlecode.com/files/redis-2.4.5.tar.gz
18
+ tar -zxf redis-2.4.5.tar.gz
19
+ mv redis-2.4.5 redis
20
+ cd redis
21
+ make
22
+
23
+ ## Usage
24
+
25
+ # Gemfile
26
+ gem 'redis-sinatra'
27
+
28
+ ### Cache Store:
29
+
30
+ require 'sinatra'
31
+ require 'redis-sinatra'
32
+
33
+ class MyApp < Sinatra::Base
34
+ register Sinatra::Cache
35
+ get "/hi" do
36
+ settings.cache.fetch("greet") { "Hello, World!" }
37
+ end
38
+ end
39
+
40
+ Keep in mind that the above fetch will return `"OK"` on success, not the return of the block.
41
+
42
+ #### Configuration
43
+
44
+ For advanced configuration options, please check the [Redis Store Wiki](https://github.com/jodosha/redis-store/wiki).
45
+
46
+ ## Running tests
47
+
48
+ git clone git://github.com/jodosha/redis-store.git
49
+ cd redis-store/redis-sinatra
50
+ gem install bundler --pre # required version: 1.1.rc
51
+ bundle exec rake
52
+
53
+ If you are on **Snow Leopard** you have to run `env ARCHFLAGS="-arch x86_64" bundle exec rake`
54
+
55
+ ## Copyright
56
+
57
+ (c) 2009 - 2011 Luca Guidi - [http://lucaguidi.com](http://lucaguidi.com), released under the MIT license
data/Rakefile CHANGED
@@ -1 +1,13 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler'
2
+ Bundler.setup
3
+ require 'rake'
4
+ require 'bundler/gem_tasks'
5
+
6
+ begin
7
+ require 'rdoc/task'
8
+ rescue LoadError
9
+ require 'rake/rdoctask'
10
+ end
11
+
12
+ load 'tasks/redis.tasks.rb'
13
+ task :default => 'redis:test:suite'
@@ -1,5 +1,5 @@
1
- module Redis
1
+ class Redis
2
2
  module Sinatra
3
- VERSION = "0.0.0"
3
+ VERSION = '1.3.1.rc'
4
4
  end
5
5
  end
data/lib/redis-sinatra.rb CHANGED
@@ -1,6 +1,9 @@
1
- require "redis-sinatra/version"
1
+ require 'sinatra'
2
+ require 'redis-store'
3
+ require 'redis-sinatra/version'
4
+ require 'sinatra/cache/redis_store'
2
5
 
3
- module Redis
6
+ class Redis
4
7
  module Sinatra
5
8
  # Your code goes here...
6
9
  end
@@ -0,0 +1,131 @@
1
+ module Sinatra
2
+ module Cache
3
+ class << self
4
+ def register(app)
5
+ app.set :cache, RedisStore.new
6
+ end
7
+ alias_method :registered, :register
8
+ end
9
+
10
+ class RedisStore
11
+ # Instantiate the store.
12
+ #
13
+ # Example:
14
+ # RedisStore.new
15
+ # # => host: localhost, port: 6379, db: 0
16
+ #
17
+ # RedisStore.new "example.com"
18
+ # # => host: example.com, port: 6379, db: 0
19
+ #
20
+ # RedisStore.new "example.com:23682"
21
+ # # => host: example.com, port: 23682, db: 0
22
+ #
23
+ # RedisStore.new "example.com:23682/1"
24
+ # # => host: example.com, port: 23682, db: 1
25
+ #
26
+ # RedisStore.new "example.com:23682/1/theplaylist"
27
+ # # => host: example.com, port: 23682, db: 1, namespace: theplaylist
28
+ #
29
+ # RedisStore.new "localhost:6379/0", "localhost:6380/0"
30
+ # # => instantiate a cluster
31
+ def initialize(*addresses)
32
+ @data = Redis::Factory.create addresses
33
+ end
34
+
35
+ def write(key, value, options = nil)
36
+ if options && options[:unless_exist]
37
+ @data.setnx key, value, options
38
+ else
39
+ @data.set key, value, options
40
+ end
41
+ end
42
+
43
+ def read(key, options = nil)
44
+ @data.get(key, options)
45
+ end
46
+
47
+ def delete(key, options = nil)
48
+ @data.del key
49
+ end
50
+
51
+ def exist?(key, options = nil)
52
+ @data.exists key
53
+ end
54
+
55
+ # Increment a key in the store.
56
+ #
57
+ # If the key doesn't exist it will be initialized on 0.
58
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
59
+ #
60
+ # Example:
61
+ # We have two objects in cache:
62
+ # counter # => 23
63
+ # rabbit # => #<Rabbit:0x5eee6c>
64
+ #
65
+ # cache.increment "counter"
66
+ # cache.read "counter", :raw => true # => "24"
67
+ #
68
+ # cache.increment "counter", 6
69
+ # cache.read "counter", :raw => true # => "30"
70
+ #
71
+ # cache.increment "a counter"
72
+ # cache.read "a counter", :raw => true # => "1"
73
+ #
74
+ # cache.increment "rabbit"
75
+ # cache.read "rabbit", :raw => true # => "1"
76
+ def increment(key, amount = 1)
77
+ @data.incrby key, amount
78
+ end
79
+
80
+ # Decrement a key in the store
81
+ #
82
+ # If the key doesn't exist it will be initialized on 0.
83
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
84
+ #
85
+ # Example:
86
+ # We have two objects in cache:
87
+ # counter # => 23
88
+ # rabbit # => #<Rabbit:0x5eee6c>
89
+ #
90
+ # cache.decrement "counter"
91
+ # cache.read "counter", :raw => true # => "22"
92
+ #
93
+ # cache.decrement "counter", 2
94
+ # cache.read "counter", :raw => true # => "20"
95
+ #
96
+ # cache.decrement "a counter"
97
+ # cache.read "a counter", :raw => true # => "-1"
98
+ #
99
+ # cache.decrement "rabbit"
100
+ # cache.read "rabbit", :raw => true # => "-1"
101
+ def decrement(key, amount = 1)
102
+ @data.decrby key, amount
103
+ end
104
+
105
+ # Delete objects for matched keys.
106
+ #
107
+ # Example:
108
+ # cache.del_matched "rab*"
109
+ def delete_matched(matcher, options = nil)
110
+ @data.keys(matcher).each { |key| @data.del key }
111
+ end
112
+
113
+ def fetch(key, options = {})
114
+ (!options[:force] && data = read(key, options)) || block_given? && begin
115
+ data = yield
116
+ write(key, data, options)
117
+ end
118
+ data || nil
119
+ end
120
+
121
+ # Clear all the data from the store.
122
+ def clear
123
+ @data.flushdb
124
+ end
125
+
126
+ def stats
127
+ @data.info
128
+ end
129
+ end
130
+ end
131
+ end
@@ -18,7 +18,12 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
21
+ s.add_dependency 'redis-store', '1.1.0.rc'
22
+ s.add_dependency 'sinatra', '1.3.1'
23
+
24
+ s.add_development_dependency 'rake', '~> 0.9.2.2'
25
+ s.add_development_dependency 'bundler', '~> 1.1.rc'
26
+ s.add_development_dependency 'mocha', '~> 0.10.0'
27
+ s.add_development_dependency 'minitest', '~> 2.8.0'
28
+ s.add_development_dependency 'purdytest', '~> 1.0.0'
24
29
  end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ describe Redis::Sinatra::VERSION do
4
+ it "must be equal to 1.3.1.rc" do
5
+ Redis::Sinatra::VERSION.must_equal '1.3.1.rc'
6
+ end
7
+ end
@@ -0,0 +1,166 @@
1
+ require 'test_helper'
2
+
3
+ class App
4
+ def initialize
5
+ @values = {}
6
+ end
7
+
8
+ def set(key, value)
9
+ @values[key] = value
10
+ end
11
+
12
+ def get(key)
13
+ @values[key]
14
+ end
15
+ end
16
+
17
+ describe Sinatra::Cache::RedisStore do
18
+ before do
19
+ @store = Sinatra::Cache::RedisStore.new
20
+ @dstore = Sinatra::Cache::RedisStore.new "redis://127.0.0.1:6380/1", "redis://127.0.0.1:6381/1"
21
+ @rabbit = OpenStruct.new :name => "bunny"
22
+ @white_rabbit = OpenStruct.new :color => "white"
23
+ with_store_management do |store|
24
+ store.write "rabbit", @rabbit
25
+ store.delete "counter"
26
+ store.delete "rub-a-dub"
27
+ end
28
+ end
29
+
30
+ it "should register as extension" do
31
+ app = App.new
32
+ Sinatra::Cache.register(app)
33
+ store = app.get(:cache)
34
+ store.must_be_kind_of(Sinatra::Cache::RedisStore)
35
+ end
36
+
37
+ it "should read the data" do
38
+ with_store_management do |store|
39
+ store.read("rabbit").must_equal(@rabbit)
40
+ end
41
+ end
42
+
43
+ it "should write the data" do
44
+ with_store_management do |store|
45
+ store.write "rabbit", @white_rabbit
46
+ store.read("rabbit").must_equal(@white_rabbit)
47
+ end
48
+ end
49
+
50
+ it "should write the data with expiration time" do
51
+ with_store_management do |store|
52
+ store.write "rabbit", @white_rabbit, :expires_in => 1 # second
53
+ # store.read("rabbit").must_equal(@white_rabbit)
54
+ sleep 2
55
+ store.read("rabbit").must_be_nil
56
+ end
57
+ end
58
+
59
+ it "should not write data if :unless_exist option is true" do
60
+ with_store_management do |store|
61
+ store.write "rabbit", @white_rabbit, :unless_exist => true
62
+ store.read("rabbit").must_equal(@rabbit)
63
+ end
64
+ end
65
+
66
+ it "should read raw data" do
67
+ with_store_management do |store|
68
+ store.read("rabbit", :raw => true).must_equal(Marshal.dump(@rabbit))
69
+ end
70
+ end
71
+
72
+ it "should write raw data" do
73
+ with_store_management do |store|
74
+ store.write "rabbit", @white_rabbit, :raw => true
75
+ store.read("rabbit", :raw => true).must_equal(%(#<OpenStruct color="white">))
76
+ end
77
+ end
78
+
79
+ it "should delete data" do
80
+ with_store_management do |store|
81
+ store.delete "rabbit"
82
+ store.read("rabbit").must_be_nil
83
+ end
84
+ end
85
+
86
+ it "should delete matched data" do
87
+ with_store_management do |store|
88
+ store.delete_matched "rabb*"
89
+ store.read("rabbit").must_be_nil
90
+ end
91
+ end
92
+
93
+ it "should verify existence of an object in the store" do
94
+ with_store_management do |store|
95
+ assert store.exist?("rabbit")
96
+ assert ! store.exist?("rab-a-dub")
97
+ end
98
+ end
99
+
100
+ it "should increment a key" do
101
+ with_store_management do |store|
102
+ 3.times { store.increment "counter" }
103
+ store.read("counter", :raw => true).to_i.must_equal(3)
104
+ end
105
+ end
106
+
107
+ it "should decrement a key" do
108
+ with_store_management do |store|
109
+ 3.times { store.increment "counter" }
110
+ 2.times { store.decrement "counter" }
111
+ store.read("counter", :raw => true).to_i.must_equal(1)
112
+ end
113
+ end
114
+
115
+ it "should increment a key by given value" do
116
+ with_store_management do |store|
117
+ store.increment "counter", 3
118
+ store.read("counter", :raw => true).to_i.must_equal(3)
119
+ end
120
+ end
121
+
122
+ it "should decrement a key by given value" do
123
+ with_store_management do |store|
124
+ 3.times { store.increment "counter" }
125
+ store.decrement "counter", 2
126
+ store.read("counter", :raw => true).to_i.must_equal(1)
127
+ end
128
+ end
129
+
130
+ it "should clear the store" do
131
+ with_store_management do |store|
132
+ store.clear
133
+ store.instance_variable_get(:@data).keys("*").flatten.must_be :empty?
134
+ end
135
+ end
136
+
137
+ it "should return store stats" do
138
+ with_store_management do |store|
139
+ store.stats.wont_be :empty?
140
+ end
141
+ end
142
+
143
+ it "should fetch data" do
144
+ with_store_management do |store|
145
+ store.fetch("rabbit").must_equal(@rabbit)
146
+ store.fetch("rub-a-dub").must_be_nil
147
+ store.fetch("rub-a-dub") { "Flora de Cana" }.must_equal("Flora de Cana")
148
+ store.fetch("rub-a-dub").must_equal("Flora de Cana")
149
+ store.fetch("rabbit", :force => true).must_be_nil # force cache miss
150
+ store.fetch("rabbit", :force => true, :expires_in => 1) { @white_rabbit }
151
+ # store.fetch("rabbit").must_equal(@white_rabbit)
152
+ sleep 2
153
+ store.fetch("rabbit").must_be_nil
154
+ end
155
+ end
156
+
157
+ private
158
+ def instantiate_store(addresses = nil)
159
+ Sinatra::Cache::RedisStore.new(addresses).instance_variable_get(:@data)
160
+ end
161
+
162
+ def with_store_management
163
+ yield @store
164
+ yield @dstore
165
+ end
166
+ end
@@ -0,0 +1,6 @@
1
+ Bundler.setup
2
+ gem 'minitest'
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+ require 'mocha'
6
+ require 'redis-sinatra'
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease:
4
+ hash: 7712022
5
+ prerelease: 6
6
6
  segments:
7
- - 0
8
- - 0
9
- - 0
10
- version: 0.0.0
7
+ - 1
8
+ - 3
9
+ - 1
10
+ - rc
11
+ version: 1.3.1.rc
11
12
  platform: ruby
12
13
  authors:
13
14
  - Luca Guidi
@@ -15,9 +16,122 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-09-08 00:00:00 Z
19
- dependencies: []
20
-
19
+ date: 2011-12-30 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: redis-store
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7712002
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 0
34
+ - rc
35
+ version: 1.1.0.rc
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: sinatra
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ hash: 25
47
+ segments:
48
+ - 1
49
+ - 3
50
+ - 1
51
+ version: 1.3.1
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 11
63
+ segments:
64
+ - 0
65
+ - 9
66
+ - 2
67
+ - 2
68
+ version: 0.9.2.2
69
+ type: :development
70
+ version_requirements: *id003
71
+ - !ruby/object:Gem::Dependency
72
+ name: bundler
73
+ prerelease: false
74
+ requirement: &id004 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ hash: 7712070
80
+ segments:
81
+ - 1
82
+ - 1
83
+ - rc
84
+ version: 1.1.rc
85
+ type: :development
86
+ version_requirements: *id004
87
+ - !ruby/object:Gem::Dependency
88
+ name: mocha
89
+ prerelease: false
90
+ requirement: &id005 !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ~>
94
+ - !ruby/object:Gem::Version
95
+ hash: 55
96
+ segments:
97
+ - 0
98
+ - 10
99
+ - 0
100
+ version: 0.10.0
101
+ type: :development
102
+ version_requirements: *id005
103
+ - !ruby/object:Gem::Dependency
104
+ name: minitest
105
+ prerelease: false
106
+ requirement: &id006 !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ~>
110
+ - !ruby/object:Gem::Version
111
+ hash: 47
112
+ segments:
113
+ - 2
114
+ - 8
115
+ - 0
116
+ version: 2.8.0
117
+ type: :development
118
+ version_requirements: *id006
119
+ - !ruby/object:Gem::Dependency
120
+ name: purdytest
121
+ prerelease: false
122
+ requirement: &id007 !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ~>
126
+ - !ruby/object:Gem::Version
127
+ hash: 23
128
+ segments:
129
+ - 1
130
+ - 0
131
+ - 0
132
+ version: 1.0.0
133
+ type: :development
134
+ version_requirements: *id007
21
135
  description: Redis for Sinatra
22
136
  email:
23
137
  - guidi.luca@gmail.com
@@ -30,10 +144,16 @@ extra_rdoc_files: []
30
144
  files:
31
145
  - .gitignore
32
146
  - Gemfile
147
+ - MIT-LICENSE
148
+ - README.md
33
149
  - Rakefile
34
150
  - lib/redis-sinatra.rb
35
151
  - lib/redis-sinatra/version.rb
152
+ - lib/sinatra/cache/redis_store.rb
36
153
  - redis-sinatra.gemspec
154
+ - test/redis/sinatra/version_test.rb
155
+ - test/sinatra/cache/redis_store_test.rb
156
+ - test/test_helper.rb
37
157
  homepage: http://jodosha.github.com/redis-store
38
158
  licenses: []
39
159
 
@@ -54,12 +174,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
174
  required_rubygems_version: !ruby/object:Gem::Requirement
55
175
  none: false
56
176
  requirements:
57
- - - ">="
177
+ - - ">"
58
178
  - !ruby/object:Gem::Version
59
- hash: 3
179
+ hash: 25
60
180
  segments:
61
- - 0
62
- version: "0"
181
+ - 1
182
+ - 3
183
+ - 1
184
+ version: 1.3.1
63
185
  requirements: []
64
186
 
65
187
  rubyforge_project: redis-sinatra
@@ -67,5 +189,8 @@ rubygems_version: 1.8.6
67
189
  signing_key:
68
190
  specification_version: 3
69
191
  summary: Redis for Sinatra
70
- test_files: []
71
-
192
+ test_files:
193
+ - test/redis/sinatra/version_test.rb
194
+ - test/sinatra/cache/redis_store_test.rb
195
+ - test/test_helper.rb
196
+ has_rdoc: