Notes on Redis
Feb. 3rd, 2015 03:08 pmWork has resolved the question of what NoSQL database I should learn: I'm being given a project that will involve Redis as well as PostgreSQL.
Available operations:
- get the value corresponding to a string-valued key
- MGET key1 key2 key3: get multiple values, returning them as an array
- set the value corresponding to a key. Variants: set the value for a key, only if the key is already present or isn't already present.
- DEL key: delete the value for a key.
- EXPIRE key delay: cause the key to expire and disappear after 'delay' seconds. Resolution is 1msec. (? correct ?) Can also specify delay when adding the key: SET key value EX delay.
- TTL key: returns the # of second the key has left before it expires.
- TYPE key: returns the type of the corresponding value
- EXISTS key: returns 1/0 based on whether the key exists.
- Increment/decrement counters atomically. INCR key increments the value by 1; INCRBY key n increments the value by n.
- GETSET key value: stores a new value and returns the previous value.
Data types for values:
Linked lists:
- Written as 1,2,3,4.
- LLEN key returns the length.
- LPUSH key value adds a value as the head of the list, and runs in O(1) time. RPUSH key value adds the value as the tail and is O(n).
- There are corresponding LPOP and RPOP commands.
- LRANGE key start end returns an array containing the corresponding values, and is also O(n).
- Capped lists: LTRIM key start end keeps only the specified range and discards everything else. To keep the 10 most recent items, do LTRIM key 0 9.
- Blocking pops: BLPOP and BRPOP block if the list is empty, or can take a timeout. Useful for implementing queues. There's even BRPOPLPUSH srckey destkey will pop the rightmost item from list 1 and push it onto list 2.
Hashes
- HSET key attr1 value1: set a single key in a hash. For multiple keys, use HMSET key attr1 value1 attr2 value2 ...
- HGET key attrname: retrieve a key within a hash. HGETALL key returns a flat array of [attr1, value1, attr2, ...].
- Other commands also have hash variants: HINCRBY, HEXISTS.
Sets:
- Are unordered collections of items. Commands include SADD key elem, SISMEMBER key elem to check membership, SPOP key removes a random element and returns it, SCARD key returns the number of elements ('cardinality').
- Intersection: SINTER key1 key2 ....
Sorted sets: each element has an FP value called its score, and the set is sorted by this score in ascending order.
- ZADD key score1 elem1 score2 elem2 .... Updates are O(log N).
- ZRANGE key start end and ZREVRANGE key start end return slices in sorted or reversed order, returning just the elements. Add "WITHSCORES" to also return the scores.
- To search by score, ZRANGEBYSCORE key min max. min and max are inclusive; you can say -inf or +inf. ZREMRANGEBYSCORE would remove the matching elements.
- Ranking: ZRANK key elem returns the position of the element in the sorted order.
- Redis 2.8 can search by the elements as well: ZRANGEBYLEX key [B [P returns elements between B and P.
Bitmaps: bit operations are defined on strings. Size limit is 512Mb = 2**32 bits.
- SETBIT key bitnum 0/1 sets a bit, and GETBIT key bitnum returns it.
- Bit operations over entire string: BITOP and|or|xor|not destkey srckey1 srckey2 ... - for NOT, only one srckey can be supplied.
- BITCOUNT key counts set bits, and BITPOS key 1/0 returns the index of the first 1 or 0 bit (you can also constrain this to a range).
HyperLogLogs: probabilistic data structure for counting # of unique items seen. Has a certain amount of error on the resulting value, but is compact and doesn't grow unboundedly.
- PFADD key elem1 elem2 elem3 ... stores the elements.
- PFCOUNT key returns the # of unique elements seen.