redis支持数据类型介绍

一:redis data type:

1 binary-safe strings

2 lists:collections of string elementes sorted according to the order of insertion(元素的存储顺序按照插入顺序)

3 sets:collections of unique,usorted string elements.(唯一,没排序的元素)

4 sorted sets:similar to sets but where every string elements is associated to a floating number value,callled score.

5 hashes:which are maps composed of fields associated with values.both the field and the value are strings.

6 bitmaps:handle string values like an array of bits

7 hyperLogLogs:data strucure which is used in order to estimate the cardinality of a set

二:redis keys:redis keys are binary safe , means that you can use any binary sequence as a key(string like "foo" to the content of  a JPEG file).The empty string is also a valid key.

  a few other rules about keys:

  1) very long keys are not a good idea,for instance a key of 1024 bytes is a bad idea not only memory-wise,but also because the look up of the key in the dataset may require several costly key-comparisons.

  2) very short keys are often not a good idea.The latter is more readable and the added space is minor compared to the space used by the key object itself and the value object.While short keys will obviously consume a bit less memory,your job is to find the right balance.

  3) Dots or dashes are often used for multi-world fields.

  4) the maximum allowed key size is 512M.

三、1 redis strings:value can not bigger than 512M , the ability to set or retrieve the value of multiple keys in a single commond is also useful for reduced latency.For this reason there are the mset and mget commonds.

       2 altering and querying the key space:There are commonds that are not defined on particular types,but are useful in order to interact with the space of keys,and thus ,can be with keys of any types.For example the exists commond return 1 or 0 to signal if a given key exists or not in the database , while the del commond deletes a key and associated value , whatever the value is .

       3 redis expires:keys with limited time to live , ttl key:check the remaining time to live for the key.

       4 redis lists:

           1)redis lists are implemented via linked lists.redis lists are implemented with lists because for a database system it is crucial to be able to add elements to a very long list in a very fast way.Another strong advantage , as you will see in a moment , is that redis lists can be taken at constant length in constant time.(redis list 是通过链表来实现的,数据库系统很难做到很快的方式把元素添加到一个很长的list,list可以在常量时间内拿到子列表)

           2)lrange takes two indexes,the first and the last element of the range to return.(lrange带着两个索引,第一个和最后一个元素)

           3)ltrim commond is similar to lrange,but instead of displaying the specified range of elements it sets this range as the new list value as the new list value.All the elements outside the given range are removed.This allows for a very simple but useful pattern:doing a list push operation + a list trim operation together in order to add a new element and discard elements exceeding a limit.(添加新元素的操作可以由push + trim操作组成,达到丢弃超过list预定元素个数的以外的元素的目的,不需要单独的线程来做额外的处理)

           4)blocking operations:usual producer / consumer set up can be implemented in the following simple way:(使用生产者消费者模型实现阻塞操作)

                     1))To push items into the list , producers call lpush

                2))To extract / process items from the list , consumers call rpop

            为了避免消费者pop出空内容,redis提供了brpop和blpop which are versions of rpop and lpop able to block if the list is empty:they will return to the callers only when a new element is added to the list , or when  a user-specified timeout is reached.

           eg:brpop tasks 5(超时间设置为5秒,如果设置为0则是不超时,一直等待)

           5)automatic creation and removal of keys:

                1))when we add an element to an aggregate(集合) data type , if the target key does not exist,an empty aggregate data type is created before adding the element.

                2))when we remove elements from an aggregate data type , if the value remains empty , the key is automatically destroyed.

                3))calling a read-only command such as llen , or a write command removing elements,with an empty , always produces the same result as if the key is hoding an empty aggregate type of the type the command expects to find. 查看集合数据类型:type key

        5 hash:hashes are handy to represent objects , actually the number of fields you can put inside a hash has no practical limits(other than available memory),so you can use hashes in many different ways inside your application . It is worth nothing that (值得注意的是) small hashes (a few elements with small values) are encoded in special way in memory that make them very memory efficient.

        6 redis sets: sinter command,which performs the intersection(交集) between different sets.(找出参数集合中公共元素),返回set元素的个数:scard key

        7 redis sorted sets:sorted set are a data type which is similar to 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).sorted elements are ordered accoring to the following rule:

            1)if A and B are two elements with a different score , then A > B if A.score is > B.score

            2)if A and B have exactly the same score , then A > B if the A string is lexicographically(字典顺序) grater than the B string.A and B strings can not be equal since sorted sets only have unique elements.

         it is possible to return scores as well , using the with scores argument,可以返回元素的分

    8 bitmaps:Bitmaps are not an actual data type , but a set of bit-oriented operations defined on the string type.One of the biggest advantages is that they often provide extreme space savings when storing information.

       common user cases for bitmaps are :

           1)real time analytics of all kinds.(实时分析)

           2)storing space efficient but high performance boolean information associated with object IDs.

     9 hyerloglogs:A HyperLogLog is a probabilistic data structure used in order to count unique things.


           

        

 


你可能感兴趣的:(redis支持数据类型介绍)