tips:db:redis

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tips:db:redis [2017/11/06 16:19] – [gui] scipiotips:db:redis [2017/11/07 13:44] (current) – [redis] scipio
Line 1: Line 1:
 ====== redis ====== ====== redis ======
  
-===== concepts ===== +  [[https://medium.com/@stockholmux/from-sql-to-redis-chapter-1-145c82e4baa0|from sql to 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** type **hash** for **tables and rows**
Line 8: Line 74:
 namespace:table:id namespace:table:id
 <code> <code>
-HSET gianoauth:realms:users:scipio name "Stefano Scipioni" +INCR gianoauth:realm:next_user_id => 1 
-HSET gianoauth:realms:users:scipio username "scipio" +HSET gianoauth:realm:user:1 id 1 username "scipio" name "Stefano Scipioni" password "hash" visits 0
-HSET gianoauth:realms:users:scipio password "hash"+
 </code> </code>
  
 add an INDEX with **sets**: collections of strings that are unordered and cannot contain duplicates add an INDEX with **sets**: collections of strings that are unordered and cannot contain duplicates
 <code> <code>
-SADD gianoauth:realms:all-users gianoauth:realms:users:scipio+SADD gianoauth:realm:all-users gianoauth:realms:users:
 +</code> 
 + 
 +visits 
 +<code> 
 +HINCRBY gianoauth:realm:user:1 visits 1 => 2
 </code> </code>
  
Line 27: Line 97:
 </code> </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.1509981566.txt.gz
  • Last modified: 2017/11/06 16:19
  • by scipio