redis_buddy 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,26 @@
1
1
  module RedisBuddy
2
2
  class CacheStore < ActiveSupport::Cache::Store
3
3
  def initialize(*adresses)
4
- ns = Rails.application.class.to_s.split('::').first.downcase
4
+ options = adresses.extract_options!
5
+ ns = options.delete(:redis_namespace) || Rails.application.class.to_s.split('::').first.downcase
5
6
  @r = RedisFactory.create(adresses)
6
7
  @client = Redis::Namespace.new(ns, :redis => @r)
8
+ super options
7
9
  end
8
10
 
9
- def write(key, value, options = {})
10
- method = :getset if options[:return]
11
- method = :setnx if options[:nx]
12
- method, ttl = :setex, options[:ttl] if options[:ttl]
13
- method = :set unless method
14
- value = encode(value)
11
+ def push(key, value)
12
+ @client.lpush(key, encode(value))
13
+ end
15
14
 
16
- ttl ? @client.send(method, key, ttl, value) : @client.send(method, key, value)
15
+ def pop(key)
16
+ value = @client.rpop(key)
17
+ value ? decode(value) : nil
17
18
  end
18
19
 
19
- def push(key, value)
20
- @client.lpush(key, encode(value))
20
+ def exist?(key)
21
+ @client.exists(key)
21
22
  end
23
+ alias :exists? :exist?
22
24
 
23
25
  def increment(key, amount = nil)
24
26
  amount.nil? ? @client.incr(key) : @client.incrby(key, amount)
@@ -28,6 +30,10 @@ module RedisBuddy
28
30
  amount.nil? ? @client.decr(key) : @client.decrby(key, amount)
29
31
  end
30
32
 
33
+ def delete_matched(matcher, options = nil)
34
+ delete keys(matcher)
35
+ end
36
+
31
37
  def expire_in(key, ttl)
32
38
  @client.expire(key, ttl)
33
39
  end
@@ -36,16 +42,6 @@ module RedisBuddy
36
42
  @client.exipireat(key, time)
37
43
  end
38
44
 
39
- def read(key)
40
- value = @client.get(key)
41
- value ? decode(value) : nil
42
- end
43
-
44
- def pop(key)
45
- value = @client.rpop(key)
46
- value ? decode(value) : nil
47
- end
48
-
49
45
  def length(key)
50
46
  @client.llen(key)
51
47
  end
@@ -58,14 +54,6 @@ module RedisBuddy
58
54
  @client.type(key)
59
55
  end
60
56
 
61
- def exists?(key)
62
- @client.exists(key)
63
- end
64
-
65
- def delete(key)
66
- @client.del(key)
67
- end
68
-
69
57
  def clear
70
58
  @client.flushdb
71
59
  end
@@ -74,24 +62,49 @@ module RedisBuddy
74
62
  @client.info
75
63
  end
76
64
 
77
- def keys(pattern = "*")
78
- @client.keys(pattern)
65
+ def keys(matcher = "*", options = nil)
66
+ options = merged_options(options)
67
+ @client.keys namespaced_key(matcher, options)
79
68
  end
80
69
 
81
70
  def client
82
71
  @client
83
72
  end
84
73
 
74
+ protected
75
+ def write_entry(key, entry, options = {})
76
+ options[:expires_in] = options.delete(:ttl) if options[:ttl]
77
+ options[:unless_exist] = options.delete(:nx) if options[:nx]
78
+ options[:getset] = options.delete(:return) if options[:return]
79
+
80
+ method = :getset if options[:getset]
81
+ method = :setnx if options[:unless_exist]
82
+ method, expires_in = :setex, options[:expires_in] if options[:expires_in]
83
+ method = :set unless method
84
+ entry = encode(entry)
85
+
86
+ expires_in ? @client.send(method, key, expires_in, entry) : @client.send(method, key, entry)
87
+ end
88
+
89
+ def read_entry(key, options)
90
+ entry = @client.get(key)
91
+ entry ? decode(entry) : nil
92
+ end
93
+
94
+ def delete_entry(key, options)
95
+ @client.del(key)
96
+ end
97
+
85
98
  private
86
99
  def decode(value)
87
- value = Yajl.load(value)
100
+ value = Marshal.load(value)
88
101
  value.with_indifferent_access if value.is_a?(Hash)
89
102
  value
90
103
  end
91
104
 
92
105
  def encode(value)
93
106
  return value unless encode?(value)
94
- Yajl.dump(value)
107
+ Marshal.dump(value)
95
108
  end
96
109
 
97
110
  def encode?(value)
@@ -1,3 +1,3 @@
1
1
  module RedisBuddy
2
- VERSION = "0.4.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_buddy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 4
9
9
  - 0
10
- version: 0.4.0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ole Riesenberg
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-16 00:00:00 +01:00
18
+ date: 2011-03-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21