当前位置:网站首页>redis -- 数据类型及操作
redis -- 数据类型及操作
2022-07-01 16:28:00 【zyanwei2018】
远程连接
redis-cli -h host -p port -a password
# 中文乱码
--raw
基础数据类型
string
- String
# 创建key
> set name zhangsan
OK
# 获取key
> get name
"zhangsan"
# 同时设置多项和获取多项
> set name zhangsan
> set age 18
> mset city wuhan country China
> mget name age city country
1) "zhangsan"
2) "18"
3) "wuhan"
4) "China"
# 删除key
> del name
(integer) 1
> get name
(nil)
# 检查是否存在
> exists name
(integer) 1
> exists age
(integer) 0
- 数字类型的String
> set n 11
OK
> get n
"11"
# 依次加一
> incr n
(integer) 12
# 依次减一
> decr n
(integer) 11
# 加特定值
> incrby n 10
(integer) 14
# 减特定值
> decrby n 10
(integer) 4
set
string类型的无序集合, 底层是 HashTable 数据结构,
字符串元素是无序的,且不能重复
# 添加
> sadd friends zhangsan
(integer) 1
> sadd friends lisi
(integer) 1
# 查看
> smembers friends
1) "lisi"
2) "zhangsan"
# 移除指定值
> srem friends lisi
# 随机删除
> spop friends
"zhangsan"
# 判断元素是否存在
> sismember friends zhangsan
(integer) 1
zset
# 添加
> zadd country 1 China
(integer) 1
> zadd country 1 UK
(integer) 1
# 查看
> zrange country 0 -1
1) "China"
2) "UK"
> zrange country 0 -1 withscores
1) "China"
2) "1"
3) "UK"
4) "1"
# 移除指定值
> zrem country UK
(integer) 1
List
# 从左边依次插入
> lpush rlist zhangsan lisi
(integer) 2
> lpush rlist wangwu
(integer) 3
# 从右边依次插入
> rpush rlist chenliu
(integer) 4
# 查看
> lrange rlist 0 -1
1) "wangwu"
2) "lisi"
3) "zhangsan"
4) "chenliu"
# 从左边依次删除
> lpop rlist
"wangwu"
# 从左边依次删除
> rpop rlist
"chenliu"
> lrange rlist 0 -1
1) "lisi"
2) "zhangsan"
# 查看索引为1的key值
> lindex rlist 1
"zhangsan"
# 修改索引为1的key值
> lset rlist 1 lili
OK
> lrange rlist 0 -1
1) "lisi"
2) "lili"
Hash
类似于Java中的Map,是一个 String 类型的 field 和 value 的映射表(键值对集合),并且特别适合用来存储对象
# hset
> hmset user name zhangsan age 18 sex male
OK
# hget
> hget user name
"zhangsan"
# hmget
> hmget user name
1) "zhangsan"
# 查看所有值
hkeys user
1) "name"
2) "age"
3) "sex"
4) "height"
# 修改
> hset user height 145
(integer) 0
> hmget user height
1) "145"
- 过期时间
- expire: 设置过期时间
- ttl: 获取过期时间,返回-1表示没有设置,-2表示数据不存在
> set name zhangsan
OK
> expire name 20
(integer) 1
> ttl name
(integer) 4
> ttl name
(integer) -2
边栏推荐
- Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
- Origin2018 installation and use (sorting)
- Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
- How to use etcd to realize distributed /etc directory
- Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
- FRP intranet penetration, reverse proxy
- 博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
- China nylon 11 industry research and future forecast report (2022 Edition)
- Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
- Preliminary study on golang crawler framework
猜你喜欢
Analysis of PostgreSQL storage structure
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
[flask introduction series] cookies and session
模板引擎Velocity 基础
How to cancel automatic search and install device drivers for laptops
【PyG】文档总结以及项目经验(持续更新
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
游戏行业安全选择游戏盾,效果怎么样?
Redis 分布式鎖
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
随机推荐
虚拟串口模拟器和串口调试助手使用教程「建议收藏」
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
How to cancel automatic search and install device drivers for laptops
Determine whether the linked list is a palindrome linked list
String class
[flask introduction series] cookies and session
Research and investment strategy report of neutral protease industry in China (2022 Edition)
Concatenate strings to get the result with the smallest dictionary order
C语言输入/输出流和文件操作
判断二叉树是否为二叉搜索树
Redis Distributed Lock
【PyG】文档总结以及项目经验(持续更新
[nodemon] app crashed - waiting for file changes before starting... resolvent
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
P2592 [ZJOI2008]生日聚会(dp)
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
Advantages, values and risks of chain games compared with traditional games