Created
October 20, 2014 14:34
-
-
Save pbrisbin/1541672e73834c95c9bb to your computer and use it in GitHub Desktop.
Redis cheat-sheet (gist mirror of something on pastebin.com)
This file contains hidden or 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
# Connect to a server | |
redis-cli server | |
# Make an insert | |
set key value | |
# Make multiple insert | |
mset key value key value [key value ...] | |
# Get a key (registry) value | |
get key | |
# Get many keys (registrys) value | |
mget key key ... | |
# Increment the value of an interger (only numbers) | |
incr keyInteger | |
# Decrement the value of an interger (only numbers) | |
decr keyInteger | |
# Start a queue of commands for serial execution | |
multi | |
# Execute the queued commands (clean the queue) | |
exec | |
# Clean the queue (therefore non command to execute, must start a new queue) | |
discard | |
################################################## | |
# HASH | |
################################################## | |
# Create a hash | |
hset my:hash key1 [value] | |
# Set multiple values to a hash | |
hmset my:hash key1 key2 | |
# Get single value from hash | |
hget my:hash key | |
# Get all values from the hash | |
hvals my:hash | |
# Get all keys from the hash | |
hkeys my:hash | |
################################################## | |
# LIST | |
################################################## | |
# Insert elements to the right of the list | |
rpush my:list key1 key2 | |
# Get elements from a range (firts it's the beggin and the second is the end of range (inclusive)) | |
# Negatives represents how many steps backward | |
# Get all elements | |
lrange my:list 0 -1 | |
# Remove a element by the times of occurrence. Negatives does the same, but search inverse. Zero represents all the occurrence | |
# [lr]rem - removes searching from the left or the right | |
lrem my:list 2 key | |
# Pop Remove and get the value | |
# [lr]pop | |
lpop my:list key | |
################################################## | |
# NOTE: | |
# To do a stack, should do rpush and rpop | |
# To do a queue, should do a lpush and rpop | |
################################################## | |
# To move the first element of a list and put in the end of other, use rpoplpush | |
rpoplpush my:list my:listB | |
# Hold (block) some seconds (120) and read the key, use b[r, l]pop and brpoplpush | |
brpop key 120 | |
################################################## | |
# SET | |
################################################## | |
# Add some values to a set -> sadd | |
sadd tech mkyong javacodegeeks theserverside | |
# Get members of the set | |
smembers tech | |
# Get the intersections | |
sinter tech mkyong | |
# Get the difference of the set1 from the set2 | |
sdiff tech mkyong | |
# See all the elements of differents sets | |
sunion set1 set2 set3 | |
# Store the union of many sets with sunionstore (Also sdiffstore sinterstore) | |
sunionstore destination set1 set2 set3 | |
# Count the elements of the set (cardinality) | |
scard set | |
# Removing: Ramdonly with spop, a specified key srem | |
spop set | |
srem tech mkyong | |
################################################## | |
# SORTED SET | |
################################################## | |
# Add elements (rank) to sorted set1 | |
zadd ss score rank scoren rankn | |
# Increment score of an rank in a ss. Negative for decrement | |
zincrby ss newScore rank | |
# Get elements as list. USE WITHSCORES to get the score of every rank | |
# Also can use zrevrange to get the ss in reverse inserction order. | |
zrange ss 0 -1 withscores | |
# Get elements according to the score (IT'S THE REASON OF THE SORTED SET). | |
# The range is inclusive, so, use '(' to make it exclusive. | |
# Use the "inf" keyword to make reference to the infinity | |
zrangebyscore ss (-8 inf | |
# To remove a range by rank, use ZREMRANGEBYRANK | |
zremrangebyrank ss gog | |
# To remove a range by score, use ZREMRANGEBYSCORE | |
zremrangebyscore ss 9 14 # ? | |
################################################## | |
# EXPIRATION | |
################################################## | |
# First, theres must be a key, then express the expiration time in secconds | |
expire key 10 # In 10 sec | |
# Check if the key still alive, 1 if yes, 0 otherwise | |
exists key | |
# Check how much time is left | |
ttl key | |
# Remote timer | |
persist key | |
# Set the expiration in a date, using Unix timestamp ins secconds | |
################################################## | |
# A common trick for keeping only recently used keys is to update the expire | |
# time whenever you retrieve a value. This is the most recently used (MRU) | |
# caching algorithm to ensure your most recently used keys will remain in | |
# Redis, while the neglected keys will just expire as normal. | |
################################################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment