redis-store 0.3.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of redis-store might be problematic. Click here for more details.

data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 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.textile ADDED
@@ -0,0 +1,102 @@
1
+ h1. Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks
2
+
3
+ h2. Installation
4
+
5
+ Download and install Redis from "http://code.google.com/p/redis/":http://code.google.com/p/redis/
6
+
7
+ curl -OL "http://redis.googlecode.com/files/redis-0.900_2.tar.gz":http://redis.googlecode.com/files/redis-0.900_2.tar.gz
8
+ tar -zxvf redis-0.900_2.tar.gz
9
+ mv redis-0.900_2 redis
10
+ cd redis
11
+ make
12
+
13
+ Install the gems
14
+
15
+ -sudo gem install ezmobius-redis-rb -s http://gems.github.com-
16
+
17
+ git clone git://github.com/ezmobius/redis-rb.git
18
+ cd redis-rb
19
+ sudo rake install
20
+
21
+ sudo gem install jodosha-redis-store -s http://gems.github.com
22
+
23
+ h2. Cache store
24
+
25
+ Provides a cache store for your Ruby web framework of choice.
26
+
27
+ h3. How to use with Rails
28
+
29
+ config.gem "jodosha-redis-store", :source => "http://gems.github.com", :lib => "redis-store"
30
+ require "redis-store"
31
+ config.cache_store = :redis_store
32
+
33
+ h3. How to use with Merb
34
+
35
+ dependency "jodosha-redis-store", "0.3.6"
36
+ dependency("merb-cache", merb_gems_version) do
37
+ Merb::Cache.setup do
38
+ register(:redis, Merb::Cache::RedisStore, :servers => ["127.0.0.1:6379"])
39
+ end
40
+ end
41
+
42
+ h3. How to use with Sinatra
43
+
44
+ require 'rubygems'
45
+ require 'sinatra'
46
+ require 'jodosha-redis-store'
47
+ class MyApp < Sinatra::Base
48
+ register Sinatra::Cache
49
+ get '/hi' do
50
+ cache.fetch("greet") { "Hello, World!" }
51
+ end
52
+ end
53
+
54
+ h2. Rack::Session
55
+
56
+ Provides a Redis store for Rack::Session. See "http://rack.rubyforge.org/doc/Rack/Session.html":http://rack.rubyforge.org/doc/Rack/Session.html
57
+
58
+ h3. How to use with a generic Rack application
59
+
60
+ require "rubygems"
61
+ require "rack"
62
+ require "jodosha-redis-store"
63
+ require "application"
64
+ use Rack::Session::Redis
65
+ run Application.new
66
+
67
+ h3. How to use with Rails
68
+
69
+ config.gem "jodosha-redis-store", :source => "http://gems.github.com", :lib => "redis-store"
70
+ ActionController::Base.session_store = Rack::Session::Redis
71
+
72
+ h3. How to use with Merb
73
+
74
+ dependency "jodosha-redis-store", "0.3.6"
75
+ Merb::Config.use do |c|
76
+ c[:session_store] = 'redis'
77
+ end
78
+ Merb::BootLoader.before_app_loads do
79
+ Merb::SessionContainer.subclasses << "Merb::RedisSession"
80
+ end
81
+
82
+ h3. How to use with Sinatra
83
+
84
+ Sorry, but Sinatra application boot system "hardcode":http://github.com/sinatra/sinatra/blob/0f02bafe86f8dd9bba9ab425468cb1067caa83ff/lib/sinatra/base.rb#L785 Rack::Session::Cookie
85
+
86
+ h2. Rack::Cache
87
+
88
+ Provides a Redis store for HTTP caching. See "http://github.com/rtomayko/rack-cache":http://github.com/rtomayko/rack-cache
89
+
90
+ require "rubygems"
91
+ require "rack"
92
+ require "rack/cache"
93
+ require "jodosha-redis-store"
94
+ require "application"
95
+ use Rack::Cache,
96
+ :metastore => 'redis://localhost:6379/0',
97
+ :entitystore => 'redis://localhost:6380/1'
98
+ run Application.new
99
+
100
+ h2. Copyright
101
+
102
+ (c) 2009 Luca Guidi - "http://lucaguidi.com":http://lucaguidi.com, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ $:.unshift 'lib'
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+ require 'spec/rake/spectask'
7
+
8
+ REDIS_STORE_VERSION = "0.3.6"
9
+
10
+ task :default => :spec
11
+
12
+ desc 'Build and install the gem (useful for development purposes).'
13
+ task :install do
14
+ system "gem build redis-store.gemspec"
15
+ system "sudo gem uninstall redis-store"
16
+ system "sudo gem install --local --no-rdoc --no-ri redis-store-#{REDIS_STORE_VERSION}.gem"
17
+ system "rm redis-store-*.gem"
18
+ end
19
+
20
+ Spec::Rake::SpecTask.new do |t|
21
+ t.spec_files = FileList['spec/**/*_spec.rb']
22
+ t.spec_opts = %w(-fs --color)
23
+ end
24
+
25
+ desc 'Show the file list for the gemspec file'
26
+ task :files do
27
+ puts "Files:\n #{Dir['**/*'].reject {|f| File.directory?(f)}.sort.inspect}"
28
+ puts "Test files:\n #{Dir['spec/**/*_spec.rb'].reject {|f| File.directory?(f)}.sort.inspect}"
29
+ end
30
+
31
+ desc "Run all examples with RCov"
32
+ Spec::Rake::SpecTask.new(:rcov) do |t|
33
+ t.spec_files = FileList['spec/**/*_spec.rb']
34
+ t.rcov = true
35
+ end
36
+
37
+ namespace :redis do
38
+ desc 'Start the Redis cluster'
39
+ task :start => :clean do
40
+ system "redis-server spec/config/single.conf"
41
+ system "redis-server spec/config/master.conf"
42
+ system "redis-server spec/config/slave.conf"
43
+ end
44
+
45
+ desc 'Stop the Redis cluster'
46
+ task :stop do
47
+ # TODO replace with:
48
+ # system "kill -9 `tmp/redis-single.pid`"
49
+ # system "kill -9 `tmp/redis-master.pid`"
50
+ # system "kill -9 `tmp/redis-slave.pid`"
51
+ system "ps -eo pid,comm | grep redis | xargs kill -9"
52
+ end
53
+
54
+ desc 'Clean the tmp/ directory'
55
+ task :clean do
56
+ system "rm tmp/*" rescue nil
57
+ end
58
+ end
@@ -0,0 +1,63 @@
1
+ module Merb
2
+ module Cache
3
+ class RedisStore < AbstractStore
4
+ # Instantiate the store.
5
+ #
6
+ # Example:
7
+ # RedisStore.new # => host: localhost, port: 6379, db: 0
8
+ # RedisStore.new :servers => ["example.com"] # => host: example.com, port: 6379, db: 0
9
+ # RedisStore.new :servers => ["example.com:23682"] # => host: example.com, port: 23682, db: 0
10
+ # RedisStore.new :servers => ["example.com:23682/1"] # => host: example.com, port: 23682, db: 1
11
+ # RedisStore.new :servers => ["localhost:6379/0", "localhost:6380/0"] # => instantiate a cluster
12
+ def initialize(config = {})
13
+ @data = RedisFactory.create config[:servers]
14
+ end
15
+
16
+ def writable?(key, parameters = {}, conditions = {})
17
+ true
18
+ end
19
+
20
+ def read(key, parameters = {}, conditions = {})
21
+ @data.get normalize(key, parameters), conditions
22
+ end
23
+
24
+ def write(key, data = nil, parameters = {}, conditions = {})
25
+ if writable?(key, parameters, conditions)
26
+ method = conditions && conditions[:unless_exist] ? :set_unless_exists : :set
27
+ @data.send method, normalize(key, parameters), data, conditions
28
+ end
29
+ end
30
+
31
+ def write_all(key, data = nil, parameters = {}, conditions = {})
32
+ write key, data, parameters, conditions
33
+ end
34
+
35
+ def fetch(key, parameters = {}, conditions = {}, &blk)
36
+ read(key, parameters) || (write key, yield, parameters, conditions if block_given?)
37
+ end
38
+
39
+ def exists?(key, parameters = {})
40
+ @data.key? normalize(key, parameters)
41
+ end
42
+
43
+ def delete(key, parameters = {})
44
+ @data.delete normalize(key, parameters)
45
+ end
46
+
47
+ def delete_all
48
+ @data.flush_db
49
+ end
50
+
51
+ def delete_all!
52
+ delete_all
53
+ end
54
+
55
+ private
56
+ # Returns cache key calculated from base key
57
+ # and SHA2 hex from parameters.
58
+ def normalize(key, parameters = {})
59
+ parameters.empty? ? "#{key}" : "#{key}--#{parameters.to_sha2}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,109 @@
1
+ module ActiveSupport
2
+ module Cache
3
+ class RedisStore < Store
4
+ # Instantiate the store.
5
+ #
6
+ # Example:
7
+ # RedisStore.new # => host: localhost, port: 6379, db: 0
8
+ # RedisStore.new "example.com" # => host: example.com, port: 6379, db: 0
9
+ # RedisStore.new "example.com:23682" # => host: example.com, port: 23682, db: 0
10
+ # RedisStore.new "example.com:23682/1" # => host: example.com, port: 23682, db: 1
11
+ # RedisStore.new "localhost:6379/0", "localhost:6380/0" # => instantiate a cluster
12
+ def initialize(*addresses)
13
+ @data = RedisFactory.create(addresses)
14
+ end
15
+
16
+ def write(key, value, options = nil)
17
+ super
18
+ method = options && options[:unless_exist] ? :set_unless_exists : :set
19
+ @data.send method, key, value, options
20
+ end
21
+
22
+ def read(key, options = nil)
23
+ super
24
+ @data.get key, options
25
+ end
26
+
27
+ def delete(key, options = nil)
28
+ super
29
+ @data.delete key
30
+ end
31
+
32
+ def exist?(key, options = nil)
33
+ super
34
+ @data.key? key
35
+ end
36
+
37
+ # Increment a key in the store.
38
+ #
39
+ # If the key doesn't exist it will be initialized on 0.
40
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
41
+ #
42
+ # Example:
43
+ # We have two objects in cache:
44
+ # counter # => 23
45
+ # rabbit # => #<Rabbit:0x5eee6c>
46
+ #
47
+ # cache.increment "counter"
48
+ # cache.read "counter", :raw => true # => "24"
49
+ #
50
+ # cache.increment "counter", 6
51
+ # cache.read "counter", :raw => true # => "30"
52
+ #
53
+ # cache.increment "a counter"
54
+ # cache.read "a counter", :raw => true # => "1"
55
+ #
56
+ # cache.increment "rabbit"
57
+ # cache.read "rabbit", :raw => true # => "1"
58
+ def increment(key, amount = 1)
59
+ log "increment", key, amount
60
+ @data.incr key, amount
61
+ end
62
+
63
+ # Decrement a key in the store
64
+ #
65
+ # If the key doesn't exist it will be initialized on 0.
66
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
67
+ #
68
+ # Example:
69
+ # We have two objects in cache:
70
+ # counter # => 23
71
+ # rabbit # => #<Rabbit:0x5eee6c>
72
+ #
73
+ # cache.decrement "counter"
74
+ # cache.read "counter", :raw => true # => "22"
75
+ #
76
+ # cache.decrement "counter", 2
77
+ # cache.read "counter", :raw => true # => "20"
78
+ #
79
+ # cache.decrement "a counter"
80
+ # cache.read "a counter", :raw => true # => "-1"
81
+ #
82
+ # cache.decrement "rabbit"
83
+ # cache.read "rabbit", :raw => true # => "-1"
84
+ def decrement(key, amount = 1)
85
+ log "decrement", key, amount
86
+ @data.decr key, amount
87
+ end
88
+
89
+ # Delete objects for matched keys.
90
+ #
91
+ # Example:
92
+ # cache.delete_matched "rab*"
93
+ def delete_matched(matcher, options = nil)
94
+ super
95
+ @data.keys(matcher).each { |key| @data.delete key }
96
+ end
97
+
98
+ # Clear all the data from the store.
99
+ def clear
100
+ log "clear", nil, nil
101
+ @data.flush_db
102
+ end
103
+
104
+ def stats
105
+ @data.info
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,111 @@
1
+ module Sinatra
2
+ module Cache
3
+ class << self
4
+ def register(app)
5
+ app.set :cache, RedisStore.new
6
+ end
7
+ end
8
+
9
+ class RedisStore
10
+ # Instantiate the store.
11
+ #
12
+ # Example:
13
+ # RedisStore.new # => host: localhost, port: 6379, db: 0
14
+ # RedisStore.new "example.com" # => host: example.com, port: 6379, db: 0
15
+ # RedisStore.new "example.com:23682" # => host: example.com, port: 23682, db: 0
16
+ # RedisStore.new "example.com:23682/1" # => host: example.com, port: 23682, db: 1
17
+ # RedisStore.new "localhost:6379/0", "localhost:6380/0" # => instantiate a cluster
18
+ def initialize(*addresses)
19
+ @data = RedisFactory.create addresses
20
+ end
21
+
22
+ def write(key, value, options = nil)
23
+ method = options && options[:unless_exist] ? :set_unless_exists : :set
24
+ @data.send method, key, value, options
25
+ end
26
+
27
+ def read(key, options = nil)
28
+ @data.get key, options
29
+ end
30
+
31
+ def delete(key, options = nil)
32
+ @data.delete key
33
+ end
34
+
35
+ def exist?(key, options = nil)
36
+ @data.key? key
37
+ end
38
+
39
+ # Increment a key in the store.
40
+ #
41
+ # If the key doesn't exist it will be initialized on 0.
42
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
43
+ #
44
+ # Example:
45
+ # We have two objects in cache:
46
+ # counter # => 23
47
+ # rabbit # => #<Rabbit:0x5eee6c>
48
+ #
49
+ # cache.increment "counter"
50
+ # cache.read "counter", :raw => true # => "24"
51
+ #
52
+ # cache.increment "counter", 6
53
+ # cache.read "counter", :raw => true # => "30"
54
+ #
55
+ # cache.increment "a counter"
56
+ # cache.read "a counter", :raw => true # => "1"
57
+ #
58
+ # cache.increment "rabbit"
59
+ # cache.read "rabbit", :raw => true # => "1"
60
+ def increment(key, amount = 1)
61
+ @data.incr key, amount
62
+ end
63
+
64
+ # Decrement a key in the store
65
+ #
66
+ # If the key doesn't exist it will be initialized on 0.
67
+ # If the key exist but it isn't a Fixnum it will be initialized on 0.
68
+ #
69
+ # Example:
70
+ # We have two objects in cache:
71
+ # counter # => 23
72
+ # rabbit # => #<Rabbit:0x5eee6c>
73
+ #
74
+ # cache.decrement "counter"
75
+ # cache.read "counter", :raw => true # => "22"
76
+ #
77
+ # cache.decrement "counter", 2
78
+ # cache.read "counter", :raw => true # => "20"
79
+ #
80
+ # cache.decrement "a counter"
81
+ # cache.read "a counter", :raw => true # => "-1"
82
+ #
83
+ # cache.decrement "rabbit"
84
+ # cache.read "rabbit", :raw => true # => "-1"
85
+ def decrement(key, amount = 1)
86
+ @data.decr key, amount
87
+ end
88
+
89
+ # Delete objects for matched keys.
90
+ #
91
+ # Example:
92
+ # cache.delete_matched "rab*"
93
+ def delete_matched(matcher, options = nil)
94
+ @data.keys(matcher).each { |key| @data.delete key }
95
+ end
96
+
97
+ def fetch(key, options = {})
98
+ (!options[:force] && data = read(key, options)) || (write key, yield, options if block_given?)
99
+ end
100
+
101
+ # Clear all the data from the store.
102
+ def clear
103
+ @data.flush_db
104
+ end
105
+
106
+ def stats
107
+ @data.info
108
+ end
109
+ end
110
+ end
111
+ end