redis
NoSQL
特点
不支持 SQL 语法
key-value形式存储数据
没有通用的语言
适用场景与SQL不同
redist安装
下载安装包:http://www.redis.cn/download.html
解压压缩包:tar -zxvf redis-6.0.6.tar.gz
编译安装需要gcc 并且gcc版本不能太低
gcc升级:yum -y install centos-release-scl && yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils && scl enable devtoolset-9 bash
(base) [root@CentOS-wangml install]# mv redis-6.0.6/ /usr/local/
(base) [root@CentOS-wangml install]# cd /usr/local/
(base) [root@CentOS-wangml local]# ls
bin etc games include lib lib64 libexec qcloud redis-6.0.6 sbin share src yd.socket.server
(base) [root@CentOS-wangml local]# cd redis-6.0.6/
(base) [root@CentOS-wangml redis-6.0.6]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md
(base) [root@CentOS-wangml redis-6.0.6]# make
......
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/usr/local/redis-6.0.6/src'
相关链接
redis下载安装以及后续设置:https://my.oschina.net/u/3986411/blog/4715948?hmsr=kaifa_aladdin
启动redis客户端以及退出
客户端启动程序/usr/local/redis-6.0.6/src/redis-cli 可以将该路径创建一个redis的软连接
(base) [root@CentOS-wangml study]# redis
127.0.0.1:6379> quit
(base) [root@CentOS-wangml study]# /usr/local/redis-6.0.6/src/redis-cli
127.0.0.1:6379> quit
redis数据库
默认创建了16个数据库,编号0-15
选择数据库使用select Num命令
127.0.0.1:6379> select 9
OK
数据库操作
set
设置key-value
127.0.0.1:6379> set name Balabal
OK
127.0.0.1:6379> get name
"Balabal"
设置多个key-value
127.0.0.1:6379> mset name Bao age 12
OK
127.0.0.1:6379> get Bao
(nil)
127.0.0.1:6379> get name
"Bao"
127.0.0.1:6379> get age
"12"
错误处理
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
OK
设置key-value以及存在时效
127.0.0.1:6379> setex class 10 1
OK
127.0.0.1:6379> get class
"1"
127.0.0.1:6379> get class
"1"
... 10秒后
127.0.0.1:6379> get class
(nil)
获取多个值
127.0.0.1:6379> mget name age
Bob123哈哈哈
12
append
追加值,返回追加后长度
127.0.0.1:6379> set name BobOK127.0.0.1:6379> get name"Bob"127.0.0.1:6379> append name 123(integer) 6127.0.0.1:6379> get name"Bob123"
中文乱码问题
127.0.0.1:6379> append name 哈哈哈(integer) 15127.0.0.1:6379> get name"Bob123xe5x93x88xe5x93x88xe5x93x88"(base) [root@CentOS-wangml study]# redis --raw127.0.0.1:6379> get nameBob123哈哈哈
key操作
删除的key 就无法再获取对应值
127.0.0.1:6379> keys *agename127.0.0.1:6379> mset a1 1 a2 2 a3 3OK127.0.0.1:6379> keys *a2a1nameagea3127.0.0.1:6379> keys a*a2a1agea3127.0.0.1:6379> exists class0127.0.0.1:6379> exists a11127.0.0.1:6379> exists a*0127.0.0.1:6379> del a11127.0.0.1:6379> keys *a2nameagea3127.0.0.1:6379> del a1 a2 a3 a42 成功删除的key数量
expire
设置key的存在时间
127.0.0.1:6379> keys *nameage127.0.0.1:6379> expire age 101127.0.0.1:6379> get age12127.0.0.1:6379> get age127.0.0.1:6379> keys *name
ttl
查看key还可以存活多久,返回正数表示还可以存活的秒数,-2表示key不存在,-1表示一直存活
127.0.0.1:6379> mset a1 1 a2 2 a3 3OK127.0.0.1:6379> expire a1 1001127.0.0.1:6379> ttl a194127.0.0.1:6379> ttl a188127.0.0.1:6379> expire a1 31127.0.0.1:6379> ttl a12127.0.0.1:6379> ttl a1-2127.0.0.1:6379> ttl a1-2127.0.0.1:6379> ttl a2-1
hash值
设置hash值
127.0.0.1:6379> hset b1 name Bobob1127.0.0.1:6379> get nameBob123哈哈哈127.0.0.1:6379> get b1WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379> type namestring127.0.0.1:6379> type b1hash127.0.0.1:6379> keys *a2b1a3name127.0.0.1:6379> hset b1 age 291127.0.0.1:6379> hset b1 class 1011127.0.0.1:6379> hget b1 nameBobob127.0.0.1:6379> hget b1 class101
获取hash的keys-values
127.0.0.1:6379> hkeys b11) "name"2) "age"3) "class"127.0.0.1:6379> hvals b11) "Bobob"2) "29"3) "101127.0.0.1:6379> hmget b1 name age class1) "Bobob"2) "29"3) "101"
删除hash中的的行
127.0.0.1:6379> hdel b1 name(integer) 1127.0.0.1:6379> hkeys b11) "age"2) "class"
list类型
list的插入和读取
127.0.0.1:6379> lpush c1 a1 a2 a3 a1 a4(integer) 5127.0.0.1:6379> keys *1) "c1"127.0.0.1:6379> lrange c1 0 51) "a4"2) "a1"3) "a3"4) "a2"5) "a1"127.0.0.1:6379> rpush c2 a1 a2 a3 a4(integer) 4127.0.0.1:6379> lrange c2 0 4 # 从左到右 第0个到第3个元素1) "a1"2) "a2"3) "a3"4) "a4"127.0.0.1:6379> type c1list127.0.0.1:6379> lrange c2 0 -11) "a1"2) "a2"3) "a3"4) "a4"
删除list元素
127.0.0.1:6379> lpush c3 a1 a1 a2 a3 a1 a2 a3(integer) 7127.0.0.1:6379> lrange c3 0 -11) "a3"2) "a2"3) "a1"4) "a3"5) "a2"6) "a1"7) "a1"127.0.0.1:6379> lrem c3 1 a1 # 从左到右 删除遇到的 1 个 a1(integer) 1127.0.0.1:6379> lrange c3 0 -11) "a3"2) "a2"3) "a3"4) "a2"5) "a1"6) "a1"127.0.0.1:6379> lrem c3 -1 a2 # 从右到左 删除遇到的 1 个 a2(integer) 1127.0.0.1:6379> lrange c3 0 -11) "a3"2) "a2"3) "a3"4) "a1"5) "a1"
插入元素
127.0.0.1:6379> lrange c3 0 -11) "a3"2) "a2"3) "a3"4) "a1"5) "a1"127.0.0.1:6379> linsert c3 before a2 a4(integer) 6127.0.0.1:6379> lrange c3 0 -11) "a3"2) "a4"3) "a2"4) "a3"5) "a1"6) "a1"127.0.0.1:6379> linsert c3 after a4 a5(integer) 7127.0.0.1:6379> lrange c3 0 -11) "a3"2) "a4"3) "a5"4) "a2"5) "a3"6) "a1"7) "a1"
set
set集合,集合中无重复数据
无序
sadd向集合添加数据 返回添加的数据个数 重复的数据只添一次
127.0.0.1:6379> sadd bb1 a1 a2 a3 a1 a2 a4(integer) 4
smembers查看集合中数据
127.0.0.1:6379> smembers bb11) "a4"2) "a2"3) "a1"4) "a3"
**srem删除集合元素
127.0.0.1:6379> srem bb1 a1(integer) 1127.0.0.1:6379> smembers bb11) "a4"2) "a2"3) "a3"
有序
127.0.0.1:6379> zadd cc1 2 a1 4 a2 1 a3 # 有序 添加权重和值
(integer) 3
127.0.0.1:6379> zrange cc1 0 -1 # 按权重从小到大排序返回
1) "a3"
2) "a1"
3) "a2"
127.0.0.1:6379> zrangebyscore cc1 2 4 # 返回指定权重区间内的值[2, 4]
1) "a1"
2) "a2"
127.0.0.1:6379> zadd cc1 16 b1 22 b2 10 b3 6 b4
(integer) 4
127.0.0.1:6379> zrange cc1 0 -1
1) "a3"
2) "a1"
3) "a2"
4) "b4"
5) "b3"
6) "b1"
7) "b2"
127.0.0.1:6379> zrangebyscore cc1 10 20
1) "b3"
2) "b1"
127.0.0.1:6379> zremrangebyscore cc1 10 20 # 按权重删除
(integer) 2
127.0.0.1:6379> zrange cc1 0 -1
1) "a3"
2) "a1"
3) "a2"
4) "b4"
5) "b2"
Go 操作redis
Go中文文档https://www.bookstack.cn/read/topgoer/d130e7abc85f1c4c.md
goredis文档https://pkg.go.dev/github.com/gomodule/redigo/redis
所需包的安装
package main
// go与redis交互 相关包安装 go get github.com/gomodule/redigo/redis
// 测试上述包是否安装成功
import (
"github.com/gomodule/redigo/redis"
)
func main() {
conn, _ := redis.Dial("tcp", ":6379")
defer conn.Close()
conn.Do("set", "c1", "hello")
}
/*
上述代码执行成功后
(base) [root@CentOS-wangml redis]# redis
127.0.0.1:6379> get c1
"hello"
127.0.0.1:6379>
*/
操作
连接数据库
Dial(network, address string) (conn, err)
// conn, _ := redis.Dial("tcp", ":6379")
执行数据库操作命令
Send(commandName string, args ...interface{}) err// 执行命令
Flush()// 刷新数据库
Receive()// 接收返回值
package main
import (
"github.com/gomodule/redigo/redis"
"fmt"
)
func main() {
conn, _ := redis.Dial("tcp", ":6379")
defer conn.Close()
conn.Send("get", "c1")// 将此条放在上面
conn.Send("set", "aaa", "cccc")
//conn.Send("get", "c1")
conn.Flush()
rel, err := conn.Receive()// 返回值仅接收第一条命令的执行结果
/*
127.0.0.1:6379> get aaa
"cccc"
*/
if err != nil {
fmt.Println("Receive error:", err)
}
//fmt.Println("c1:", rel)// c1: OK 接收到conn.Send("set", "aaa", "cccc")
fmt.Println("c1:", rel)// c1: [104 101 108 108 111] 返回的时 c1:hello hello的每个字母的字节码
}
package main
import (
"github.com/gomodule/redigo/redis"
"fmt"
)
func main() {
conn, _ := redis.Dial("tcp", ":6379")
defer conn.Close()
//rel, err := conn.Do("get", "c1")// 执行命令 刷新缓冲区 返回结果 将Send Flush Receive结合一步完成
// 将字节码转为string
rel, err := redis.String(conn.Do("get", "c1"))
if err != nil {
fmt.Println("Receive error:", err)
}
//fmt.Println("c1:", rel)// c1: [104 101 108 108 111]
fmt.Println("c1:", rel)// c1: hello
// 127.0.0.1:6379> mset name Bob age 11 class 101
// OK
//rel2, err := redis.Strings(conn.Do("mget", "name", "age", "class"))
rel2, err := redis.Values(conn.Do("mget", "name", "age", "class"))
var name string
var age int
var class string
redis.Scan(rel2, &name, &age, &class)
if err != nil {
fmt.Println("Receive error:", err)
}
//fmt.Println(rel2)// [Bob 11 101]
fmt.Println("name:", name, "age:", age, "class:", class)// name: Bob age: 11 class: 101
}
转载请注明出处
最新评论