Skip to content

Instantly share code, notes, and snippets.

@KSoto
Created February 14, 2012 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KSoto/1824146 to your computer and use it in GitHub Desktop.
Save KSoto/1824146 to your computer and use it in GitHub Desktop.
CPSC 473 - Redis Ruby Script - H3 - Katie Soto
# Katie Soto
# CPSC 473, CSUF, 2/2012
# Create a Ruby script that mimics the "Try Redis" tutorial on http://try.redis-db.com/
require 'sinatra'
require 'redis'
redis = Redis.new
redis.flushdb
p 'Setting \'server:name\' to \'fido\'...'
redis.set 'server:name', 'fido'
p 'Value of \'server:name\' is... '
p redis['server:name']
p 'Setting \'connections\' to \'10\'...'
redis.set 'connections', '10'
p 'Incrementing \'connections\'...'
p redis.incr 'connections'
p 'Incrementing \'connections\' again...'
p redis.incr 'connections'
p 'Deleting \'connections\'...'
redis.del('connections')
p 'Incrementing \'connections\'...'
p redis.incr 'connections'
p 'Setting \'resource:lock\' to \'Redis Demo\'...'
redis.set 'resource:lock', 'Redis Demo'
p 'Setting Expiration of \'resource:lock\' to 120 seconds...'
redis.expire 'resource:lock', 120
p 'Getting the Time to Live of \'resource:lock\'...'
p redis.ttl 'resource:lock'
p 'Getting the Time to Live of \'count\'...'
p redis.ttl 'count'
p 'Setting \'resource:lock\' to \'Redis Demo 1\'...'
redis.set 'resource:lock', 'Redis Demo 1'
p 'Setting Expiration of \'resource:lock\' to 120 seconds...'
redis.expire 'resource:lock', 120
p 'Getting the Time to Live of \'resource:lock\'...'
p redis.ttl 'resource:lock'
p 'Setting \'resource:lock\' again resets the ttl timer:'
redis.set 'resource:lock', 'Redis Demo 2'
p redis.ttl 'resource:lock'
p 'Pushing \'Tom\' to the end of the \'friends\' list...'
redis.rpush 'friends', 'Tom'
p 'Pushing \'Bob\' to the end of the \'friends\' list...'
redis.rpush 'friends', 'Bob'
p 'Pushing \'Sam\' to the start of the \'friends\' list...'
redis.lpush 'friends', 'Sam'
p 'Retrieving all elements of the \'friends\' list...'
p redis.lrange 'friends', 0, -1
p 'Retrieving the first and second elements of the \'friends\' list...'
p redis.lrange 'friends', 0, 1
p 'Retrieving the second and third elements of the \'friends\' list...'
p redis.lrange 'friends', 1, 2
p 'Retrieving the current length of the list \'friends\'...'
p redis.llen 'friends'
p 'Popping (and returning) the first element from the list \'friends\'...'
p redis.lpop 'friends'
p 'Popping (and returning) the last element from the list \'friends\'...'
p redis.rpop 'friends'
p 'Retrieving the current length of the list \'friends\'...'
p redis.llen 'friends'
p 'Retrieving all elements of the \'friends\' list...'
p redis.lrange 'friends', 0, -1
p 'Adding \'flight\' to the \'superpowers\' set...'
redis.sadd 'superpowers', 'flight'
p 'Adding \'x-ray vision\' to the \'superpowers\' set...'
redis.sadd 'superpowers', 'x-ray vision'
p 'Adding \'reflexes\' to the \'superpowers\' set...'
redis.sadd 'superpowers', 'reflexes'
p 'Removing \'reflexes\' from the \'superpowers\' set...'
redis.srem 'superpowers', 'reflexes'
p 'Is \'flight\' a member of the set \'superpowers\'?'
p redis.sismember 'superpowers', 'flight'
p 'Is \'reflexes\' a member of the set \'superpowers\'?'
p redis.sismember 'superpowers', 'reflexes'
p 'Retrieving all members of the set \'superpowers\'...'
p redis.smembers 'superpowers'
p 'Adding \'pecking\' to the \'birdpowers\' set...'
redis.sadd 'birdpowers', 'pecking'
p 'Adding \'flight\' to the \'birdpowers\' set...'
redis.sadd 'birdpowers', 'flight'
p 'Retrieving the union of \'superpowers\' and \'birdpowers\'...'
p redis.sunion 'superpowers', 'birdpowers'
p 'Adding \'Alan Kay\' with score 1940 to the \'hackers\' sorted set...'
redis.zadd 'hackers', 1940, 'Alan Kay'
p 'Adding \'Richard Stallman\' with score 1953 to the \'hackers\' sorted set...'
redis.zadd 'hackers', 1953, 'Richard Stallman'
p 'Adding \'Yukihiro Matsumoto\' with score 1965 to the \'hackers\' sorted set...'
redis.zadd 'hackers', 1965, 'Yukihiro Matsumoto'
p 'Adding \'Claude Shannon\' with score 1916 to the \'hackers\' sorted set...'
redis.zadd 'hackers', 1916, 'Claude Shannon'
p 'Adding \'Linus Torvalds\' with score 1969 to the \'hackers\' sorted set...'
redis.zadd 'hackers', 1969, 'Linus Torvalds'
p 'Adding \'Alan Turing\' with score 1912 to the \'hackers\' sorted set...'
redis.zadd 'hackers', 1912, 'Alan Turing'
p 'Retrieving the third through fifth hackers of the sorted set...'
p redis.zrange 'hackers', 2, 4
@KSoto
Copy link
Author

KSoto commented Feb 14, 2012

awesome, if I do say so myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment