Redis data types

来源:互联网 发布:南大碎尸案推理 知乎 编辑:程序博客网 时间:2024/06/06 10:24
Binary-safe strings.
Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists.
Sets: collections of unique, unsorted string elements.
Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).
Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.
Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don't be scared, it is simpler than it seems... See later in the HyperLogLog section of this tutorial.


key:binary sequence,valid
Very long keys are not a good idea,SHA1 is a better idea
Very short keys are often not a good idea
Try to stick with a schema,Dots or dashes
The maximum allowed key size is 512 MB

exists type






expires
They can be set both using seconds or milliseconds precision.
However the expire time resolution is always 1 millisecond.
Information about expires are replicated and persisted on disk, the time virtually passes when your Redis server remains stopped (this means that Redis saves the date at which a key will expire).

expire,persist,ttl,set ex


1.Strings
set get del getset mset mget incr incrby decr decrby(atomic)
set: nx,xx
only data type in Memcached

2.Lists:a sequence of ordered elements
lpush rpush lrange lpop rpop ltrim brpop blop rpoplpush brpoplpush llen
Redis lists are implemented via Linked Lists.This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time. 
Accessing an element by index is very fast in lists implemented with an Array (constant time indexed access) and not so fast in lists implemented by linked lists (where the operation requires an amount of work proportional to the index of the accessed element).
When fast access to the middle of a large collection of elements is important, there is a different data structure that can be used, called sorted sets. 
variadic commands 0,-1
use cases:
Remember the latest updates posted by users into a social network.
Communication between processes, using a consumer-producer pattern where the producer pushes items into a list, and a consumer (usually a worker) consumes those items and executed actions. Redis has special list commands to make this use case both more reliable and efficient.
blocking operation

3.Hashes
hget hset hmset hmget hgetall hincrby

4.Sets
sadd smembers sismember sinter spop sunionstore scard srandmember
unordered collections
expressing relations between objects
poker game

5.Sorted sets
zadd zrange zrevrange withscores zrangebyscore -inf +inf zremrangebysocre zrank zrevrank zrangebylex zrevrangebylex zremrangebylex zlexcount
a mix between a Set and a Hash. Like sets, sorted sets are composed of unique, non-repeating string elements, so in some sense a sorted set is a set as well.However while elements inside sets are not ordered, every element in a sorted set is associated with a floating point value, called the score (this is why the type is also similar to a hash, since every element is mapped to a value).
generic index
suitable when there are tons of updates


6.Bitmaps
setbit getbit bitop bitcount bitpos
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type. 
remember a single bit information
Real time analytics of all kinds.
Storing space efficient but high performance boolean information associated with object IDs.
Bitmaps are trivial to split into multiple keys

7.HyperLogLogs
pfadd pfcount 
trade memory for precision,1%
constant amount of memory,12k bytes in the worst case, or a lot less if your HyperLogLog (We'll just call them HLL from now) has seen very few elements
HLLs in Redis, while technically a different data structure, is encoded as a Redis string
counting unique queries performed by users in a search form every day
0 0
原创粉丝点击