美文网首页
Redis笔记

Redis笔记

作者: 也许________ | 来源:发表于2019-09-25 15:16 被阅读0次

Redis启动方式有3种
①默认启动
②带配置的启动
③以配置文件启动(推荐)

默认启动命令
redis-server

带配置的启动
redis-server --configKey configValue --configKey2 configValue2
例:redis-server --port 8888

配置文件启动(redis.conf 中具有redis的所有配置信息)
redis-server /opt/redis/redis.conf

命令

添加值
set key value 例:set hello world

取值
get key 例:get hello

删除值
del key 例:del hello

设置过期时间
expire key sec 例:set hello 5

查看过期时间
ttl key 例:ttl hello
返回值 -1 表示没设置过期时间
返回值 -2 表示键不存在
返回值>=0 表示剩余时间

添加值并且设置过期时间
set key value ex sec 例:set hello world ex 5
给key值"hello" 设置value值“world”,并且设置过期时间5秒

当key存在时,才能添加或更新值
set key value xx 例:set hello world xx

当key不存在时,才能添加值
set key value nx 例:set hello world nx

另外两个命令用于添加值
setex key sec value 例:set hello 5 world
给key“hello” 设置 5秒过期时间,并且value为"world"

只有key不存在的时候,能够添加值
setnx key value 例:setnx hello world

配置文件需要修改的地方
  • daemonize 改为yes,表示后台运行redis
  • bind 0.0.0.0,表示允许任何主机连接redis
  • requirepass 设置密码,增加一道防线,保证数据的安全性

参考文章
https://blog.csdn.net/q1035331653/article/details/79077260
https://www.cnblogs.com/xinhuaxuan/category/963844.html

相关文章

网友评论

      本文标题:Redis笔记

      本文链接:https://www.haomeiwen.com/subject/xrvnixtx.html