Skip to content

Instantly share code, notes, and snippets.

@karlpokus
Last active November 18, 2015 15:35
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 karlpokus/8539171bea2e26d39f9e to your computer and use it in GitHub Desktop.
Save karlpokus/8539171bea2e26d39f9e to your computer and use it in GitHub Desktop.
redis api in bash
# simple string
SET name "fido"
GET name // "fido"
# increment
SET connections 10
INCR connections // 11
DEL connections // deleted
INCR connections // 1
# list (like array)
# RPUSH puts the new value at the end of the list
RPUSH friends "Alice"
RPUSH friends "Bob"
# LPUSH puts the new value at the start of the list.
LPUSH friends "Sam"
# LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve elements until the end of the list.
LRANGE friends 0 -1 => 1) "Sam", 2) "Alice", 3) "Bob"
LRANGE friends 0 1 => 1) "Sam", 2) "Alice"
LRANGE friends 1 2 => 1) "Alice", 2) "Bob"
# LLEN returns the current length of the list.
LLEN friends => 3
# LPOP removes the first element from the list and returns it.
LPOP friends => "Sam"
# RPOP removes the last element from the list and returns it.
RPOP friends => "Bob"
# set - a set is similar to a list, except it does not have a specific order and each element may only appear once.
# add
SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"
# remove
SREM superpowers "reflexes"
# check existance (1 if true and 0 if false)
SISMEMBER superpowers "flight" // 1
SISMEMBER superpowers "reflexes" // 0
# list all items
SMEMBERS superpowers //
# hashes (like object)
HSET user:1000 name "John"
HSET user:1000 email "John@foo.com"
HGETALL user:1000
HGET user:1001 name
HSET user:1000 visits 10
HINCRBY user:1000 visits 1 // 11
HINCRBY user:1000 visits 10 // 21
HDEL user:1000 visits
HINCRBY user:1000 visits 1 // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment