redis

一、扩容和缩容

  1.  扩容:正常情况下,使用的内存 > 数组容量 扩容,如果redis正在持久化,一般不扩容,除非使用的内存>5被数组容量的时候,一定会扩容。
  2.     缩容:当使用的容量 < 容量的1/10,为什么不是1/2,如果频繁插入和删除,可能会出现内存抖动。

二、字典的数据结构

  typedef struct dictEntry{  //键值对

    void *key;//存储key

    union{

      void *val;//可以指向不同类型string list hash stream set zset

      uint64_t u64;//用于redis集群 哨兵模式,选举算法

      int64_6 s64;//记录过期时间

      double d;

    }v; //值

    struct dictEntry *next; //链表法,指向下一个节点;头插法(最近插入的数据,可能访问的比较多)。

  }dictEntry;

  typedef struct dict{ //string list set  zset hashes

    dictType *type;//改字典对应的特定操作函数

    void *privdata;改字典依赖的数据 上下文 具体操作 set key val

    dictht ht[2];在扩容的时候,如果数据量特别大,不会一次吧老的数据一次hash到新的数组里面,而是采用渐进式rehash,扩容:ht[0]为扩容之前的数组,ht[1]为扩容之后的数组;缩容:ht[0]为缩容之前的数组,ht[1]为缩容之后的数组。

    long reshashidx;//为-1时代表没有进行reshash,否则标识正在rehash,值表示进行rehash的位置。

    unsigned long iterators;//安全迭代器的个数

  }dict;

  typedef struct dictht{

    dictEntry **table;//指针数组,用于存储键值对

    unsigned long size; //table数组的大小,默认值为2的n次幂

    unsigned long sizemask; //掩码 值为size - 1;例如 4-1,8-1,16-1 

    unsigned long used; //已经存的键值对

  }dictht;

  

三、redis事件机制

  网络IO,阻塞和非阻塞

  

原文地址:https://www.cnblogs.com/rana4504/p/14940278.html