Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
JavaDevTeam
notes
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
notes
/
Redis
/
Redis-key的操作.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
61 lines (60 loc) · 1.9 KB
master
Breadcrumbs
notes
/
Redis
/
Redis-key的操作.java
Copy path
Top
File metadata and controls
Code
Blame
61 lines (60 loc) · 1.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-----------------------
Redis
-
key操作
|
-----------------------
*
key的命名规则
*
啥都可以
,
不能是换行
,
空格
*
尽量短
,
也可以自己定义key的格式
object
-
type
:
id
:
field
set
key
value
*
存储一个数据到内存
mset
key
value
key
value
key
value
..
*
可以一次性存储多个key
value
msetnx
key
value
key
value
...
*
如果里面有一个key是已经存在的
,
那么不会执行任何的插入动作
getset
key
newvalue
*
给指定的key设置新的值
,
并且会返回原始值
get
key
*
从内存中获得数据
mget
key
key
key
...
*
从内存中获取多个指定key的数据
exists
key
*
测试指定的Key是否存在
del
key1
key2
...
keyn
*
删除指定的key
keys
pattern
*
返回指定匹配模式的所有key
,
注意
,
返回的是key
*
pattern
,
有点像正则
.
但不是
rename
oldkey
newkey
*
重命名key
dbsize
*
返回当前数据库的key数量
expire
key
seconds
*
为key指定过期的时间
*
设置成功后返回
1
ttl
key
*
返回key剩余的过期秒数
*
如果数据存在
,
但是没有设置过期时
,
间返回
-
1
*
数据已经不存在
,
返回
-
2
select
db
-
index
*
选择数据库
(
通过下标选择
)
*
默认是16个数据库
,
可以通过修改配置文件来自定义
*
大约在84行
:
databases
16
move
key
db
-
index
*
把key从当前数据库移到指定的数据库
flushdb
*
清空当前数据库中的所有key
flushall
*
清空所有数据库中的所有key
strlen
key
*
返回指定key的value的长度
set
命令还支持可选的NX选项和XX选项
*
如果添加了NX选项
,
那么set操作的时候会判断key值是否存在
,
如果存在那么不会做覆盖操作
.
如果不存在
,
那么就会正常存入
*
如果添加了XX选项
,
那么set操作的时候会判断key值是否存在
,
如果存在
,
那么就执行操作进行覆盖
,
如果不存在
,
那么不会执行任何操作
*
添加了NX
/
XX选项的命令
,
执行成功返回ok
,
失败返回nil
*
例
:
*
set
name
Kevin
NX
//name这个key如果已经存在.那么就不会进行任何操作
*
set
name
Kevin
XX
//name这个key必须是存在的.才会进行存储覆盖.反之不会做任何的操作
*
针对于防止覆盖
,
其实还有一种写法
*
SETNX
key
value
;
//同样,如果key已经存在那么不会执行写入操作,设置失败返回0,成功返回1
*
MSETNX
key
value
key
value
...
*
如果有一个key是存在的
,
那么批量的插入都不会执行
You can’t perform that action at this time.