redis-objects 1.4.3 → 1.6.0
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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +9 -4
- data/CHANGELOG.rdoc +37 -0
- data/README.md +40 -9
- data/lib/redis/base_object.rb +6 -0
- data/lib/redis/counter.rb +2 -6
- data/lib/redis/enumerable_object.rb +28 -0
- data/lib/redis/hash_key.rb +10 -38
- data/lib/redis/helpers/core_commands.rb +16 -11
- data/lib/redis/list.rb +2 -19
- data/lib/redis/lock.rb +0 -8
- data/lib/redis/objects/connection_pool_proxy.rb +1 -0
- data/lib/redis/objects/hashes.rb +9 -0
- data/lib/redis/objects/lists.rb +9 -0
- data/lib/redis/objects/sets.rb +9 -0
- data/lib/redis/objects/version.rb +1 -1
- data/lib/redis/objects.rb +9 -4
- data/lib/redis/set.rb +4 -21
- data/lib/redis/sorted_set.rb +6 -13
- data/lib/redis/value.rb +0 -5
- data/redis-objects.gemspec +5 -1
- data/spec/redis_autoload_objects_spec.rb +1 -1
- data/spec/redis_objects_conn_spec.rb +16 -25
- data/spec/redis_objects_custom_serializer.rb +199 -0
- data/spec/redis_objects_instance_spec.rb +151 -38
- data/spec/redis_objects_model_spec.rb +85 -3
- data/spec/spec_helper.rb +15 -2
- metadata +38 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 831f2916f47c47d9e3fcce9f8c585327c3e0e6e56204880ddfc860c279ec0e85
|
|
4
|
+
data.tar.gz: 188263c23463afc2f68b026efb79bac230ed140fba5d164540c99bac9d4553c2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2189c5151d3f59ca67445e4f560e23f68e34c62c6a257b58bbf9eb08775420044b82680c7dc4b808769347542c27fa1f295776bd418e4bf34a1d1e1b78799843
|
|
7
|
+
data.tar.gz: a26ad235bba5d586b8076c5640d4f725806187e41345af154d929b8ba4339932d97c86fb9d4a512777052df67aeca0a3f0fcda7d22e4f55248ff78fd14e3b552
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.rdoc
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
= Changelog for Redis::Objects
|
|
2
2
|
|
|
3
|
+
== 1.5.1 (10 Jul 2021)
|
|
4
|
+
|
|
5
|
+
* Added double-splat for **options to account for Ruby 3.0 [Nate Wiger]
|
|
6
|
+
|
|
7
|
+
* Fix ConnectionPoolProxy Ruby 3.0 compatibility Fix: https://github.com/nateware/redis-objects/pull/258 [Jean byroot Boussier]
|
|
8
|
+
|
|
9
|
+
* Change Redis#exists to Redis#exists? * bump redis version to 4.2 [Alina Hryshchuk]
|
|
10
|
+
|
|
11
|
+
* Local variable `dir` is not in use since 98226b95f35ef455f231692fdb679dfd61200a78 [Akira Matsuda]
|
|
12
|
+
|
|
13
|
+
* Issue 249: when atomic decrbyfloat fails, increment back instead of decrementing again [Slava Samoliuk]
|
|
14
|
+
|
|
15
|
+
* Update documentation to reflect ability to assign values directly [Artin Boghosian]
|
|
16
|
+
|
|
17
|
+
* Allow directly assigning values of lists, hashes and sets [Artin Boghosian]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
== 1.5.0 (18 Sep 2019)
|
|
21
|
+
|
|
22
|
+
* updated README on expireat [Nate Wiger]
|
|
23
|
+
|
|
24
|
+
* Add option for using a custom serializer [Tomás Rojas]
|
|
25
|
+
|
|
26
|
+
* DRY up objects to enable custom prefixing when desired [Tomás Rojas]
|
|
27
|
+
|
|
28
|
+
* Allow spop to return multiple members [Evan Paul]
|
|
29
|
+
|
|
30
|
+
* Rename #delete! to #redis_delete_objects [Mattias Pfeiffer]
|
|
31
|
+
|
|
32
|
+
* Make deletion simpler with just 1 call to Redis [Mattias Pfeiffer]
|
|
33
|
+
|
|
34
|
+
* Move `CoreCommands` inclusion to `BaseObject` [Tomás Rojas]
|
|
35
|
+
|
|
36
|
+
* Move `Enumerable` functionality to `EnumerableObject` [Tomás Rojas]
|
|
37
|
+
|
|
38
|
+
* Move `attr_reader`s to `Redis::BaseObject` [Tomás Rojas]
|
|
39
|
+
|
|
3
40
|
== 1.4.3 (7 Oct 2018)
|
|
4
41
|
|
|
5
42
|
* Merge pull request #235 from johnc219/fix/end-time-expiration Add expiration in seconds to obtain end_time [Nate Wiger]
|
data/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
Redis::Objects - Map Redis types directly to Ruby objects
|
|
2
2
|
=========================================================
|
|
3
3
|
|
|
4
|
-
[](https://travis-ci.com/github/nateware/redis-objects)
|
|
5
|
+
[](https://codecov.io/gh/nateware/redis-objects)
|
|
5
6
|
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MJF7JU5M7F8VL)
|
|
6
7
|
|
|
7
8
|
This is **not** an ORM. People that are wrapping ORM’s around Redis are missing the point.
|
|
@@ -9,7 +10,7 @@ This is **not** an ORM. People that are wrapping ORM’s around Redis are missin
|
|
|
9
10
|
The killer feature of Redis is that it allows you to perform _atomic_ operations
|
|
10
11
|
on _individual_ data structures, like counters, lists, and sets. The **atomic** part is HUGE.
|
|
11
12
|
Using an ORM wrapper that retrieves a "record", updates values, then sends those values back,
|
|
12
|
-
_removes_ the atomicity,
|
|
13
|
+
_removes_ the atomicity, and thus the major advantage of Redis. Just use MySQL, k?
|
|
13
14
|
|
|
14
15
|
This gem provides a Rubyish interface to Redis, by mapping [Redis data types](http://redis.io/commands)
|
|
15
16
|
to Ruby objects, via a thin layer over the `redis` gem. It offers several advantages
|
|
@@ -144,6 +145,7 @@ Familiar Ruby array operations Just Work (TM):
|
|
|
144
145
|
@team.on_base.shift
|
|
145
146
|
@team.on_base.length # 1
|
|
146
147
|
@team.on_base.delete('player2')
|
|
148
|
+
@team.on_base = ['player1', 'player2'] # ['player1', 'player2']
|
|
147
149
|
~~~
|
|
148
150
|
|
|
149
151
|
Sets work too:
|
|
@@ -157,6 +159,15 @@ Sets work too:
|
|
|
157
159
|
puts player
|
|
158
160
|
end
|
|
159
161
|
player = @team.outfielders.detect{|of| of == 'outfielder2'}
|
|
162
|
+
@team.outfielders = ['outfielder1', 'outfielder3'] # ['outfielder1', 'outfielder3']
|
|
163
|
+
~~~
|
|
164
|
+
|
|
165
|
+
Hashes work too:
|
|
166
|
+
|
|
167
|
+
~~~ruby
|
|
168
|
+
@team.pitchers_faced['player1'] = 'pitcher2'
|
|
169
|
+
@team.pitchers_faced['player2'] = 'pitcher1'
|
|
170
|
+
@team.pitchers_faced = { 'player1' => 'pitcher2', 'player2' => 'pitcher1' }
|
|
160
171
|
~~~
|
|
161
172
|
|
|
162
173
|
And you can do unions and intersections between objects (kinda cool):
|
|
@@ -550,20 +561,40 @@ Use :expiration and :expireat options to set default expiration.
|
|
|
550
561
|
|
|
551
562
|
~~~ruby
|
|
552
563
|
value :value_with_expiration, :expiration => 1.hour
|
|
553
|
-
value :value_with_expireat, :expireat => Time.now + 1.hour
|
|
564
|
+
value :value_with_expireat, :expireat => lambda { Time.now + 1.hour }
|
|
554
565
|
~~~
|
|
555
566
|
|
|
556
|
-
:warning: `
|
|
557
|
-
In this example, it will be one hour after
|
|
567
|
+
:warning: In the above example, `expiration` is evaluated at class load time.
|
|
568
|
+
In this example, it will be one hour after loading the class, not after one hour
|
|
569
|
+
after setting a value. If you want to expire one hour after setting the value,
|
|
570
|
+
please use `:expireat` with `lambda`.
|
|
571
|
+
|
|
572
|
+
Custom serialization
|
|
573
|
+
--------------------
|
|
574
|
+
You can customize how values are serialized by setting `serializer: CustomSerializer`.
|
|
575
|
+
The default is `Marshal` from the standard lib, but it can be anything that responds to `dump` and
|
|
576
|
+
`load`. `JSON` and `YAML` are popular options.
|
|
558
577
|
|
|
559
|
-
If you
|
|
578
|
+
If you need to pass extra arguments to `dump` or `load`, you can set
|
|
579
|
+
`marshal_dump_args: { foo: 'bar' }` and `marshal_load_args: { foo: 'bar' }` respectively.
|
|
560
580
|
|
|
561
581
|
~~~ruby
|
|
562
|
-
|
|
582
|
+
class CustomSerializer
|
|
583
|
+
def self.dump(value)
|
|
584
|
+
# custom code for serializing
|
|
585
|
+
end
|
|
586
|
+
|
|
587
|
+
def self.load(value)
|
|
588
|
+
# custom code for deserializing
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
@account = Account.create!(params[:account])
|
|
593
|
+
@newest = Redis::Value.new('custom_serializer', marshal: true, serializer: CustomSerializer)
|
|
594
|
+
@newest.value = @account.attributes
|
|
563
595
|
~~~
|
|
564
596
|
|
|
565
597
|
Author
|
|
566
598
|
=======
|
|
567
|
-
Copyright (c) 2009-
|
|
599
|
+
Copyright (c) 2009-2019 [Nate Wiger](http://nateware.com). All Rights Reserved.
|
|
568
600
|
Released under the [Artistic License](http://www.opensource.org/licenses/artistic-license-2.0.php).
|
|
569
|
-
|
data/lib/redis/base_object.rb
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
require 'redis/helpers/core_commands'
|
|
2
|
+
|
|
1
3
|
class Redis
|
|
2
4
|
# Defines base functionality for all redis-objects.
|
|
3
5
|
class BaseObject
|
|
6
|
+
include Redis::Helpers::CoreCommands
|
|
7
|
+
|
|
8
|
+
attr_reader :key, :options
|
|
9
|
+
|
|
4
10
|
def initialize(key, *args)
|
|
5
11
|
@key = key.is_a?(Array) ? key.flatten.join(':') : key
|
|
6
12
|
@options = args.last.is_a?(Hash) ? args.pop : {}
|
data/lib/redis/counter.rb
CHANGED
|
@@ -9,10 +9,6 @@ class Redis
|
|
|
9
9
|
# class to define a counter.
|
|
10
10
|
#
|
|
11
11
|
class Counter < BaseObject
|
|
12
|
-
require 'redis/helpers/core_commands'
|
|
13
|
-
include Redis::Helpers::CoreCommands
|
|
14
|
-
|
|
15
|
-
attr_reader :key, :options
|
|
16
12
|
def initialize(key, *args)
|
|
17
13
|
super(key, *args)
|
|
18
14
|
@options[:start] ||= @options[:default] || 0
|
|
@@ -106,7 +102,7 @@ class Redis
|
|
|
106
102
|
def decrbyfloat(by=1.0, &block)
|
|
107
103
|
allow_expiration do
|
|
108
104
|
val = redis.incrbyfloat(key, -by).to_f
|
|
109
|
-
block_given? ? rewindable_block(:incrbyfloat,
|
|
105
|
+
block_given? ? rewindable_block(:incrbyfloat, by, val, &block) : val
|
|
110
106
|
end
|
|
111
107
|
end
|
|
112
108
|
|
|
@@ -116,7 +112,7 @@ class Redis
|
|
|
116
112
|
alias_method :to_i, :value
|
|
117
113
|
|
|
118
114
|
def nil?
|
|
119
|
-
!redis.exists(key)
|
|
115
|
+
!redis.exists?(key)
|
|
120
116
|
end
|
|
121
117
|
|
|
122
118
|
##
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/base_object'
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
#
|
|
5
|
+
# Class representing a Redis enumerable type (list, set, sorted set, or hash).
|
|
6
|
+
#
|
|
7
|
+
class EnumerableObject < BaseObject
|
|
8
|
+
include Enumerable
|
|
9
|
+
|
|
10
|
+
# Iterate through each member. Redis::Objects mixes in Enumerable,
|
|
11
|
+
# so you can also use familiar methods like +collect+, +detect+, and so forth.
|
|
12
|
+
def each(&block)
|
|
13
|
+
value.each(&block)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def sort(options={})
|
|
17
|
+
return super() if block_given?
|
|
18
|
+
options[:order] = "asc alpha" if options.keys.count == 0 # compat with Ruby
|
|
19
|
+
val = redis.sort(key, **options)
|
|
20
|
+
val.is_a?(Array) ? val.map{|v| unmarshal(v)} : val
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# ActiveSupport's core extension `Enumerable#as_json` implementation is incompatible with ours.
|
|
24
|
+
def as_json(*)
|
|
25
|
+
to_hash
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/redis/hash_key.rb
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
require File.dirname(__FILE__) + '/
|
|
1
|
+
require File.dirname(__FILE__) + '/enumerable_object'
|
|
2
2
|
|
|
3
3
|
class Redis
|
|
4
4
|
#
|
|
5
5
|
# Class representing a Redis hash.
|
|
6
6
|
#
|
|
7
|
-
class HashKey <
|
|
8
|
-
require 'enumerator'
|
|
9
|
-
include Enumerable
|
|
10
|
-
require 'redis/helpers/core_commands'
|
|
11
|
-
include Redis::Helpers::CoreCommands
|
|
12
|
-
|
|
13
|
-
attr_reader :key, :options
|
|
7
|
+
class HashKey < EnumerableObject
|
|
14
8
|
def initialize(key, *args)
|
|
15
9
|
super
|
|
16
10
|
@options[:marshal_keys] ||= {}
|
|
@@ -74,11 +68,6 @@ class Redis
|
|
|
74
68
|
alias_method :clone, :all
|
|
75
69
|
alias_method :value, :all
|
|
76
70
|
|
|
77
|
-
# Enumerate through all fields. Redis: HGETALL
|
|
78
|
-
def each(&block)
|
|
79
|
-
all.each(&block)
|
|
80
|
-
end
|
|
81
|
-
|
|
82
71
|
# Enumerate through each keys. Redis: HKEYS
|
|
83
72
|
def each_key(&block)
|
|
84
73
|
keys.each(&block)
|
|
@@ -101,11 +90,6 @@ class Redis
|
|
|
101
90
|
true if size == 0
|
|
102
91
|
end
|
|
103
92
|
|
|
104
|
-
# Clears the dict of all keys/values. Redis: DEL
|
|
105
|
-
def clear
|
|
106
|
-
redis.del(key)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
93
|
# Set keys in bulk, takes a hash of field/values {'field1' => 'val1'}. Redis: HMSET
|
|
110
94
|
def bulk_set(*args)
|
|
111
95
|
raise ArgumentError, "Argument to bulk_set must be hash of key/value pairs" unless args.last.is_a?(::Hash)
|
|
@@ -139,24 +123,20 @@ class Redis
|
|
|
139
123
|
hsh
|
|
140
124
|
end
|
|
141
125
|
|
|
142
|
-
# Get values in bulk, takes an array of
|
|
126
|
+
# Get values in bulk, takes an array of fields as arguments.
|
|
143
127
|
# Values are returned in a collection in the same order than their keys in *keys Redis: HMGET
|
|
144
|
-
def bulk_values(*
|
|
145
|
-
|
|
146
|
-
return [] if
|
|
147
|
-
res = redis.hmget(key,
|
|
148
|
-
|
|
128
|
+
def bulk_values(*fields)
|
|
129
|
+
get_fields = *fields.flatten
|
|
130
|
+
return [] if get_fields.empty?
|
|
131
|
+
res = redis.hmget(key, get_fields)
|
|
132
|
+
get_fields.collect{|k| unmarshal(res.shift, options[:marshal_keys][k])}
|
|
149
133
|
end
|
|
150
134
|
|
|
151
135
|
# Increment value by integer at field. Redis: HINCRBY
|
|
152
136
|
def incrby(field, by=1)
|
|
153
137
|
allow_expiration do
|
|
154
138
|
ret = redis.hincrby(key, field, by)
|
|
155
|
-
|
|
156
|
-
ret.to_i
|
|
157
|
-
else
|
|
158
|
-
nil
|
|
159
|
-
end
|
|
139
|
+
ret.to_i
|
|
160
140
|
end
|
|
161
141
|
end
|
|
162
142
|
alias_method :incr, :incrby
|
|
@@ -171,11 +151,7 @@ class Redis
|
|
|
171
151
|
def incrbyfloat(field, by=1.0)
|
|
172
152
|
allow_expiration do
|
|
173
153
|
ret = redis.hincrbyfloat(key, field, by)
|
|
174
|
-
|
|
175
|
-
ret.to_f
|
|
176
|
-
else
|
|
177
|
-
nil
|
|
178
|
-
end
|
|
154
|
+
ret.to_f
|
|
179
155
|
end
|
|
180
156
|
end
|
|
181
157
|
|
|
@@ -183,9 +159,5 @@ class Redis
|
|
|
183
159
|
def decrbyfloat(field, by=1.0)
|
|
184
160
|
incrbyfloat(field, -by)
|
|
185
161
|
end
|
|
186
|
-
|
|
187
|
-
def as_json(*)
|
|
188
|
-
to_hash
|
|
189
|
-
end
|
|
190
162
|
end
|
|
191
163
|
end
|
|
@@ -2,16 +2,21 @@ class Redis
|
|
|
2
2
|
module Helpers
|
|
3
3
|
# These are core commands that all types share (rename, etc)
|
|
4
4
|
module CoreCommands
|
|
5
|
-
def exists
|
|
5
|
+
def exists
|
|
6
6
|
redis.exists key
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
def exists?
|
|
10
|
+
redis.exists? key
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Delete key. Redis: DEL
|
|
9
14
|
def delete
|
|
10
15
|
redis.del key
|
|
11
16
|
end
|
|
12
17
|
alias_method :del, :delete
|
|
13
18
|
alias_method :clear, :delete
|
|
14
|
-
|
|
19
|
+
|
|
15
20
|
def type
|
|
16
21
|
redis.type key
|
|
17
22
|
end
|
|
@@ -29,7 +34,7 @@ class Redis
|
|
|
29
34
|
@key = dest if ret && setkey
|
|
30
35
|
ret
|
|
31
36
|
end
|
|
32
|
-
|
|
37
|
+
|
|
33
38
|
def expire(seconds)
|
|
34
39
|
redis.expire key, seconds
|
|
35
40
|
end
|
|
@@ -50,20 +55,19 @@ class Redis
|
|
|
50
55
|
redis.move key, dbindex
|
|
51
56
|
end
|
|
52
57
|
|
|
53
|
-
def
|
|
54
|
-
options[:
|
|
55
|
-
val = redis.sort(key, options)
|
|
56
|
-
val.is_a?(Array) ? val.map{|v| unmarshal(v)} : val
|
|
58
|
+
def serializer
|
|
59
|
+
options[:serializer] || Marshal
|
|
57
60
|
end
|
|
58
61
|
|
|
59
62
|
def marshal(value, domarshal=false)
|
|
60
63
|
if options[:marshal] || domarshal
|
|
61
|
-
|
|
64
|
+
dump_args = options[:marshal_dump_args] || []
|
|
65
|
+
serializer.dump(value, *(dump_args.is_a?(Array) ? dump_args : [dump_args]))
|
|
62
66
|
else
|
|
63
67
|
value
|
|
64
68
|
end
|
|
65
69
|
end
|
|
66
|
-
|
|
70
|
+
|
|
67
71
|
def unmarshal(value, domarshal=false)
|
|
68
72
|
if value.nil?
|
|
69
73
|
nil
|
|
@@ -73,7 +77,8 @@ class Redis
|
|
|
73
77
|
elsif !value.is_a?(String)
|
|
74
78
|
value
|
|
75
79
|
else
|
|
76
|
-
|
|
80
|
+
load_args = options[:marshal_load_args] || []
|
|
81
|
+
serializer.load(value, *(load_args.is_a?(Array) ? load_args : [load_args]))
|
|
77
82
|
end
|
|
78
83
|
else
|
|
79
84
|
value
|
data/lib/redis/list.rb
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
|
-
require File.dirname(__FILE__) + '/
|
|
1
|
+
require File.dirname(__FILE__) + '/enumerable_object'
|
|
2
2
|
|
|
3
3
|
class Redis
|
|
4
4
|
#
|
|
5
5
|
# Class representing a Redis list. Instances of Redis::List are designed to
|
|
6
6
|
# behave as much like Ruby arrays as possible.
|
|
7
7
|
#
|
|
8
|
-
class List <
|
|
9
|
-
require 'enumerator'
|
|
10
|
-
include Enumerable
|
|
11
|
-
require 'redis/helpers/core_commands'
|
|
12
|
-
include Redis::Helpers::CoreCommands
|
|
13
|
-
|
|
14
|
-
attr_reader :key, :options
|
|
15
|
-
|
|
8
|
+
class List < EnumerableObject
|
|
16
9
|
# Works like push. Can chain together: list << 'a' << 'b'
|
|
17
10
|
def <<(value)
|
|
18
11
|
push(value) # marshal in push()
|
|
@@ -123,12 +116,6 @@ class Redis
|
|
|
123
116
|
redis.lrem(key, count, marshal(name)) # weird api
|
|
124
117
|
end
|
|
125
118
|
|
|
126
|
-
# Iterate through each member of the set. Redis::Objects mixes in Enumerable,
|
|
127
|
-
# so you can also use familiar methods like +collect+, +detect+, and so forth.
|
|
128
|
-
def each(&block)
|
|
129
|
-
values.each(&block)
|
|
130
|
-
end
|
|
131
|
-
|
|
132
119
|
# Return a range of values from +start_index+ to +end_index+. Can also use
|
|
133
120
|
# the familiar list[start,end] Ruby syntax. Redis: LRANGE
|
|
134
121
|
def range(start_index, end_index)
|
|
@@ -169,9 +156,5 @@ class Redis
|
|
|
169
156
|
def to_s
|
|
170
157
|
values.join(', ')
|
|
171
158
|
end
|
|
172
|
-
|
|
173
|
-
def as_json(*)
|
|
174
|
-
to_hash
|
|
175
|
-
end
|
|
176
159
|
end
|
|
177
160
|
end
|
data/lib/redis/lock.rb
CHANGED
|
@@ -11,7 +11,6 @@ class Redis
|
|
|
11
11
|
class Lock < BaseObject
|
|
12
12
|
class LockTimeout < StandardError; end #:nodoc:
|
|
13
13
|
|
|
14
|
-
attr_reader :key, :options
|
|
15
14
|
def initialize(key, *args)
|
|
16
15
|
super(key, *args)
|
|
17
16
|
@options[:timeout] ||= 5
|
|
@@ -19,13 +18,6 @@ class Redis
|
|
|
19
18
|
redis.setnx(key, @options[:start]) unless @options[:start] == 0 || @options[:init] === false
|
|
20
19
|
end
|
|
21
20
|
|
|
22
|
-
# Clear the lock. Should only be needed if there's a server crash
|
|
23
|
-
# or some other event that gets locks in a stuck state.
|
|
24
|
-
def clear
|
|
25
|
-
redis.del(key)
|
|
26
|
-
end
|
|
27
|
-
alias_method :delete, :clear
|
|
28
|
-
|
|
29
21
|
def value
|
|
30
22
|
nil
|
|
31
23
|
end
|
|
@@ -9,6 +9,7 @@ class Redis
|
|
|
9
9
|
def method_missing(name, *args, &block)
|
|
10
10
|
@pool.with { |x| x.send(name, *args, &block) }
|
|
11
11
|
end
|
|
12
|
+
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
|
|
12
13
|
|
|
13
14
|
def respond_to_missing?(name, include_all = false)
|
|
14
15
|
@pool.with { |x| x.respond_to?(name, include_all) }
|
data/lib/redis/objects/hashes.rb
CHANGED
data/lib/redis/objects/lists.rb
CHANGED
data/lib/redis/objects/sets.rb
CHANGED
data/lib/redis/objects.rb
CHANGED
|
@@ -46,8 +46,6 @@ class Redis
|
|
|
46
46
|
#
|
|
47
47
|
#
|
|
48
48
|
module Objects
|
|
49
|
-
dir = File.expand_path(__FILE__.sub(/\.rb$/,''))
|
|
50
|
-
|
|
51
49
|
autoload :Counters, 'redis/objects/counters'
|
|
52
50
|
autoload :Lists, 'redis/objects/lists'
|
|
53
51
|
autoload :Locks, 'redis/objects/locks'
|
|
@@ -173,8 +171,15 @@ class Redis
|
|
|
173
171
|
def redis() self.class.redis end
|
|
174
172
|
def redis_objects() self.class.redis_objects end
|
|
175
173
|
|
|
176
|
-
def
|
|
177
|
-
redis.del(
|
|
174
|
+
def redis_delete_objects
|
|
175
|
+
redis.del(redis_instance_keys)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def redis_instance_keys
|
|
179
|
+
redis_objects
|
|
180
|
+
.reject { |_, value| value[:global] }
|
|
181
|
+
.keys
|
|
182
|
+
.collect { |name| redis_field_key(name) }
|
|
178
183
|
end
|
|
179
184
|
|
|
180
185
|
def redis_options(name) #:nodoc:
|
data/lib/redis/set.rb
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
require File.dirname(__FILE__) + '/
|
|
1
|
+
require File.dirname(__FILE__) + '/enumerable_object'
|
|
2
2
|
|
|
3
3
|
class Redis
|
|
4
4
|
#
|
|
5
5
|
# Class representing a set.
|
|
6
6
|
#
|
|
7
|
-
class Set <
|
|
8
|
-
require 'enumerator'
|
|
9
|
-
include Enumerable
|
|
10
|
-
require 'redis/helpers/core_commands'
|
|
11
|
-
include Redis::Helpers::CoreCommands
|
|
12
|
-
|
|
13
|
-
attr_reader :key, :options
|
|
14
|
-
|
|
7
|
+
class Set < EnumerableObject
|
|
15
8
|
# Works like add. Can chain together: list << 'a' << 'b'
|
|
16
9
|
def <<(value)
|
|
17
10
|
add(value)
|
|
@@ -27,8 +20,8 @@ class Redis
|
|
|
27
20
|
end
|
|
28
21
|
|
|
29
22
|
# Remove and return a random member. Redis: SPOP
|
|
30
|
-
def pop
|
|
31
|
-
unmarshal redis.spop(key)
|
|
23
|
+
def pop(count = nil)
|
|
24
|
+
unmarshal redis.spop(key, count)
|
|
32
25
|
end
|
|
33
26
|
|
|
34
27
|
# return a random member. Redis: SRANDMEMBER
|
|
@@ -74,12 +67,6 @@ class Redis
|
|
|
74
67
|
res
|
|
75
68
|
end
|
|
76
69
|
|
|
77
|
-
# Iterate through each member of the set. Redis::Objects mixes in Enumerable,
|
|
78
|
-
# so you can also use familiar methods like +collect+, +detect+, and so forth.
|
|
79
|
-
def each(&block)
|
|
80
|
-
members.each(&block)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
70
|
# Return the intersection with another set. Can pass it either another set
|
|
84
71
|
# object or set name. Also available as & which is a bit cleaner:
|
|
85
72
|
#
|
|
@@ -185,10 +172,6 @@ class Redis
|
|
|
185
172
|
members.join(', ')
|
|
186
173
|
end
|
|
187
174
|
|
|
188
|
-
def as_json(*)
|
|
189
|
-
to_hash
|
|
190
|
-
end
|
|
191
|
-
|
|
192
175
|
private
|
|
193
176
|
|
|
194
177
|
def keys_from_objects(sets)
|
data/lib/redis/sorted_set.rb
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
require File.dirname(__FILE__) + '/
|
|
1
|
+
require File.dirname(__FILE__) + '/enumerable_object'
|
|
2
2
|
|
|
3
3
|
class Redis
|
|
4
4
|
#
|
|
5
5
|
# Class representing a sorted set.
|
|
6
6
|
#
|
|
7
|
-
class SortedSet <
|
|
8
|
-
# require 'enumerator'
|
|
9
|
-
# include Enumerable
|
|
10
|
-
require 'redis/helpers/core_commands'
|
|
11
|
-
include Redis::Helpers::CoreCommands
|
|
12
|
-
|
|
13
|
-
attr_reader :key, :options
|
|
14
|
-
|
|
7
|
+
class SortedSet < EnumerableObject
|
|
15
8
|
# How to add values using a sorted set. The key is the member, eg,
|
|
16
9
|
# "Peter", and the value is the score, eg, 163. So:
|
|
17
10
|
# num_posts['Peter'] = 163
|
|
@@ -124,7 +117,7 @@ class Redis
|
|
|
124
117
|
options[:offset] || options[:limit] || options[:count]
|
|
125
118
|
args[:with_scores] = true if options[:withscores] || options[:with_scores]
|
|
126
119
|
|
|
127
|
-
redis.zrangebyscore(key, min, max, args).map{|v| unmarshal(v) }
|
|
120
|
+
redis.zrangebyscore(key, min, max, **args).map{|v| unmarshal(v) }
|
|
128
121
|
end
|
|
129
122
|
|
|
130
123
|
# Returns all the elements in the sorted set at key with a score between max and min
|
|
@@ -140,7 +133,7 @@ class Redis
|
|
|
140
133
|
options[:offset] || options[:limit] || options[:count]
|
|
141
134
|
args[:with_scores] = true if options[:withscores] || options[:with_scores]
|
|
142
135
|
|
|
143
|
-
redis.zrevrangebyscore(key, max, min, args).map{|v| unmarshal(v) }
|
|
136
|
+
redis.zrevrangebyscore(key, max, min, **args).map{|v| unmarshal(v) }
|
|
144
137
|
end
|
|
145
138
|
|
|
146
139
|
# Remove all elements in the sorted set at key with rank between start and end. Start and end are
|
|
@@ -229,7 +222,7 @@ class Redis
|
|
|
229
222
|
def interstore(name, *sets)
|
|
230
223
|
allow_expiration do
|
|
231
224
|
opts = sets.last.is_a?(Hash) ? sets.pop : {}
|
|
232
|
-
redis.zinterstore(key_from_object(name), keys_from_objects([self] + sets), opts)
|
|
225
|
+
redis.zinterstore(key_from_object(name), keys_from_objects([self] + sets), **opts)
|
|
233
226
|
end
|
|
234
227
|
end
|
|
235
228
|
|
|
@@ -265,7 +258,7 @@ class Redis
|
|
|
265
258
|
def unionstore(name, *sets)
|
|
266
259
|
allow_expiration do
|
|
267
260
|
opts = sets.last.is_a?(Hash) ? sets.pop : {}
|
|
268
|
-
redis.zunionstore(key_from_object(name), keys_from_objects([self] + sets), opts)
|
|
261
|
+
redis.zunionstore(key_from_object(name), keys_from_objects([self] + sets), **opts)
|
|
269
262
|
end
|
|
270
263
|
end
|
|
271
264
|
|