linux查看redis数据库键值,Redis存储键值方式详解

本文详细介绍了Redis作为一个内存数据库的键值存储方式,类似Java HashMap。RedisDb结构体包含了键空间、过期键空间等组件,并通过dict结构存储键值对。在Redis中,dict有两个表ht[0]和ht[1]用于rehash,避免一次性拷贝带来的性能影响。当需要查看Redis数据库键值时,可以通过Linux命令操作。
摘要由CSDN通过智能技术生成

redis是一个存储键值对的内存数据库,其存储键值的方式和Java中的HashMap相似。表征redis数据库的结构体是redisDb (在server.h文件中),redis服务器默认有16个数据库,编号从0到15。

typedef struct redisDb {

dict *dict;                /* 键空间 */

dict *expires;              /* 过期键空间 */

dict *blocking_keys;        /* 客户端在等待的键 (BLPOP) */

dict *ready_keys;          /* 接收到 push 的阻塞键 */

dict *watched_keys;        /* WATCHED keys for MULTI/EXEC CAS */

struct evictionPoolEntry *eviction_pool;    /* Eviction pool of keys */

int id;                    /* Database ID */

long long avg_ttl;          /* Average TTL, just for stats */

} redisDb;

dict 中存储的是 key -> value,而expires存储的 key -> 过期时间

dict是dict.h文件中定义的结构体:

typedef struct dict {

dictType *type;

void *privdata;

dictht ht[2];

long rehashidx; /* rehashing not in progress if rehashidx == -1 */

unsigned long iterators; /* number of iterators currently running */

} dict;

typedef struct dictht {

dictEntry **table;

unsigned long size; //table的大小

unsigned long sizemask;

unsigned long used; //table中键值对的数量

} dictht;

typedef struct dictEntry {

void *key;

union {

void *val;

uint64_t u64;

int64_t s64;

double d;

} v;

struct dictEntry *next;

} dictEntry;

dict可以类比为java中的HashMap,dictEntry对应java.util.HashMap.Entry,稍微不同的是,dict对entry的table做了简单的封装(即dictht),而且dict中有两个table用于rehash。

分析dict的dictReplace(dict.c文件中),类似于HashMap的put:

/* Add or Overwrite:

* Add an element, discarding the old value if the key already exists.

* Return 1 if the key was added from scratch, 0 if there was already an

* element with such key and dictReplace() just performed a value update

* operation. */

int dictReplace(dict *d, void *key, void *val)

{

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值