当前位置:网站首页>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
边栏推荐
- Are you still using charged document management tools? I have a better choice! Completely free
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- How to cancel automatic search and install device drivers for laptops
- Activity的生命周期和启动模式详解
- How to restore the system with one click on Lenovo laptop
- 博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
- C語言輸入/輸出流和文件操作
- Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
- [nodemon] app crashed - waiting for file changes before starting... resolvent
- Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
猜你喜欢
VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v
Stegano in the world of attack and defense
SystemVerilog-结构体(二)
Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
Transition technology from IPv4 to IPv6
Template Engine Velocity Foundation
How to repair the laptop that cannot connect to the wireless network
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
随机推荐
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
Buuctf gold III
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
How to use etcd to realize distributed /etc directory
Go 语言怎么使用对称加密?
Dataframe gets the number of words in the string
SQL question brushing 586 Customers with the most orders
SystemVerilog-结构体(二)
Girls who want to do software testing look here
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
Concatenate strings to get the result with the smallest dictionary order
Comprehensively view the value of enterprise digital transformation
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
China carbon disulfide industry research and investment strategy report (2022 Edition)
How wild are hackers' ways of making money? CTF reverse entry Guide
SQL question brushing 1050 Actors and directors who have worked together at least three times