您的位置:宽带测速网 > 网络知识 > 怎么在redis中设置key的过期时间

怎么在redis中设置key的过期时间

2025-06-17 11:30来源:互联网 [ ]

在redis中设置key过期时间的方法:1.启动redis服务;2.登录redis数据库;3.执行命令设置过期时间;

具体步骤如下:

1.首先,在命令行中启动redis服务;

service redis start 

2.mysql服务启动后,输入用户名,登录redis数据库;

redis-cli.exe -h -p

3.登录到redis数据库后,在数据库中执行以下命令即可为key设置过期时间;

1)使用expire命令设置key的过期时间

#设置key的过期时间为10秒

expire key 10

2)使用setex命令为key赋值并设置过期时间

#设置key的值为value,过期时间为10秒

setex key 10 value