xlymian-redis-store 0.3.8

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.
@@ -0,0 +1,7 @@
1
+ *.rdb
2
+ *.swp
3
+ bin/
4
+ coverage/
5
+ pkg/
6
+ vendor/
7
+ tmp/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ gem "redis", "0.1.1"
2
+
3
+ # testing gems
4
+ only :testing do
5
+ gem "ruby-debug"
6
+ gem "rspec"
7
+ gem "rack", "1.1.0"
8
+ gem "rack-cache"
9
+ gem "activesupport"
10
+ gem "merb"
11
+ end
12
+
13
+ disable_system_gems
@@ -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.
@@ -0,0 +1,105 @@
1
+ # Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks
2
+
3
+ ## 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-1.02.tar.gz
8
+ tar -zxvf redis-1.02.tar.gz
9
+ mv redis-1.02 redis
10
+ cd redis
11
+ make
12
+
13
+ Install the gems
14
+
15
+ sudo gem install redis-rb redis-store
16
+
17
+ ## Cache store
18
+
19
+ Provides a cache store for your Ruby web framework of choice.
20
+
21
+ ### Rails
22
+
23
+ config.gem "redis-store", :source => "http://gemcutter.org", :lib => "redis-store"
24
+ require "redis-store"
25
+ config.cache_store = :redis_store
26
+
27
+ ### Merb
28
+
29
+ dependency "redis-store", "0.3.7"
30
+ dependency("merb-cache", merb_gems_version) do
31
+ Merb::Cache.setup do
32
+ register(:redis, Merb::Cache::RedisStore, :servers => ["127.0.0.1:6379"])
33
+ end
34
+ end
35
+
36
+ ### Sinatra
37
+
38
+ require "sinatra"
39
+ require "redis-store"
40
+ class MyApp < Sinatra::Base
41
+ register Sinatra::Cache
42
+ get "/hi" do
43
+ cache.fetch("greet") { "Hello, World!" }
44
+ end
45
+ end
46
+
47
+ ## Rack::Session
48
+
49
+ Provides a Redis store for Rack::Session. See [http://rack.rubyforge.org/doc/Rack/Session.html](http://rack.rubyforge.org/doc/Rack/Session.html)
50
+
51
+ ### Rack application
52
+
53
+ require "rack"
54
+ require "redis-store"
55
+ require "application"
56
+ use Rack::Session::Redis
57
+ run Application.new
58
+
59
+ ### Rails
60
+
61
+ config.gem "redis-store", :source => "http://gemcutter.org", :lib => "redis-store"
62
+ ActionController::Base.session_store = Rack::Session::Redis
63
+
64
+ ### Merb
65
+
66
+ dependency "redis-store", "0.3.7"
67
+ Merb::Config.use do |c|
68
+ c[:session_store] = "redis"
69
+ end
70
+ Merb::BootLoader.before_app_loads do
71
+ Merb::SessionContainer.subclasses << "Merb::RedisSession"
72
+ end
73
+
74
+ ### Sinatra
75
+
76
+ Sorry, but Sinatra application boot system [hardcode](http://github.com/sinatra/sinatra/blob/0f02bafe86f8dd9bba9ab425468cb1067caa83ff/lib/sinatra/base.rb#L785) `Rack::Session::Cookie`
77
+
78
+ ## Rack::Cache
79
+
80
+ Provides a Redis store for HTTP caching. See [http://github.com/rtomayko/rack-cache](http://github.com/rtomayko/rack-cache)
81
+
82
+ require "rack"
83
+ require "rack/cache"
84
+ require "redis-store"
85
+ require "application"
86
+ use Rack::Cache,
87
+ :metastore => 'redis://localhost:6379/0',
88
+ :entitystore => 'redis://localhost:6380/1'
89
+ run Application.new
90
+
91
+ ## Running specs
92
+
93
+ gem install jeweler bundler
94
+ git clone git://github.com/jodosha/redis-store.git
95
+ cd redis-store
96
+ gem bundle
97
+ rake dtach:install
98
+ rake redis:install
99
+ rake
100
+
101
+ If you are on **Snow Leopard** you have to run `env ARCHFLAGS="-arch x86_64" gem bundle`
102
+
103
+ ## Copyright
104
+
105
+ (c) 2009 Luca Guidi - [http://lucaguidi.com](http://lucaguidi.com), released under the MIT license
@@ -0,0 +1,57 @@
1
+ $:.unshift 'lib'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'spec/rake/spectask'
6
+
7
+ task :default => "spec:suite"
8
+
9
+ begin
10
+ require "jeweler"
11
+ Jeweler::Tasks.new do |gemspec|
12
+ gemspec.name = "xlymian-redis-store"
13
+ gemspec.summary = "Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks."
14
+ gemspec.description = "Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks."
15
+ gemspec.email = "guidi.luca@gmail.com"
16
+ gemspec.homepage = "http://github.com/xlymian/redis-store"
17
+ gemspec.authors = [ "Luca Guidi" ]
18
+ end
19
+
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ namespace :spec do
26
+ desc "Run all the examples by starting a detached Redis instance"
27
+ task :suite do
28
+ invoke_with_redis_cluster "spec:run"
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:run) do |t|
32
+ t.spec_files = FileList['spec/**/*_spec.rb']
33
+ t.spec_opts = %w(-fs --color)
34
+ end
35
+ end
36
+
37
+ desc "Run all examples with RCov"
38
+ task :rcov do
39
+ invoke_with_redis_cluster "rcov_run"
40
+ end
41
+
42
+ Spec::Rake::SpecTask.new(:rcov_run) do |t|
43
+ t.spec_files = FileList['spec/**/*_spec.rb']
44
+ t.rcov = true
45
+ end
46
+
47
+ # courtesy of http://github.com/ezmobius/redis-rb team
48
+ load "tasks/redis.tasks.rb"
49
+ def invoke_with_redis_cluster(task_name)
50
+ begin
51
+ result = RedisClusterRunner.start_detached
52
+ raise("Could not start redis-server, aborting.") unless result
53
+ Rake::Task[task_name].invoke
54
+ ensure
55
+ RedisClusterRunner.stop
56
+ end
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.8
@@ -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