redis-store 1.0.0.1 → 1.1.0.rc
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/Gemfile +2 -34
- data/README.md +17 -220
- data/Rakefile +7 -54
- data/lib/redis-store.rb +11 -44
- data/lib/redis/distributed_store.rb +8 -1
- data/lib/redis/factory.rb +17 -21
- data/lib/redis/store.rb +3 -8
- data/lib/redis/store/interface.rb +4 -0
- data/lib/redis/store/marshalling.rb +4 -0
- data/lib/redis/store/version.rb +1 -8
- data/lib/tasks/redis.tasks.rb +167 -0
- data/redis-store.gemspec +22 -97
- data/{spec → test}/config/node-one.conf +2 -2
- data/{spec → test}/config/node-two.conf +2 -2
- data/{spec → test}/config/redis.conf +3 -2
- data/{spec/redis/distributed_store_spec.rb → test/redis/distributed_store_test.rb} +13 -15
- data/test/redis/factory_test.rb +98 -0
- data/test/redis/store/interface_test.rb +27 -0
- data/test/redis/store/marshalling_test.rb +127 -0
- data/test/redis/store/namespace_test.rb +86 -0
- data/test/redis/store/version_test.rb +7 -0
- data/test/redis/store_test.rb +17 -0
- data/test/test_helper.rb +22 -0
- metadata +85 -97
- data/.travis.yml +0 -7
- data/CHANGELOG +0 -311
- data/VERSION +0 -1
- data/lib/action_controller/session/redis_session_store.rb +0 -81
- data/lib/active_support/cache/redis_store.rb +0 -254
- data/lib/cache/merb/redis_store.rb +0 -79
- data/lib/cache/sinatra/redis_store.rb +0 -131
- data/lib/i18n/backend/redis.rb +0 -67
- data/lib/rack/cache/redis_entitystore.rb +0 -48
- data/lib/rack/cache/redis_metastore.rb +0 -40
- data/lib/rack/session/merb.rb +0 -32
- data/lib/rack/session/redis.rb +0 -88
- data/spec/action_controller/session/redis_session_store_spec.rb +0 -126
- data/spec/active_support/cache/redis_store_spec.rb +0 -426
- data/spec/cache/merb/redis_store_spec.rb +0 -143
- data/spec/cache/sinatra/redis_store_spec.rb +0 -192
- data/spec/i18n/backend/redis_spec.rb +0 -72
- data/spec/rack/cache/entitystore/pony.jpg +0 -0
- data/spec/rack/cache/entitystore/redis_spec.rb +0 -124
- data/spec/rack/cache/metastore/redis_spec.rb +0 -259
- data/spec/rack/session/redis_spec.rb +0 -234
- data/spec/redis/factory_spec.rb +0 -110
- data/spec/redis/store/interface_spec.rb +0 -23
- data/spec/redis/store/marshalling_spec.rb +0 -119
- data/spec/redis/store/namespace_spec.rb +0 -76
- data/spec/redis/store/version_spec.rb +0 -7
- data/spec/redis/store_spec.rb +0 -13
- data/spec/spec_helper.rb +0 -43
- data/tasks/redis.tasks.rb +0 -235
@@ -1,79 +0,0 @@
|
|
1
|
-
module Merb
|
2
|
-
module Cache
|
3
|
-
class RedisStore < AbstractStore
|
4
|
-
# Instantiate the store.
|
5
|
-
#
|
6
|
-
# Example:
|
7
|
-
# RedisStore.new
|
8
|
-
# # => host: localhost, port: 6379, db: 0
|
9
|
-
#
|
10
|
-
# RedisStore.new :servers => ["example.com"]
|
11
|
-
# # => host: example.com, port: 6379, db: 0
|
12
|
-
#
|
13
|
-
# RedisStore.new :servers => ["example.com:23682"]
|
14
|
-
# # => host: example.com, port: 23682, db: 0
|
15
|
-
#
|
16
|
-
# RedisStore.new :servers => ["example.com:23682/1"]
|
17
|
-
# # => host: example.com, port: 23682, db: 1
|
18
|
-
#
|
19
|
-
# RedisStore.new :servers => ["example.com:23682/1/theplaylist"]
|
20
|
-
# # => host: example.com, port: 23682, db: 1, namespace: theplaylist
|
21
|
-
#
|
22
|
-
# RedisStore.new :servers => ["localhost:6379/0", "localhost:6380/0"]
|
23
|
-
# # => instantiate a cluster
|
24
|
-
def initialize(config = { })
|
25
|
-
@data = Redis::Factory.create config[:servers]
|
26
|
-
end
|
27
|
-
|
28
|
-
def writable?(key, parameters = {}, conditions = {})
|
29
|
-
true
|
30
|
-
end
|
31
|
-
|
32
|
-
def read(key, parameters = {}, conditions = {})
|
33
|
-
@data.get normalize(key, parameters), conditions
|
34
|
-
end
|
35
|
-
|
36
|
-
def write(key, data = nil, parameters = {}, conditions = {})
|
37
|
-
if writable?(key, parameters, conditions)
|
38
|
-
method = conditions && conditions[:unless_exist] ? :setnx : :set
|
39
|
-
@data.send method, normalize(key, parameters), data, conditions
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def write_all(key, data = nil, parameters = {}, conditions = {})
|
44
|
-
write key, data, parameters, conditions
|
45
|
-
end
|
46
|
-
|
47
|
-
def fetch(key, parameters = {}, conditions = {}, &blk)
|
48
|
-
(data = read(key, parameters)) || block_given? && begin
|
49
|
-
data = yield
|
50
|
-
write(key, data, parameters, conditions)
|
51
|
-
end
|
52
|
-
data || nil
|
53
|
-
end
|
54
|
-
|
55
|
-
def exists?(key, parameters = {})
|
56
|
-
@data.exists normalize(key, parameters)
|
57
|
-
end
|
58
|
-
|
59
|
-
def delete(key, parameters = {})
|
60
|
-
@data.del normalize(key, parameters)
|
61
|
-
end
|
62
|
-
|
63
|
-
def delete_all
|
64
|
-
@data.flushdb
|
65
|
-
end
|
66
|
-
|
67
|
-
def delete_all!
|
68
|
-
delete_all
|
69
|
-
end
|
70
|
-
|
71
|
-
private
|
72
|
-
# Returns cache key calculated from base key
|
73
|
-
# and SHA2 hex from parameters.
|
74
|
-
def normalize(key, parameters = {})
|
75
|
-
parameters.empty? ? "#{key}" : "#{key}--#{parameters.to_sha2}"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
@@ -1,131 +0,0 @@
|
|
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
|
data/lib/i18n/backend/redis.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
module I18n
|
2
|
-
module Backend
|
3
|
-
class Redis
|
4
|
-
include Base, Flatten
|
5
|
-
attr_accessor :store
|
6
|
-
|
7
|
-
# Instantiate the store.
|
8
|
-
#
|
9
|
-
# Example:
|
10
|
-
# RedisStore.new
|
11
|
-
# # => host: localhost, port: 6379, db: 0
|
12
|
-
#
|
13
|
-
# RedisStore.new "example.com"
|
14
|
-
# # => host: example.com, port: 6379, db: 0
|
15
|
-
#
|
16
|
-
# RedisStore.new "example.com:23682"
|
17
|
-
# # => host: example.com, port: 23682, db: 0
|
18
|
-
#
|
19
|
-
# RedisStore.new "example.com:23682/1"
|
20
|
-
# # => host: example.com, port: 23682, db: 1
|
21
|
-
#
|
22
|
-
# RedisStore.new "example.com:23682/1/theplaylist"
|
23
|
-
# # => host: example.com, port: 23682, db: 1, namespace: theplaylist
|
24
|
-
#
|
25
|
-
# RedisStore.new "localhost:6379/0", "localhost:6380/0"
|
26
|
-
# # => instantiate a cluster
|
27
|
-
def initialize(*addresses)
|
28
|
-
@store = ::Redis::Factory.create(addresses)
|
29
|
-
end
|
30
|
-
|
31
|
-
def translate(locale, key, options = {})
|
32
|
-
options[:resolve] ||= false
|
33
|
-
super locale, key, options
|
34
|
-
end
|
35
|
-
|
36
|
-
def store_translations(locale, data, options = {})
|
37
|
-
escape = options.fetch(:escape, true)
|
38
|
-
flatten_translations(locale, data, escape, false).each do |key, value|
|
39
|
-
case value
|
40
|
-
when Proc
|
41
|
-
raise "Key-value stores cannot handle procs"
|
42
|
-
else
|
43
|
-
@store.set "#{locale}.#{key}", value
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def available_locales
|
49
|
-
locales = @store.keys.map { |k| k =~ /\./; $` }
|
50
|
-
locales.uniq!
|
51
|
-
locales.compact!
|
52
|
-
locales.map! { |k| k.to_sym }
|
53
|
-
locales
|
54
|
-
end
|
55
|
-
|
56
|
-
protected
|
57
|
-
def lookup(locale, key, scope = [], options = {})
|
58
|
-
key = normalize_flat_keys(locale, key, scope, options[:separator])
|
59
|
-
@store.get "#{locale}.#{key}"
|
60
|
-
end
|
61
|
-
|
62
|
-
def resolve_link(locale, key)
|
63
|
-
key
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Cache
|
3
|
-
class EntityStore
|
4
|
-
class RedisBase < EntityStore
|
5
|
-
# The underlying ::Redis instance used to communicate with the Redis daemon.
|
6
|
-
attr_reader :cache
|
7
|
-
|
8
|
-
extend Rack::Utils
|
9
|
-
|
10
|
-
def open(key)
|
11
|
-
data = read(key)
|
12
|
-
data && [data]
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.resolve(uri)
|
16
|
-
new ::Redis::Factory.convert_to_redis_client_options(uri.to_s)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class Redis < RedisBase
|
21
|
-
def initialize(server, options = {})
|
22
|
-
@cache = ::Redis.new server
|
23
|
-
end
|
24
|
-
|
25
|
-
def exist?(key)
|
26
|
-
cache.exists key
|
27
|
-
end
|
28
|
-
|
29
|
-
def read(key)
|
30
|
-
cache.get key
|
31
|
-
end
|
32
|
-
|
33
|
-
def write(body)
|
34
|
-
buf = StringIO.new
|
35
|
-
key, size = slurp(body){|part| buf.write(part) }
|
36
|
-
[key, size] if cache.set(key, buf.string)
|
37
|
-
end
|
38
|
-
|
39
|
-
def purge(key)
|
40
|
-
cache.del key
|
41
|
-
nil
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
REDIS = Redis
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Cache
|
3
|
-
class MetaStore
|
4
|
-
class RedisBase < MetaStore
|
5
|
-
extend Rack::Utils
|
6
|
-
|
7
|
-
# The Redis::Store object used to communicate with the Redis daemon.
|
8
|
-
attr_reader :cache
|
9
|
-
|
10
|
-
def self.resolve(uri)
|
11
|
-
new ::Redis::Factory.convert_to_redis_client_options(uri.to_s)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class Redis < RedisBase
|
16
|
-
def initialize(server, options = {})
|
17
|
-
options[:redis_server] ||= server
|
18
|
-
@cache = ::Redis::Factory.create options
|
19
|
-
end
|
20
|
-
|
21
|
-
def read(key)
|
22
|
-
key = hexdigest(key)
|
23
|
-
cache.get(key) || []
|
24
|
-
end
|
25
|
-
|
26
|
-
def write(key, entries)
|
27
|
-
key = hexdigest(key)
|
28
|
-
cache.set(key, entries)
|
29
|
-
end
|
30
|
-
|
31
|
-
def purge(key)
|
32
|
-
cache.del(hexdigest(key))
|
33
|
-
nil
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
REDIS = Redis
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
data/lib/rack/session/merb.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
module Merb
|
2
|
-
# HACK for cyclic dependency: redis-store is required before Merb session stores
|
3
|
-
class Mash < Hash; end
|
4
|
-
class SessionContainer < Mash; class_inheritable_accessor :session_store_type end
|
5
|
-
class SessionStoreContainer < SessionContainer; end
|
6
|
-
|
7
|
-
class RedisSession < SessionStoreContainer
|
8
|
-
self.session_store_type = :redis
|
9
|
-
end
|
10
|
-
|
11
|
-
module RedisStore
|
12
|
-
def retrieve_session(session_id)
|
13
|
-
get("session:#{session_id}")
|
14
|
-
end
|
15
|
-
|
16
|
-
def store_session(session_id, data)
|
17
|
-
set("session:#{session_id}", data)
|
18
|
-
end
|
19
|
-
|
20
|
-
def delete_session(session_id)
|
21
|
-
delete("session:#{session_id}")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
module Rack
|
27
|
-
module Session
|
28
|
-
class Redis
|
29
|
-
include Merb::RedisStore
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
data/lib/rack/session/redis.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Session
|
3
|
-
class Redis < Abstract::ID
|
4
|
-
attr_reader :mutex, :pool
|
5
|
-
DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :redis_server => "redis://127.0.0.1:6379/0"
|
6
|
-
|
7
|
-
def initialize(app, options = {})
|
8
|
-
super
|
9
|
-
@mutex = Mutex.new
|
10
|
-
options[:redis_server] ||= @default_options[:redis_server]
|
11
|
-
@pool = ::Redis::Factory.create options
|
12
|
-
end
|
13
|
-
|
14
|
-
def generate_sid
|
15
|
-
loop do
|
16
|
-
sid = super
|
17
|
-
break sid unless @pool.get(sid)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def get_session(env, sid)
|
22
|
-
session = @pool.get(sid) if sid
|
23
|
-
@mutex.lock if env['rack.multithread']
|
24
|
-
unless sid and session
|
25
|
-
env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
|
26
|
-
session = {}
|
27
|
-
sid = generate_sid
|
28
|
-
ret = @pool.set sid, session
|
29
|
-
raise "Session collision on '#{sid.inspect}'" unless ret
|
30
|
-
end
|
31
|
-
session.instance_variable_set('@old', {}.merge(session))
|
32
|
-
return [sid, session]
|
33
|
-
rescue Errno::ECONNREFUSED
|
34
|
-
warn "#{self} is unable to find server."
|
35
|
-
warn $!.inspect
|
36
|
-
return [ nil, {} ]
|
37
|
-
ensure
|
38
|
-
@mutex.unlock if env['rack.multithread']
|
39
|
-
end
|
40
|
-
|
41
|
-
def set_session(env, session_id, new_session, options)
|
42
|
-
@mutex.lock if env['rack.multithread']
|
43
|
-
session = @pool.get(session_id) rescue {}
|
44
|
-
if options[:renew] or options[:drop]
|
45
|
-
@pool.del session_id
|
46
|
-
return false if options[:drop]
|
47
|
-
session_id = generate_sid
|
48
|
-
@pool.set session_id, 0
|
49
|
-
end
|
50
|
-
old_session = new_session.instance_variable_get('@old') || {}
|
51
|
-
session = merge_sessions session_id, old_session, new_session, session
|
52
|
-
@pool.set session_id, session, options
|
53
|
-
return session_id
|
54
|
-
rescue Errno::ECONNREFUSED
|
55
|
-
warn "#{self} is unable to find server."
|
56
|
-
warn $!.inspect
|
57
|
-
return false
|
58
|
-
ensure
|
59
|
-
@mutex.unlock if env['rack.multithread']
|
60
|
-
end
|
61
|
-
|
62
|
-
def destroy_session(env, session_id, options)
|
63
|
-
options = { :renew => true }.update(options) unless options[:drop]
|
64
|
-
set_session(env, session_id, 0, options)
|
65
|
-
end
|
66
|
-
|
67
|
-
private
|
68
|
-
def merge_sessions(sid, old, new, cur=nil)
|
69
|
-
cur ||= {}
|
70
|
-
unless Hash === old and Hash === new
|
71
|
-
warn 'Bad old or new sessions provided.'
|
72
|
-
return cur
|
73
|
-
end
|
74
|
-
|
75
|
-
delete = old.keys - new.keys
|
76
|
-
warn "//@#{sid}: dropping #{delete*','}" if $DEBUG and not delete.empty?
|
77
|
-
delete.each{|k| cur.delete k }
|
78
|
-
|
79
|
-
update = new.keys.select{|k| new[k] != old[k] }
|
80
|
-
warn "//@#{sid}: updating #{update*','}" if $DEBUG and not update.empty?
|
81
|
-
update.each{|k| cur[k] = new[k] }
|
82
|
-
|
83
|
-
cur
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|