redis-objects 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +6 -0
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/redis/counter.rb +8 -0
- data/lib/redis/helpers/core_commands.rb +5 -1
- data/lib/redis/list.rb +2 -1
- data/lib/redis/objects/counters.rb +7 -0
- data/lib/redis/sorted_set.rb +7 -2
- data/redis-objects.gemspec +11 -8
- data/spec/redis_objects_active_record_spec.rb +4 -0
- data/spec/redis_objects_instance_spec.rb +3 -0
- data/spec/redis_objects_model_spec.rb +4 -0
- data/spec/spec_helper.rb +20 -4
- metadata +16 -5
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
= Changelog for Redis::Objects
|
2
2
|
|
3
|
+
== 0.5.2 [Development]
|
4
|
+
|
5
|
+
* Added Redis::SortedSet#member? method [Karl Varga]
|
6
|
+
|
7
|
+
* Added +ttl+ method to CoreCommands [Karl Varga]
|
8
|
+
|
3
9
|
== 0.5.1 [Final] (23 May 2011)
|
4
10
|
|
5
11
|
* Fixed super class delegation conflicts with Redis Counters vs ActiveRecord [Tim Aßmann]
|
data/Rakefile
CHANGED
@@ -11,6 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/nateware/redis-objects"
|
12
12
|
gem.authors = ["Nate Wiger"]
|
13
13
|
gem.add_development_dependency "bacon", ">= 0"
|
14
|
+
gem.add_development_dependency "redis-namespace", ">= 0"
|
14
15
|
gem.requirements << 'redis, v2.1.1 or greater'
|
15
16
|
gem.add_dependency('redis', '>= 2.1.1') # ALSO: update spec/spec_helper.rb
|
16
17
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.2
|
data/lib/redis/counter.rb
CHANGED
@@ -28,6 +28,14 @@ class Redis
|
|
28
28
|
true # hack for redis-rb regression
|
29
29
|
end
|
30
30
|
|
31
|
+
# Reset the counter to its starting value, and return previous value.
|
32
|
+
# Use this to "reap" the counter and save it somewhere else. This is
|
33
|
+
# atomic in that no increments or decrements are lost if you process
|
34
|
+
# the returned value.
|
35
|
+
def getset(to=options[:start])
|
36
|
+
redis.getset(key, to.to_i).to_i
|
37
|
+
end
|
38
|
+
|
31
39
|
# Returns the current value of the counter. Normally just calling the
|
32
40
|
# counter will lazily fetch the value, and only update it if increment
|
33
41
|
# or decrement is called. This forces a network call to redis-server
|
data/lib/redis/list.rb
CHANGED
@@ -55,7 +55,8 @@ class Redis
|
|
55
55
|
# a range of values using Redis: LRANGE.
|
56
56
|
def [](index, length=nil)
|
57
57
|
if index.is_a? Range
|
58
|
-
|
58
|
+
last = index.exclude_end? ? (index.last - 1) : index.last
|
59
|
+
range(index.first, last)
|
59
60
|
elsif length
|
60
61
|
case length <=> 0
|
61
62
|
when 1 then range(index, index + length - 1)
|
@@ -79,6 +79,13 @@ class Redis
|
|
79
79
|
redis.set(redis_field_key(name, id), to.to_i)
|
80
80
|
true
|
81
81
|
end
|
82
|
+
|
83
|
+
# Set a counter to its starting value and return the old value.
|
84
|
+
def getset_counter(name, id=nil, to=nil)
|
85
|
+
verify_counter_defined!(name, id)
|
86
|
+
to = @redis_objects[name][:start] if to.nil?
|
87
|
+
redis.getset(redis_field_key(name, id), to.to_i).to_i
|
88
|
+
end
|
82
89
|
|
83
90
|
private
|
84
91
|
|
data/lib/redis/sorted_set.rb
CHANGED
@@ -168,8 +168,8 @@ class Redis
|
|
168
168
|
alias_method :incrby, :increment
|
169
169
|
|
170
170
|
# Convenience to calling increment() with a negative number.
|
171
|
-
def decrement(by=1)
|
172
|
-
redis.zincrby(key, -by).to_i
|
171
|
+
def decrement(member, by=1)
|
172
|
+
redis.zincrby(key, -by, member).to_i
|
173
173
|
end
|
174
174
|
alias_method :decr, :decrement
|
175
175
|
alias_method :decrby, :decrement
|
@@ -286,6 +286,11 @@ class Redis
|
|
286
286
|
redis.zcount(key, min, max)
|
287
287
|
end
|
288
288
|
|
289
|
+
# Return a boolean indicating whether +value+ is a member.
|
290
|
+
def member?(value)
|
291
|
+
!redis.zscore(key, to_redis(value)).nil?
|
292
|
+
end
|
293
|
+
|
289
294
|
private
|
290
295
|
|
291
296
|
def keys_from_objects(sets)
|
data/redis-objects.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.5.
|
7
|
+
s.name = "redis-objects"
|
8
|
+
s.version = "0.5.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Nate Wiger"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2011-09-20"
|
13
|
+
s.description = "Map Redis types directly to Ruby objects. Works with any class or ORM."
|
14
|
+
s.email = "nate@wiger.org"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"README.rdoc"
|
17
17
|
]
|
@@ -46,24 +46,27 @@ Gem::Specification.new do |s|
|
|
46
46
|
"spec/redis_objects_model_spec.rb",
|
47
47
|
"spec/spec_helper.rb"
|
48
48
|
]
|
49
|
-
s.homepage =
|
49
|
+
s.homepage = "http://github.com/nateware/redis-objects"
|
50
50
|
s.require_paths = ["lib"]
|
51
51
|
s.requirements = ["redis, v2.1.1 or greater"]
|
52
|
-
s.rubygems_version =
|
53
|
-
s.summary =
|
52
|
+
s.rubygems_version = "1.8.10"
|
53
|
+
s.summary = "Map Redis types directly to Ruby objects"
|
54
54
|
|
55
55
|
if s.respond_to? :specification_version then
|
56
56
|
s.specification_version = 3
|
57
57
|
|
58
58
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
59
59
|
s.add_development_dependency(%q<bacon>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<redis-namespace>, [">= 0"])
|
60
61
|
s.add_runtime_dependency(%q<redis>, [">= 2.1.1"])
|
61
62
|
else
|
62
63
|
s.add_dependency(%q<bacon>, [">= 0"])
|
64
|
+
s.add_dependency(%q<redis-namespace>, [">= 0"])
|
63
65
|
s.add_dependency(%q<redis>, [">= 2.1.1"])
|
64
66
|
end
|
65
67
|
else
|
66
68
|
s.add_dependency(%q<bacon>, [">= 0"])
|
69
|
+
s.add_dependency(%q<redis-namespace>, [">= 0"])
|
67
70
|
s.add_dependency(%q<redis>, [">= 2.1.1"])
|
68
71
|
end
|
69
72
|
end
|
@@ -147,6 +147,7 @@ describe Redis::List do
|
|
147
147
|
# Test against similar Ruby functionality
|
148
148
|
a = @list.values
|
149
149
|
@list[0..2].should == a[0..2]
|
150
|
+
@list[0...2].should == a[0...2]
|
150
151
|
@list.slice(0..2).should == a.slice(0..2)
|
151
152
|
@list[0, 2].should == a[0, 2]
|
152
153
|
@list.range(0, 2).should == a[0..2] # range for Redis works like .. in Ruby
|
@@ -266,6 +267,8 @@ describe Redis::Counter do
|
|
266
267
|
@counter.should == 0
|
267
268
|
@counter.reset(15).should.be.true
|
268
269
|
@counter.should == 15
|
270
|
+
@counter.getset(111).should == 15
|
271
|
+
@counter.should == 111
|
269
272
|
end
|
270
273
|
|
271
274
|
after do
|
@@ -193,6 +193,8 @@ describe Redis::Objects do
|
|
193
193
|
Roster.decrement_counter(:available_slots, @roster.id).should == 11
|
194
194
|
Roster.reset_counter(:available_slots, @roster.id).should == true
|
195
195
|
Roster.get_counter(:available_slots, @roster.id).should == 10
|
196
|
+
Roster.getset_counter(:available_slots, @roster.id, 555).should == 10
|
197
|
+
Roster.get_counter(:available_slots, @roster.id).should == 555
|
196
198
|
end
|
197
199
|
|
198
200
|
it "should support class-level increment/decrement of global counters" do
|
@@ -211,6 +213,8 @@ describe Redis::Objects do
|
|
211
213
|
Roster.decrement_counter(:total_players_online).should == 1
|
212
214
|
Roster.reset_counter(:total_players_online).should == true
|
213
215
|
Roster.get_counter(:total_players_online).should == 0
|
216
|
+
Roster.getset_counter(:total_players_online, nil, 111).should == 0
|
217
|
+
Roster.get_counter(:total_players_online).should == 111
|
214
218
|
end
|
215
219
|
|
216
220
|
it "should take an atomic block for increment/decrement" do
|
data/spec/spec_helper.rb
CHANGED
@@ -6,17 +6,33 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
6
6
|
require 'bacon'
|
7
7
|
Bacon.summary_at_exit
|
8
8
|
|
9
|
-
$redis = Redis.new(:host => ENV['REDIS_HOST'], :port => ENV['REDIS_PORT'])
|
10
|
-
|
11
9
|
UNIONSTORE_KEY = 'test:unionstore'
|
12
10
|
INTERSTORE_KEY = 'test:interstore'
|
13
11
|
DIFFSTORE_KEY = 'test:diffstore'
|
14
12
|
|
13
|
+
# Start our own redis-server to avoid corrupting any others
|
14
|
+
REDIS_BIN = 'redis-server'
|
15
|
+
REDIS_PORT = ENV['REDIS_PORT'] || 9212
|
16
|
+
REDIS_HOST = ENV['REDIS_HOST'] || 'localhost'
|
17
|
+
REDIS_PID = File.expand_path 'redis.pid', File.dirname(__FILE__)
|
18
|
+
REDIS_DUMP = File.expand_path 'redis.rdb', File.dirname(__FILE__)
|
19
|
+
puts "=> Starting redis-server on #{REDIS_HOST}:#{REDIS_PORT}"
|
20
|
+
fork_pid = fork do
|
21
|
+
system "(echo port #{REDIS_PORT}; echo logfile /dev/null; echo daemonize yes; echo pidfile #{REDIS_PID}; echo dbfilename #{REDIS_DUMP}) | #{REDIS_BIN} -"
|
22
|
+
end
|
23
|
+
at_exit do
|
24
|
+
pid = File.read(REDIS_PID).to_i
|
25
|
+
puts "=> Killing #{REDIS_BIN} with pid #{pid}"
|
26
|
+
Process.kill "TERM", pid
|
27
|
+
Process.kill "KILL", pid
|
28
|
+
File.unlink REDIS_PID, REDIS_DUMP
|
29
|
+
end
|
30
|
+
|
31
|
+
# Grab a global handle
|
32
|
+
$redis = Redis.new(:host => REDIS_HOST, :port => REDIS_PORT)
|
15
33
|
|
16
34
|
SORT_ORDER = {:order => 'desc alpha'}
|
17
35
|
SORT_LIMIT = {:limit => [2, 2]}
|
18
36
|
SORT_BY = {:by => 'm_*'}
|
19
37
|
SORT_GET = {:get => 'spec/*/sorted'}.merge!(SORT_LIMIT)
|
20
38
|
SORT_STORE = {:store => "spec/aftersort"}.merge!(SORT_GET)
|
21
|
-
|
22
|
-
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: redis-objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.5.
|
5
|
+
version: 0.5.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nate Wiger
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-09-20 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bacon
|
@@ -24,16 +24,27 @@ dependencies:
|
|
24
24
|
type: :development
|
25
25
|
version_requirements: *id001
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
|
-
name: redis
|
27
|
+
name: redis-namespace
|
28
28
|
prerelease: false
|
29
29
|
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: redis
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
30
41
|
none: false
|
31
42
|
requirements:
|
32
43
|
- - ">="
|
33
44
|
- !ruby/object:Gem::Version
|
34
45
|
version: 2.1.1
|
35
46
|
type: :runtime
|
36
|
-
version_requirements: *
|
47
|
+
version_requirements: *id003
|
37
48
|
description: Map Redis types directly to Ruby objects. Works with any class or ORM.
|
38
49
|
email: nate@wiger.org
|
39
50
|
executables: []
|
@@ -95,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
106
|
requirements:
|
96
107
|
- redis, v2.1.1 or greater
|
97
108
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.
|
109
|
+
rubygems_version: 1.8.10
|
99
110
|
signing_key:
|
100
111
|
specification_version: 3
|
101
112
|
summary: Map Redis types directly to Ruby objects
|