Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ddc6933
Showing
8 changed files
with
700 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*~ | ||
/coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# redis-rb | ||
|
||
A ruby client library for the redis key value storage system. | ||
|
||
## Models | ||
|
||
Minimal model support for redis-rb. | ||
Directly maps ruby properties to `model_name:id:field_name` keys in redis. | ||
Scalar, list and set properties are supported. | ||
|
||
Values can be marshaled to/from `Integer`, `Float`, `DateTime`, `JSON`. See `Redis::Model::Marshal` for more info. | ||
|
||
### Define | ||
|
||
require 'redis/model' | ||
|
||
class User < Redis::Model | ||
value :name, :string | ||
value :created, :datetime | ||
value :profile, :json | ||
list :posts, :json | ||
set :followers, :int | ||
end | ||
|
||
### Write | ||
|
||
u = User.with_key(1) | ||
u.name = 'Joe' # set user:1:name Joe | ||
u.created = DateTime.now # set user:1:created 2009-10-05T12:09:56+0400 | ||
u.profile = { # set user:1:profile {"sex":"M","about":"Lorem","age":23} | ||
:age => 23, | ||
:sex => 'M', | ||
:about => 'Lorem' | ||
} | ||
u.posts << { # rpush user:1:posts {"title":"Hello world!","text":"lorem"} | ||
:title => "Hello world!", | ||
:text => "lorem" | ||
} | ||
u.followers << 2 # sadd user:1:followers 2 | ||
|
||
### Read | ||
|
||
u = User.with_key(1) | ||
p u.name # get user:1:name | ||
p u.created.strftime('%m/%d/%Y') # get user:1:created | ||
p u.posts[0,20] # lrange user:1:posts 0 20 | ||
p u.posts[0] # lindex user:1:posts 0 | ||
p u.followers.has_key?(2) # sismember user:1:followers 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'rubygems' | ||
require 'spec/rake/spectask' | ||
|
||
task :default => :spec | ||
|
||
desc "Run specs" | ||
Spec::Rake::SpecTask.new do |t| | ||
t.spec_files = FileList['spec/**/*_spec.rb'] | ||
t.spec_opts = %w(-fs --color) | ||
end | ||
|
||
desc "Run all examples with RCov" | ||
Spec::Rake::SpecTask.new(:rcov) do |t| | ||
t.spec_files = FileList['spec/**/*_spec.rb'] | ||
t.rcov = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'rubygems' | ||
require 'benchmark' | ||
$:.push File.join(File.dirname(__FILE__), 'lib') | ||
require 'redis/model' | ||
|
||
n = 20000 | ||
|
||
class TestModel < Redis::Model | ||
value :foo | ||
list :bar | ||
end | ||
|
||
@r = Redis.new#(:debug => true) | ||
@r['foo'] = "The first line we sent to the server is some text" | ||
|
||
Benchmark.bmbm do |x| | ||
x.report("set (model)") do | ||
n.times do |i| | ||
m = TestModel.with_key(i) | ||
m.foo = "The first line we sent to the server is some text"; | ||
m.foo | ||
end | ||
end | ||
|
||
x.report("push+trim (model)") do | ||
n.times do |i| | ||
m = TestModel.with_key(i) | ||
m.bar << i | ||
m.bar.trim 0, 30 | ||
end | ||
end | ||
end | ||
|
||
@r.keys('*').each do |k| | ||
@r.delete k | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'rubygems' | ||
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') | ||
require 'redis/model' | ||
|
||
|
||
class User < Redis::Model | ||
value :name, :string | ||
value :created, :datetime | ||
value :profile, :json | ||
|
||
list :posts, :json | ||
|
||
set :followers, :int | ||
end | ||
|
||
|
||
|
||
u = User.with_key(1) | ||
u.delete | ||
u.name = 'Joe' | ||
u.created = DateTime.now | ||
u.profile = { | ||
:age => 23, | ||
:sex => 'M', | ||
:about => 'Lorem' | ||
} | ||
u.posts << { | ||
:title => "Hello world!", | ||
:text => "lorem" | ||
} | ||
u.followers << 2 | ||
|
||
|
||
|
||
u = User.with_key(1) | ||
p u.name | ||
p u.created.strftime('%m/%d/%Y') | ||
p u.posts[0,20] | ||
p u.posts[0] | ||
p u.followers.has_key?(2) |
Oops, something went wrong.