This is a fork of redis-rb, it supports Redisql commands
Redisql commands are tested in the file "test/commands_on_sql_test.rb"
A quick example would be: (in Ruby) >> r.create_table("new_table", "(id INT, val TEXT)") >> r.insert("new_table", "(1,ONE)"); >> r.insert("new_table", "(2,TWO)"); >> r.select("", "new_table", "id BETWEEN 1 AND 2") => ["1,ONE", "2,TWO"] >> r.update("new_table", "val=two", "id = 2") >> r.select("", "new_table", "id BETWEEN 1 AND 2") => ["1,ONE", "2,two"] It is SQL, you know it already probably
There are also MORPH commands to morph data between relation tables and redis data objects Create the table "x_table" from the results from the redis command "ZRANGE zset 0 1 WITHSCORES" >> r.create_table_as("x_table", "ZRANGE", "zset", "0 1 WITHSCORES")
Create the redis HashTable "z_hash" from the results of the SQL command "SELECT zkey,zvalue FROM z_table WHERE zkey BETWEEN z2 AND z4"
>> r.select_store("zkey,zvalue", "z_table", "zkey BETWEEN z2 AND z4", "HSET z_hash")
The MORPH commands work on all redis data objects and w/ all SQL SELECT result sets (i.e. range-queries and joins)
AND of course ALL redis commands are supported ... the following is the text of redis-rb
A Ruby client library for the Redis key-value store.
Versions 1.0.x target all versions of Redis. You have to use this one if you are using Redis < 1.2.
Version 2.0 is a big refactoring of the previous version and makes little effort to be backwards-compatible when it shouldn't. It does not support Redis' original protocol, favoring the new, binary-safe one. You should be using this version if you're running Redis 1.2+.
Redis is a key-value store with some interesting features:
- It's fast.
- Keys are strings but values are typed. Currently Redis supports strings, lists, sets, sorted sets and hashes. Atomic operations can be done on all of these types.
See the Redis homepage for more information.
You can connect to Redis by instantiating the Redis
class:
require "redis"
redis = Redis.new
This assumes Redis was started with default values listening on localhost
, port 6379. If you need to connect to a remote server or a different port, try:
redis = Redis.new(:host => "10.0.1.1", :port => 6380)
Once connected, you can start running commands against Redis:
>> redis.set "foo", "bar"
=> "OK"
>> redis.get "foo"
=> "bar"
>> redis.sadd "users", "albert"
=> true
>> redis.sadd "users", "bernard"
=> true
>> redis.sadd "users", "charles"
=> true
How many users?
>> redis.scard "users"
=> 3
Is albert
a user?
>> redis.sismember "users", "albert"
=> true
Is isabel
a user?
>> redis.sismember "users", "isabel"
=> false
Handle groups:
>> redis.sadd "admins", "albert"
=> true
>> redis.sadd "admins", "isabel"
=> true
Users who are also admins:
>> redis.sinter "users", "admins"
=> ["albert"]
Users who are not admins:
>> redis.sdiff "users", "admins"
=> ["bernard", "charles"]
Admins who are not users:
>> redis.sdiff "admins", "users"
=> ["isabel"]
All users and admins:
>> redis.sunion "admins", "users"
=> ["albert", "bernard", "charles", "isabel"]
Redis only stores strings as values. If you want to store an object inside a key, you can use a serialization/deseralization mechanism like JSON:
>> redis.set "foo", [1, 2, 3].to_json
=> OK
>> JSON.parse(redis.get("foo"))
=> [1, 2, 3]
You can use MULTI/EXEC
to run arbitrary commands in an atomic fashion:
redis.multi do
redis.set "foo", "bar"
redis.incr "baz"
end
Check the Redis Command Reference or check the tests to find out how to use this client.
Fork the project and send pull requests. You can also ask for help at #redis-rb
on Freenode.