Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 的 NoSQL 数据库,并提供多种语言的 API。
使用Docker运行redis跑起来
首选,如果之前使用过redis容器,我们需要先remove掉之前的容器
docker rm redis
然后创建redis容器,并运行
docker run -itd --name redis -p 6379:6379 redis
进入redis容器中
docker exec -it redis /bin/bash
接着我们通过 redis-cli 连接测试使用 redis 服务
root@b18183382cc3:/data# redis-cli
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
Redis CRUD操作 字符串
set address 北京
setex指令 可以设置数据存在的时间, setex key second value
127.0.0.1:6379[1]> setex message 10 hello
OK
127.0.0.1:6379[1]> get message
"hello"
127.0.0.1:6379[1]> get message
"hello"
127.0.0.1:6379[1]> get message
"hello"
127.0.0.1:6379[1]> get message
(nil)
MSET 一次设置多个key-value
127.0.0.1:6379[1]> mset worker tom worker1 love
OK
127.0.0.1:6379[1]> get worker
"tom"
127.0.0.1:6379[1]> get worker1
MGET一次获取多个key-value
127.0.0.1:6379[1]> mget worker worker1
1) "tom"
2) "love"
Redis Hash操作
Redis hash 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象。
HSET
127.0.0.1:6379[1]> hset user name "Tom"
(integer) 1
127.0.0.1:6379[1]> hset user age 30
(integer) 1
127.0.0.1:6379[1]> hset user job "Golang Coder"
(integer) 1
HGET
127.0.0.1:6379[1]> HGET user name
"Tom"
127.0.0.1:6379[1]> HGET user job
"Golang Coder"
HGETALL
127.0.0.1:6379> hgetall user
1) "name"
2) "Tom"
3) "age"
4) "30"
5) "job"
6) "Golang Coder"
127.0.0.1:6379> hmset user1 name "Jack" Age 30 Job "Lover"
OK
127.0.0.1:6379> hgetall user1
1) "name"
2) "Jack"
3) "Age"
4) "30"
5) "Job"
6) "Lover"
127.0.0.1:6379> hmget user1 name Job Age
1) "Jack"
2) "Lover"
3) "30"
127.0.0.1:63
Hlen和hexist
127.0.0.1:6379> hlen user1u
(integer) 3
127.0.0.1:6379> HEXISTS user1 name
(integer) 1
127.0.0.1:6379> HEXISTS user1 name1
(integer) 0
List
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)List 有序 可重复
Lpush 和 Lrange
127.0.0.1:6379> lpush city Beijing Shanghai Tianjin
(integer) 3
127.0.0.1:6379> lrange city 0 -1
1) "Tianjin"
2) "Shanghai"
3) "Beijing"
127.0.0.1:6379> lrange city 0 3
1) "Tianjin"
2) "Shanghai"
3) "Beijing"
Lpop和Rpop 从链表取出并移走数据
127.0.0.1:6379> rpop city
"Tsingdao"
127.0.0.1:6379> lpop city
"Weihai"
127.0.0.1:6379> lrange city 0 -1
1) "Tianjin"
2) "Shanghai"
3) "Beijing"
删除链表所有数据 DEL
127.0.0.1:6379> del city
Set类型
字符串无序 不能重复
127.0.0.1:6379> sadd student love jack
(integer) 2
127.0.0.1:6379> smenmbers student
(error) ERR unknown command `smenmbers`, with args beginning with: `student`,
127.0.0.1:6379> smembers student
1) "jack"
2) "love"
127.0.0.1:6379> sadd student tom
(integer) 1
127.0.0.1:6379> smembers student
1) "tom"
2) "jack"
3) "love"
127.0.0.1:6379> sismember student jack
(integer) 1
127.0.0.1:6379> srem student jack
(integer) 1
127.0.0.1:6379> smembers student
1) "tom"
2) "love"
Go语言连接操作Redis
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
func main(){
//写入数据和读取数据
//1.connect
conn, err := redis.Dial("tcp","172.17.0.1:6379")
defer conn.Close()
if err != nil {
fmt.Println("Redis conn error",err)
return
}
fmt.Println("conn success")
//2.write data to redis string
_, err = conn.Do("Set", "person","baby")
if err != nil {
fmt.Println("Set error",err)
return
}
fmt.Println("Set Success")
//3. Read data from redis
message, err := redis.String(conn.Do("Get", "person"))//r interface
if err != nil {
fmt.Println("Get error",err)
return
}
fmt.Println("Sting Message:", message)
//Hash Set
_, err = conn.Do("HSet", "per","age", 18, "name","Tom")
if err != nil {
fmt.Println("HSet error",err)
return
}
//Hash Read
mess, err := redis.String(conn.Do("HGet", "per", "name"))
if err != nil {
fmt.Println("HGet error",err)
return
}
fmt.Println("Per Name:", mess)
mess1, err := redis.Int(conn.Do("HGet", "per", "age"))
if err != nil {
fmt.Println("HGet error",err)
return
}
fmt.Println("Per Age:", mess1)
//HMSET
_, err = conn.Do("HMSet", "stu","age", 21, "name","love")
if err != nil {
fmt.Println("HMSet error",err)
return
}
hashmess, err := redis.Strings(conn.Do("HMGet", "stu", "name", "age"))
if err != nil {
fmt.Println("HMGet error",err)
return
}
fmt.Println("Hash Per Name:", hashmess)
}
Redis连接池操作
从连接池中Get出一个conn连接
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
var pool *redis.Pool
//初始化连接池
func init(){
pool = & redis.Pool{
MaxIdle: 8,
MaxActive: 0,
IdleTimeout: 100,
Dial : func() (redis.Conn, error) {
return redis.Dial("tcp","172.17.0.1:6379")
},
}
}
func main(){
//先取出一个连接
conn := pool.Get()
defer conn.Close()
_,err := conn.Do("Set", "name", "Tom")
if err != nil{
fmt.Println("Error:", err)
return
}
r, err := redis.String(conn.Do("Get", "name"))
if err != nil{
fmt.Println("Error:", err)
return
}
fmt.Println("conn1 String=", r)
conn2 := pool.Get()
defer conn2.Close()
_,err = conn2.Do("Set", "name", "Love")
if err != nil{
fmt.Println("Error:", err)
return
}
message, err := redis.String(conn.Do("Get", "name"))
if err != nil{
fmt.Println("Error:", err)
return
}
fmt.Println("Conn2 String=", message)
}