Redis Set Go
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Redis Set Go

on

  • 424 views

 

Statistics

Views

Total Views
424
Views on SlideShare
424
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Redis Set Go Document Transcript

  • 1. The Redis Wondering what’s redis? is it a red thing? can we eat that? Well, so much for questions, let’s start diggin what redis really is. " Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. ” Fore more info : http://redis.io/topics/introduction The Installation We need to install the service of course to make it work. Jerome (PHP) already prepared the documentation on how to install redis in your mac. Thanks to Jerome! Installing on mac : http://goo.gl/000DmC Installing on ubuntu : http://goo.gl/utn659 NOTE : “The Redis project does not directly support Windows, however the Microsoft Open Tech group develops and maintains an experimental Windows port targeting Win32/64” Working on Redis After the installation, of course we need to use and work on it. So let’s get started! ● Starting the redis-server ○ in the terminal/command line execute redis-server. ● Executing commands (SET, GET, etc..) ○ in the terminal/command line execute redis-cli and you will be prompted to enter some redis commands. DID YOU KNOW? Redis means “REmote DIctionary Server”  
  • 2. Redis Data Types and Basic Commands Strings ● The most basic kind of Redis value. ● Binary safe (http://en.wikipedia.org/wiki/Binary-safe) ● Can be at max 512MB in length. Basic Commands ( for more string commands : http://redis.io/commands/#string ) ● SET key value - Set the string value of a key. - If the key already exists, the value will be overwritten. Return : Status code “OK” ● GET key - Get the value of a key - Will return an error if the value of the key is not a string. - if the key does not exist it will return (nil) or null. Return : Value of the key Example : (for GET and SET)                   ● redis>  SET  foo  “bar” OK redis>  GET  foo “bar” APPEND key append_string - Append a value to a key. - If the key does not exist, the key will be created with the value of the string to append. Return : Length of the value of the key. Example :                           redis>  SET  foo  “Hello” OK redis>  APPEND  foo  “  World” (integer)  11 redis>  GET  foo “Hello  World”
  • 3. ● INCR key - Increment the integer value of a key by one. - Will return an error if the value is not an integer - If key does not exist, initial value will be 0 then perform increment. Return : The value of the key after the increment Example :                         ● redis>  SET  foo  10 OK redis>  INCR  foo (integer)  11 redis>  GET  foo 11 INCRBY key increment - Increment the integer value of a key by the given number. - Will return an error if the value is not an integer. - If key does not exist, initial value will be 0 then perform decrement. Return : The value of the key after the increment Example :                         ● redis>  SET  foo  10 OK redis>  INCRBY  foo  10 (integer)  20 redis>  GET  foo 20 DECR key - Decrement the integer value of a key by one. - Will return an error if the value is not an integer - If key does not exist, initial value will be 0 then perform decrement. Return : The value of the key after the decrement Example :                           redis>  SET  foo  10 OK redis>  DECR  foo (integer)  9 redis>  GET  foo 9
  • 4. ● DECRBY key decrement - Decrement the integer value of a key by the given number. - Will return an error if the value is not an integer. - If key does not exist, initial value will be 0 then perform decrement. Return : The value of the key after the decrement Example :                         ● redis>  SET  foo  10 OK redis>  DECRBY  foo  9 (integer)  1 redis>  GET  foo 1 STRLEN key - Returns the length of value stored in a key Return : length of the value, 0 if key does not exist Example :                   redis>  SET  foo  “I  am  legend.” OK redis>  STRLEN  foo (integer)  12 Lists ● List of strings sorted by insertion order ● Can append or prepend in a list Basic Commands ( for more list commands : http://redis.io/commands#list ) ● {L,R}PUSH(X) key value [value…] - Prepend/Append multiple values to a list. (LPUSH prepend, RPUSH append) - If key does not exist, it will create an empty list then execute the push process. - {L,R}PUSHX appends/prepends only if the list exists. Return : length of the list after the push process Example :         redis>  RPUSH  todo_list  “Clean  house”  “Workout” (integer)  2
  • 5.                   redis>  LRANGE  todo_list  0  -­‐1 1)  “Clean  House” 2)  “Workout”                               redis>  LPUSH  todo_list  “Clean  house”  “Workout” (integer)  2 redis>  LRANGE  todo_list  0  -­‐1 1)  “Workout” 2)  “Clean  House” DID YOU KNOW? To display all elements of the list, execute LRANGE key 0 -1 ● LINDEX key index - Prepend/Append multiple values to a list. (LPUSH prepend, RPUSH append) - If key does not exist, it will create an empty list then execute the push process. Return : length of the list after the push process Example :                                                       redis>  LPUSH  foo  “bar”  “fizz”  “bazz” (integer)  3 redis>  LINDEX  foo  0 “bar” redis>  LINDEX  foo  1 “fizz” redis>  LINDEX  foo  2 “bazz” redis>  LINDEX  foo  3 (nil) ● LRANGE  key start end   - Get a range of elements from a list Return : Elements within the range specified Example :                           redis>  LPUSH  foo  “bar”  “fizz”  “bazz” (integer)  3 redis>  LRANGE  foo  0  1 1)  “bazz” 2)  “fizz”
  • 6.       Sets ● Unordered collection of strings. Basic Commands ( for more string commands : http://redis.io/commands#set ) ● SADD key member [ member… ] - Add specified member to a set - if key doesn’t exist, new set will be created and then add the specified keys. - Existing member will be ignored. - Will return an error if the value stored in the key is not a set. Return : The number of members/elements that were added to the set. Example :                         redis>  SADD  foo  “bar”  “fizz”  “fizz” (integer)  2 redis>  SMEMBERS  foo 1)  “bazz” 2)  “fizz” ● SMEMBERS key - Get all the members in a set - if key does not exist, empty set will be returned. Return : All members of a set Example :                         redis>  SADD  foo  “bar”  “fizz”  “fizz” (integer)  2 redis>  SMEMBERS  foo 1)  “bazz” 2)  “fizz” ● SCARD key - Get the number of elements in a set Return : The number of elements in the set. 0 if key does not exist. Example :               redis>  SADD  foo  “bar”  “fizz”  “fizz” (integer)  2 redis>  SADD  foo  “bazz”
  • 7.                   (integer)  1 redis>  SCARD  foo (integer)  3 ● SISMEMBER key member - Check if a member is a member of the set stored in specified key . Return : 1 if member exist, 0 if not           Example : redis>  SADD  foo  “bar”   (integer)  1   redis>  SISMEMBER  foo  “bar”   (integer)  1   redis>  SISMEMBER  foo  “test”   (integer)  0           ● SDIFF key [ key… ] - Differentiate the first key to the other specified key Return : The members of the first set not existing on the specified set/s. Example :                                                       redis>  SADD  foo  “bar” (integer)  1 redis>  SADD  foo  “test” (integer)  1 redis>  SADD  foo2  “bar” (integer)  1 redis>  SADD  foo2  “fizz” (integer)  1 redis>  SDIFF  foo  foo2 1)  “test” Hashes ● maps between string fields and string values. ● perfect for data type to represent objects (like a user with id, name, username, password, etc). Basic Commands ( for more string commands : http://redis.io/commands#hash ) ●   HSET key field value - Set a field in hash stored at key. - If field already exist, value of the field will be overwritten.
  • 8. Return : 1 if field is new and value was set, 0 if field exists and value was overwritten. Example :                   ● redis>  HSET  hashtest  hashfield  “hashvalue” (integer)  1 redis>  HSET  hashtest  hashfield  “hashvalueoverwrite” (integer)  0 HMSET key field value [field value…] - Set multiple field in hash stored at key. - If fields already exist, value of the field will be overwritten. Return : OK if no error in setting the key Example :                                                       ● redis>  HMSET  hashtest  field1  “hashval1”  field2  “hashval2” OK redis>  HGET  hashtest  field1 “hashval1” redis>  HGET  hashtest  field2 “hashval2” redis>  HMSET  hashtest  field1  “hashvaloverwrite1” OK redis>  HGET  hashtest  field1 “hashvaloverwrite1” HGET key field - Returns the value of the specified field. Return : Value of the field. If field does not exist nil will be returned. Example :                   ●   redis>  HSET  hashtest  field1  “hashval1” OK redis>  HGET  hashtest  field1 “hashval1” HMGET key field [field…] - Returns the value of the specified field/s.
  • 9. Return : List of values associated with the field/s specified. Example :                               ● redis>  HMSET  hashtest  field1  “hashval1”  field2  “hashval2” OK redis>  HGET  hashtest  field1  field2  field3 1)  “hashval1” 2)  “hashval2” 3)  (nil) HDEL key field [field…] - Removes the specified field/s from the hash Return : Number of fields that were removed. Example :                               redis>  HSET  hashkey  field1  “hashval1” OK redis>  HDEL  hashkey  field0 (integer)  0 redis>  HDEL  hashkey  field1 (integer)  1 Sorted Sets ● almost similar to SETS. (collection of strings) ● associated with scores that will be used in ordering the set. ● members are unique, scores can be repeated. ● in games, this is often used for computing ranks. Basic Commands ( for more string commands : http://redis.io/commands#sorted_set ) ● ZADD key score member [score member...] - Adds a member with specific score in the sorted set. - If a specified member is existing, score will be overwritten. Return : Number of elements added in the sorted set not including the existing member value update. Example :                           redis>  ZADD  sstest  1  “ssmember1” (integer)  1 redis>  ZADD  sstest  2  “ssmember2” (integer)  1 redis>  ZADD  sstest  3  “ssmember3”
  • 10.                                                 ● (integer)  1 redis>  ZRANGE  sstest  0  -­‐1  WITHSCORES 1)  “ssmember1” 2)  “1” 3)  “ssmember2” 4)  “2” 5)  “ssmember3” 6)  “3” ZREM key member [member...] - Removes specified member/s in a sorted set. - If a specified member is existing, score will be overwritten. Return : Number of members removed from the sorted set. Example :                                                 ● redis>  ZADD  sstest  1  “ssmember1” (integer)  1 redis>  ZADD  sstest  2  “ssmember2” (integer)  1 redis>  ZREM  sstest  “ssmember2” (integer)  1 redis>  ZRANGE  sstest  0  -­‐1  WITHSCORES 1)  “ssmember1” 2)  “1” ZRANGE key start stop [WITHSCORES] - Returns elements within the specified range. (elements are in ascending order, use ZREVRANGE to make the order in descending) - start and stop option are both zero-based indexes, 0 is the first element, 1 is next and so on. - WITHSCORES option will also return the element with the score. Return : List of elements based on the range specified. Example :                                                   redis>  ZADD  sstest  1  “ssmember1” (integer)  1 redis>  ZADD  sstest  2  “ssmember2” (integer)  1 redis>  ZADD  sstest  3  “ssmember3” (integer)  1 redis>  ZRANGE  sstest  0  1 1)  “ssmember1” 2)  “ssmember2”
  • 11. Redis Configuration Redis can be started without any configuration file. It will use the default configuration of redis. Easy right? But using default configuration is recommended only for testing and development phase of your application. 3 Ways to configure redis server 1. Using a configuration file ● create a redis.conf file containing your configurations (see this link for redis configurations directives -> https://raw.github.com/antirez/redis/2.6/redis.conf) ● start redis server with the config file you made ○ redis-­‐server  /path/to/redis.conf   2. Passing arguments in the command line ● arguments passed are the same with the directives in redis.conf file. You just have to add the prefix --. ○ ● redis-­‐server  -­‐-­‐daemonize  yes  -­‐-­‐port  6380   this will generate an in-memory configuration file. 3. On the fly ● While the redis-server is running you can execute a command in redis-cli ○ CONFIG  SET  timeout  0   ● ●   arguments passed to CONFIG SET are also the same in the config file directives. note that setting configuration on the fly will not affect the redis.conf file you made.