当前位置:网站首页>Redis介绍与使用
Redis介绍与使用
2022-07-06 09:19:00 【几何学家】
redis概述
redis是一种非关系型数据库,部署在内从中的键值数据库,其value可以有五种类型,分别为string,set,hash,list,zset
STRING:可以存储字符串,整数或者浮点数,可以对于整个字符串或者字符串中的字串进行操作,当其为整数或者浮点数时,可以进行自增或者自减操作
LIST:主要存储列表。两端压入或弹出,可以对于多个元素进行修剪,只保留一个范围内的元素
SET :可以添加获取移除单个元素,计算交,并,差集,从集合中随机获取元素
HASH:可以添加获取移除单个元素,检查元素是否存在,获取所有键值对
ZSET:添加获取删除元素,根据分值范围或成员来获取元素,计算一个键的排名
数据结构底层实现
string:
struct sdshdr{
int len;//已经占用空间长度
int free;//剩余空间长度
char buf[];//数据空间
}
相比于直接调用c字符串的优势是可以直接保存长度无需遍历。除此之外还可以减少缓冲区溢出的问题,因为使用前会先检查剩余长度是否足够。
list:
双向链表,不过多介绍
typedef struct listNode{
struct listNode *prev;
struct listNode * next;
void * value;
}
hash:
typedef struct dictht {
//哈希表数组
dictEntry **table;
//哈希表大小
unsigned long size;
//哈希表大小掩码,用于计算索引值
unsigned long sizemask;
//该哈希表已有节点的数量
unsigned long used;
}
typeof struct dictEntry{
//键
void *key;
//值
//不同的键值对应的值可能不同类型,
//因此使用union来解决这个问题
union{
void *val;
uint64_tu64;
int64_ts64;
}
struct dictEntry *next;
}
当hash冲突时,采用拉链法解决冲突
zset:
底层采用跳表实现,可以用来代替二叉平衡树
跳跃表(skiplist)是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速查找访问节点的目的。跳跃表是一种随机化的数据,跳跃表以有序的方式在层次化的链表中保存元素,效率和平衡树媲美 ——查找、删除、添加等操作都可以在O(logn)期望时间下完成。
//节点的具体实现
typedef struct zskiplistNode{
//层
struct zskiplistLevel{
//前进指针
struct zskiplistNode *forward;
//跨度
unsigned int span;
} level[];
//后退指针
struct zskiplistNode *backward;
//分值
double score;
//成员对象
robj *obj;
}
typedef struct zskiplist {
//表头节点和表尾节点
structz skiplistNode *header,*tail;
//表中节点数量
unsigned long length;
//表中层数最大的节点的层数
int level;
}zskiplist;
还不了解的小伙伴可以看下面这篇帖子
Skip List–跳表(全网最详细的跳表文章没有之一)
set:
typedef struct intset{
//编码方式int16_t、int32_t、int64_t
uint32_t enconding;
// 集合包含的元素数量
uint32_t length;
//保存元素的数组
int8_t contents[];
}
intset是Redis内存数据结构之一,和之前的 sds、 skiplist、dict、adlist
等通用数据相比,它是Redis特有的,用来实现Redis的Set结构(当元素较小且为数字类型时),它的特点有:元素类型只能为数字。 元素有三种类型:int16_t、int32_t、int64_t。 元素有序,不可重复。
intset和sds一样,内存连续,就像数组一样。
边栏推荐
- [Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- Fairygui bar subfamily (scroll bar, slider, progress bar)
- MySQL backup -- common errors in xtrabackup backup
- Edit distance (multi-source BFS)
- [algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
- FairyGUI簡單背包的制作
- Wechat applet development experience
- How to ensure data consistency between MySQL and redis?
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
猜你喜欢
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
![[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数

Excel导入,导出功能实现

rtklib单点定位spp使用抗差估计遇到的问题及解决

Fairygui loop list

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

使用rtknavi进行RT-PPP测试

Combination of fairygui check box and progress bar

《软件测试》习题答案:第一章
随机推荐
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
Wechat applet development experience
几道高频的JVM面试题
341. Flatten nested list iterator
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Record: the solution of MySQL denial of access when CMD starts for the first time
使用rtknavi进行RT-PPP测试
Employment of cashier [differential constraint]
KF UD分解之UD分解基础篇【1】
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Realization of the code for calculating the mean square error of GPS Height Fitting
闇の連鎖(LCA+树上差分)
Fairygui gain buff value change display
第一人称视角的角色移动
WSL common commands
The earth revolves around the sun
[算法] 剑指offer2 golang 面试题2:二进制加法
Detailed explanation of balanced binary tree is easy to understand
记录:动态Web项目servlet访问数据库404错误之解决
