easyredis 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/Rakefile +39 -28
- data/easyredis.gemspec +2 -2
- data/lib/easyredis.rb +63 -15
- data/tests/test.rb +2 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -43,7 +43,7 @@ We can now make post objects:
|
|
43
43
|
Posts are automatically given ids that we can then use to retrive them:
|
44
44
|
|
45
45
|
id = p.id
|
46
|
-
p = Post
|
46
|
+
p = Post.find(id)
|
47
47
|
p.title # => "My First Post"
|
48
48
|
|
49
49
|
We also get a created_at field for free that we can sort by.
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('easyredis','0.0.
|
5
|
+
Echoe.new('easyredis','0.0.3') do |p|
|
6
6
|
p.description = "simple framework designed to make using redis as a database simpler"
|
7
7
|
p.url = "https://github.com/alecbenzer/easyredis"
|
8
8
|
p.author = "Alec Benzer"
|
@@ -15,17 +15,17 @@ end
|
|
15
15
|
require 'benchmark'
|
16
16
|
require './tests/test'
|
17
17
|
|
18
|
-
$names = ["Bill","Bob","John","Jack","Alec","Mark","Nick","Evan","Eamon","Joe","Vikram"]
|
19
|
-
|
20
18
|
def rand_name
|
21
|
-
|
19
|
+
length = 2
|
20
|
+
chars = []
|
21
|
+
length.times { chars << (rand(26) + 65).chr }
|
22
|
+
chars.join
|
22
23
|
end
|
23
24
|
|
25
|
+
|
24
26
|
namespace :bm do
|
25
27
|
task :clear do
|
26
|
-
|
27
|
-
|
28
|
-
puts "destroying #{$count} previous entries"
|
28
|
+
puts "destroying #{Man.count} previous entries"
|
29
29
|
Benchmark.bm do |bm|
|
30
30
|
bm.report { Man.destroy_all }
|
31
31
|
end
|
@@ -34,43 +34,54 @@ namespace :bm do
|
|
34
34
|
task :add do
|
35
35
|
count = ENV["count"] ? ENV["count"].to_i : 25000
|
36
36
|
puts "adding #{count} new entries"
|
37
|
-
Benchmark.
|
38
|
-
|
37
|
+
time = Benchmark::Tms.new
|
38
|
+
count.times do
|
39
|
+
name = rand_name
|
40
|
+
age = rand(100)
|
41
|
+
time += Benchmark.measure { m = Man.new ; m.name = name ; m.age = age }
|
39
42
|
end
|
43
|
+
puts time.format
|
40
44
|
end
|
41
45
|
|
42
46
|
task :populate => [:clear, :add]
|
43
47
|
|
44
|
-
task :sort do
|
45
|
-
puts "sorting #{Man.count} entries by name"
|
46
|
-
Benchmark.bm do |bm|
|
47
|
-
#bm.report("ruby:") { Man.all.sort_by { |m| m.name } }
|
48
|
-
#bm.report("redis:") { Man.sort_by(:name) }
|
49
|
-
bm.report { Man.sort_by(:name) }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
48
|
task :search do
|
54
49
|
puts "searching #{Man.count} entries by a particular name"
|
50
|
+
name = Man.rand.name
|
55
51
|
Benchmark.bm do |bm|
|
56
|
-
name = rand_name
|
57
|
-
#bm.report("ruby:") { Man.all.select {|m| m.name == name} }
|
58
|
-
#bm.report("redis:") { Man.search_by(:name,name) }
|
59
52
|
bm.report { Man.search_by(:name,name) }
|
60
53
|
end
|
61
54
|
end
|
62
55
|
|
56
|
+
task :singlesearch do
|
57
|
+
puts "seaching #{Man.count} entries by a particular age"
|
58
|
+
puts "NOTE: this metric is only relevant if Model#search is not detecting single-field searches and using Model#search_by"
|
59
|
+
age = rand(100)
|
60
|
+
t1 = Benchmark.measure { Man.search(:age => age) }
|
61
|
+
t2 = Benchmark.measure { Man.search_by(:age,age) }
|
62
|
+
puts "Model#search:"
|
63
|
+
puts t1.format
|
64
|
+
puts "Model#search_by:"
|
65
|
+
puts t2.format
|
66
|
+
puts "search is #{((t1.real/t2.real) - 1)*100}% slower"
|
67
|
+
end
|
68
|
+
|
69
|
+
task :multisearch do
|
70
|
+
man = Man.rand
|
71
|
+
name = man.name
|
72
|
+
age = man.age
|
73
|
+
count = 0
|
74
|
+
time = Benchmark.measure { count = Man.search(:name => name, :age => age).size }
|
75
|
+
puts "retrived #{count} out of #{Man.count} entries in:"
|
76
|
+
puts time.format
|
77
|
+
end
|
78
|
+
|
63
79
|
task :find do
|
64
|
-
puts "finding one of #{Man.count}
|
80
|
+
puts "finding one of #{Man.count} entries by name"
|
81
|
+
name = Man.rand.name
|
65
82
|
Benchmark.bm do |bm|
|
66
|
-
name = rand_name
|
67
|
-
#bm.report("redis:") { Man.find_by(:name,name) }
|
68
83
|
bm.report { Man.find_by(:name,name) }
|
69
84
|
end
|
70
85
|
end
|
71
86
|
|
72
87
|
end
|
73
|
-
|
74
|
-
task :doc do
|
75
|
-
puts `rdoc lib/ --title "EasyRedis"`
|
76
|
-
end
|
data/easyredis.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{easyredis}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Alec Benzer"]
|
9
|
-
s.date = %q{2011-02-
|
9
|
+
s.date = %q{2011-02-06}
|
10
10
|
s.description = %q{simple framework designed to make using redis as a database simpler}
|
11
11
|
s.email = %q{alecbezer @nospam@ gmail.com}
|
12
12
|
s.extra_rdoc_files = ["README.md", "lib/easyredis.rb"]
|
data/lib/easyredis.rb
CHANGED
@@ -45,6 +45,8 @@ module EasyRedis
|
|
45
45
|
end
|
46
46
|
|
47
47
|
# connect to a redis server
|
48
|
+
#
|
49
|
+
# takes the same options that Redis#new does from the redis gem
|
48
50
|
def self.connect(options = {})
|
49
51
|
@redis = Redis.new(options)
|
50
52
|
end
|
@@ -93,21 +95,28 @@ module EasyRedis
|
|
93
95
|
offset = index
|
94
96
|
self[offset...(offset+limit)]
|
95
97
|
elsif index.is_a? Range
|
96
|
-
|
97
|
-
b = index.end
|
98
|
-
b -= 1 if index.exclude_end?
|
99
|
-
ids = []
|
100
|
-
if @order == :asc
|
101
|
-
ids = EasyRedis.redis.zrange(@klass.sort_prefix(@field),a,b)
|
102
|
-
elsif @order == :desc
|
103
|
-
ids = EasyRedis.redis.zrevrange(@klass.sort_prefix(@field),a,b)
|
104
|
-
end
|
105
|
-
ids.map{|i|@klass.new(i)}
|
98
|
+
access(index)
|
106
99
|
elsif index.is_a? Integer
|
107
100
|
self[index..index].first
|
108
101
|
end
|
109
102
|
end
|
110
103
|
|
104
|
+
# helper method for []
|
105
|
+
#
|
106
|
+
# takes a range and returns corresponding elements
|
107
|
+
def access(range)
|
108
|
+
a = range.begin
|
109
|
+
b = range.end
|
110
|
+
b -= 1 if index.exclude_end?
|
111
|
+
ids = []
|
112
|
+
if @order == :asc
|
113
|
+
ids = EasyRedis.redis.zrange(@klass.sort_prefix(@field),a,b)
|
114
|
+
elsif @order == :desc
|
115
|
+
ids = EasyRedis.redis.zrevrange(@klass.sort_prefix(@field),a,b)
|
116
|
+
end
|
117
|
+
ids.map{|i|@klass.new(i)}
|
118
|
+
end
|
119
|
+
|
111
120
|
# iterate through all members of this sort
|
112
121
|
def each
|
113
122
|
self[0..-1].each { |o| yield o }
|
@@ -130,6 +139,14 @@ module EasyRedis
|
|
130
139
|
end
|
131
140
|
end
|
132
141
|
|
142
|
+
def last(n = nil)
|
143
|
+
if n
|
144
|
+
self[-n..-1]
|
145
|
+
else
|
146
|
+
self[-1]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
133
150
|
def inspect
|
134
151
|
"#<EasyRedis::Sort model=#{@klass.name}, field=#{@field.to_s}, order=#{@order.to_s}>"
|
135
152
|
end
|
@@ -143,6 +160,8 @@ module EasyRedis
|
|
143
160
|
|
144
161
|
# add a field to the model
|
145
162
|
def self.field(name)
|
163
|
+
@@fields ||= []
|
164
|
+
@@fields << name.to_sym
|
146
165
|
name = name.to_s
|
147
166
|
getter = name
|
148
167
|
setter = name + "="
|
@@ -185,6 +204,18 @@ module EasyRedis
|
|
185
204
|
self.sort_by :created_at, options
|
186
205
|
end
|
187
206
|
|
207
|
+
def self.first(n = nil)
|
208
|
+
self.all.first(n)
|
209
|
+
end
|
210
|
+
|
211
|
+
def self.last(n = nil)
|
212
|
+
self.all.last(n)
|
213
|
+
end
|
214
|
+
|
215
|
+
def self.rand
|
216
|
+
self[Kernel.rand(self.count)]
|
217
|
+
end
|
218
|
+
|
188
219
|
# find an instance of this model based on its id
|
189
220
|
def self.find(id)
|
190
221
|
if EasyRedis.redis.zscore(prefix.pluralize,id)
|
@@ -194,9 +225,8 @@ module EasyRedis
|
|
194
225
|
end
|
195
226
|
end
|
196
227
|
|
197
|
-
|
198
|
-
|
199
|
-
find(id)
|
228
|
+
def self.[](index,amt=nil)
|
229
|
+
self.all[index,amt]
|
200
230
|
end
|
201
231
|
|
202
232
|
# get all entries where field matches val
|
@@ -207,12 +237,23 @@ module EasyRedis
|
|
207
237
|
ids = EasyRedis.redis.zrangebyscore(sort_prefix(field),scr,scr,proc_options(options))
|
208
238
|
ids.map{|i| new(i) }
|
209
239
|
end
|
210
|
-
|
240
|
+
|
211
241
|
# get the first entry where field matches val
|
212
242
|
def self.find_by(field,val)
|
213
243
|
search_by(field,val,:limit => 1).first
|
214
244
|
end
|
215
245
|
|
246
|
+
def self.search(params)
|
247
|
+
return search_by(*params.first) if params.size == 1 # comment out for benchmarking purposes
|
248
|
+
result_set = nil
|
249
|
+
params.each do |field,value|
|
250
|
+
scr = EasyRedis.score(value)
|
251
|
+
ids = EasyRedis.redis.zrangebyscore(sort_prefix(field),scr,scr)
|
252
|
+
result_set = result_set ? (result_set & Set.new(ids)) : Set.new(ids)
|
253
|
+
end
|
254
|
+
result_set.map{|i|new(i)}
|
255
|
+
end
|
256
|
+
|
216
257
|
# get all entries, sorted by the given field
|
217
258
|
def self.sort_by(field,options = {:order => :asc})
|
218
259
|
EasyRedis::Sort.new(field,options[:order],self)
|
@@ -284,7 +325,7 @@ module EasyRedis
|
|
284
325
|
def [](field)
|
285
326
|
EasyRedis.redis.hget(key_name,field)
|
286
327
|
end
|
287
|
-
|
328
|
+
|
288
329
|
# directly change a field of this entry's redis hash
|
289
330
|
#
|
290
331
|
# note that you cannot access created_at or id with these methods
|
@@ -311,6 +352,13 @@ module EasyRedis
|
|
311
352
|
"#<#{self.class.name}:#{@id}>"
|
312
353
|
end
|
313
354
|
|
355
|
+
# clears all fields, causing future gets to reretrive them from redis
|
356
|
+
def clear
|
357
|
+
@@fields.each do |field|
|
358
|
+
self.instance_variable_set("@"+field.to_s,nil)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
314
362
|
|
315
363
|
private
|
316
364
|
|
data/tests/test.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: easyredis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Alec Benzer
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-06 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|