tips:db:redis

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tips:db:redis [2017/11/06 14:24] – created scipiotips:db:redis [2017/11/07 13:44] (current) – [redis] scipio
Line 1: Line 1:
 ====== redis ====== ====== redis ======
  
 +  * [[https://medium.com/@stockholmux/from-sql-to-redis-chapter-1-145c82e4baa0|from sql to redis]]
 +  * [[https://joshtronic.com/2013/07/29/mysql-and-redis-command-equivalents/|from mysql to redis]]
 +
 +===== string =====
 +
 +
 +type **string**: store a value [integer, string, ...] belong a key [string]
 +<code>
 +SET connections 10
 +INCR connections => 11
 +INCR connections => 12
 +DEL connections
 +INCR connections => 1
 +</code>
 +
 +expiring key
 +<code>
 +SET resource:lock "Redis Demo"
 +EXPIRE resource:lock 120
 +</code>
 +
 +===== list =====
 +
 +A list have an order
 +
 +RPUSH puts the new value at the end of the list. LPUSH puts the new value at the start of the list. LLEN, LPOP, RPOP
 +<code>
 +RPUSH friends "Alice"
 +RPUSH friends "Bob"
 +</code>
 +
 +<code>
 +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"
 +</code>
 +
 +===== set =====
 +
 +A set does not have an order but each element may only appear once. 
 +
 +<code>
 +SADD superpowers "flight"
 +SADD superpowers "x-ray vision"
 +SADD superpowers "reflexes"
 +SREM superpowers "reflexes"
 +
 +SISMEMBER superpowers "flight" => 1
 +SISMEMBER superpowers "reflexes" => 1
 +</code>
 +
 +===== sorted set =====
 +
 +A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.
 +<code>
 +ZADD hackers 1940 "Alan Kay"
 +ZADD hackers 1969 "Linus Torvalds"
 +ZADD hackers 1906 "Grace Hopper"
 +ZADD hackers 1953 "Richard Stallman"
 +
 +ZRANGE hackers 2 4
 +1) "Richard Stallman"
 +2) "Linus Torvalds"
 +</code>
 +
 +
 +===== hash =====
 +
 +
 +type **hash** for **tables and rows**
 +
 +namespace:table:id
 +<code>
 +INCR gianoauth:realm:next_user_id => 1
 +HSET gianoauth:realm:user:1 id 1 username "scipio" name "Stefano Scipioni" password "hash" visits 0
 +</code>
 +
 +add an INDEX with **sets**: collections of strings that are unordered and cannot contain duplicates
 +<code>
 +SADD gianoauth:realm:all-users gianoauth:realms:users:1
 +</code>
 +
 +visits
 +<code>
 +HINCRBY gianoauth:realm:user:1 visits 1 => 2
 +</code>
 +
 +use multi exec to surround HSET and SADD
 +<code>
 +MULTI
 +HSET ...
 +HSET ...
 +SADD ...
 +EXEC
 +</code>
 +
 +**sorted set**: like a set a sorted set only allows members without repeats, but also allows you to specify a score
 +
 +<code>
 +ZADD gianoauth:realms:priceIndex 10.99 scipio
 +</code>
 ===== gui ===== ===== gui =====
  
  • tips/db/redis.1509974646.txt.gz
  • Last modified: 2017/11/06 14:24
  • by scipio