List的命令介绍与源代码剖析(6)

一:列表类型
1. 大家一定要知道列表的数据存储是什么???
“双向链表”

二:命令介绍

1. LPush key value
  
2. RPush key value

3. LPop key

4. RPop key

5. LLen   O(1)

6. Lrange key start stop  O(N)
   
7. LRem key count value   O(N)

8. LIndex   O(N)

9. LSet key index value

10. LTrim key start end 

     《1》 复杂度: O(N),
     《2》 是lrange的破坏版
127.0.0.1:6379> lrange nums 0 -1
1) "40"
2) "300"
3) "20"

11. Linert key BeFore|After  pivot value

127.0.0.1:6379> linsert nums before 300 35
(integer) 4
127.0.0.1:6379> lrange nums
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange nums 0 -1
1) "40"
2) "35"
3) "300"
4) "20"
127.0.0.1:6379> 

12. RPOPLPush source dest 【原子性】  

127.0.0.1:6379> lpushrpop nums nums2
(error) ERR unknown command 'lpushrpop'
127.0.0.1:6379> rpoplpush nums nums2
"20"
127.0.0.1:6379> lrange nums2 0 -1
1) "20"
127.0.0.1:6379> rpoplpush nums nums2
"300"
127.0.0.1:6379> rpoplpush nums nums2
"35"
127.0.0.1:6379> rpoplpush nums nums2
"40"
127.0.0.1:6379> rpoplpush nums nums2
(nil)
127.0.0.1:6379> lrange nums2 0 -1
1) "40"
2) "35"
3) "300"

4) "20"

见下图

List的命令介绍与源代码剖析(6)




三:List的应用场景

    redis可以用来做 “队列”

四:List的源码实现


<1> list

    typedef struct list {
    listNode *head;
    listNode *tail;
    void *(*dup)(void *ptr);
    void (*free)(void *ptr);
    int (*match)(void *ptr, void *key);
    unsigned long len;
} list;


<2> listNode

typedef struct listNode {
    struct listNode *prev;
    struct listNode *next;
    void *value;
} listNode;


<3> listIter

typedef struct listIter {
    listNode *next;
    int direction;
} listIter;


五: List的常用方法


1. LLen 【看一下是否是获取list这个结构体中的len属性】
O(1)

   《1》  void llenCommand(redisClient *c)
   《2》  listTypeLength(o)
   《3》  listLength((list*)subject->ptr);
   《4》  #define listLength(l) ((l)->len)

2. LPush/RPush 【查看一下时间复杂度是否为O(1)】

   《1》 void lpushCommand(redisClient *c) 
   《2》 listTypePush(lobj,c->argv[j],where);
   《3》 listAddNodeHead(subject->ptr,value); 

3.  list的源码在 adlist.c,t_list.c 这两个文件中。
    

见下图List的命令介绍与源代码剖析(6)