本文主要介绍操作redis数据库的框架redigo,因此下面涉及到redis中的命令本文不做详解,下文通过使用redigo实现go语言代码操作redis,对应命令的作用及说明请看这篇文章,里面介绍了redis各数据类型常用的一些命令。
redigo框架是一款go语言操作redis数据库的框架,这个框架的使用非常的简单,只要知道redis中各种操作的命令即可快速掌握使用。
下载安装:go get github.com/gomodule/redigo/redis
代码中使用redis.String、redis.Bool、redis.Values来转换数据是因为c.Do执行了redis命令之后返回的是redis原生给出的数据(即在redis客户端返回什么数据这里就会接收到什么数据),像如果是字符串的话直接打印则每个字符以ascii码表示,bool值的话以0和1表示,因此需要对这些格式的数据做一次转化。
redis客户端连接
//redis客户端连接
func redisConnect()redis.Conn{
c,err := redis.Dial("tcp","127.0.0.1:6379")
if err != nil {
log.Fatal(err)
}
return c
}
string类型数据操作
//string类型数据命令
//redis命令:set key val
func set(key,val string){
_,err := c.Do("set",key,val)
if err != nil {
log.Fatal(err)
}
}
//redis命令:mset key1 val1 key2 val2 key3 val3 ...
func mset(key1,val1,key2,val2,key3,val3 string){
_,err := c.Do("mset",key1,val1,key2,val2,key3,val3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:get key
func get(key string){
val,err := redis.String(c.Do("get",key))
if err != nil {
log.Fatal(err)
}
fmt.Println(val)
}
//redis命令:mget key1 key2 key3 ...
func mget(key1,key2,key3 string){
vals,err := redis.Values(c.Do("mget",key1,key2,key3))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:del key1 key2 ...
func del(key1,key2,key3 string){
_,err := c.Do("del",key1,key2,key3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:getrange key start end
func getrange(key string,start,end int){
s,err := redis.String(c.Do("getrange",key,start,end))
if err != nil{
log.Fatal(err)
}
fmt.Println(s)
}
//redis命令:exists key
func exists(key string){
isExists,err := redis.Bool(c.Do("exists",key))
if err != nil {
log.Fatal(err)
}
fmt.Println(isExists)
}
//redis命令:setex key 10 val
func setex(key,val string,expire int){
_,err := c.Do("setex",key,expire,val)
if err != nil {
log.Fatal(err)
}
}
hash类型数据操作
//hash类型数据命令
//redis命令:hset hashTable key val
func hset(hashTable,key,val string){
_,err := c.Do("hset",hashTable,key,val)
if err != nil {
log.Fatal(err)
}
}
//redis命令:hmset hashTable key1 val1 key2 val2 key3 val3 ...
func hmset(hashTable,key1,val1,key2,val2,key3,val3 string){
_,err := c.Do("hmset",hashTable,key1,val1,key2,val2,key3,val3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:hget hashTable key
func hget(hashTable,key string){
val,err := redis.String(c.Do("hget",hashTable,key))
if err != nil {
log.Fatal(err)
}
fmt.Println(val)
}
//redis命令:hmget hashTable key1 key2 key3 ...
func hmget(hashTable,key1,key2,key3 string){
vals,err := redis.Values(c.Do("hmget",hashTable,key1,key2,key3))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:hdel hashTable key1 key2 key3
func hdel(hashTable,key1,key2,key3 string){
//批量删除一个或若干个hash表中的键值对,如果是一次性删除多个,只要至少删除掉一个即返回true
isDelSuccessful,err := redis.Bool(c.Do("hdel",hashTable,key1,key2,key3))
if err != nil {
log.Fatal(err)
}
fmt.Println(isDelSuccessful)
}
//redis命令:hgetall hashTable
func hgetall(hashTable string){
vals,err := redis.Values(c.Do("hgetall",hashTable))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:hkeys hashTable
func hkeys(hashTable string){
keys,err := redis.Values(c.Do("hkeys",hashTable))
if err != nil {
log.Fatal(err)
}
for k,v := range keys {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:hvals hashTable
func hvals(hashTable string){
vals,err := redis.Values(c.Do("hvals",hashTable))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:hlen hashTable
func hlen(hashTable string){
len,err := redis.Int(c.Do("hlen",hashTable))
if err != nil {
log.Fatal(err)
}
fmt.Println(len)
}
//redis命令:hexists hashTable
func hexists(hashTable,key string){
isExists,err := redis.Bool(c.Do("hexists",hashTable,key))
if err != nil {
log.Fatal(err)
}
fmt.Println(isExists)
}
list类型数据操作
//list类型数据命令
//redis命令:lpush list val1 val2 val3 ...
func lpush(list,val1,val2,val3 string){
_,err := c.Do("lpush",list,val1,val2,val3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:rpush list val1 val2 val3 ...
func rpush(list,val1,val2,val3 string){
_,err := c.Do("rpush",list,val1,val2,val3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:lpop list
func lpop(list string){
v,err := redis.String(c.Do("lpop",list))
if err != nil {
log.Fatal(err)
}
fmt.Println("删除列表首个元素并返回:" + v)
}
//redis命令:rpop list
func rpop(list string){
v,err := redis.String(c.Do("rpop",list))
if err != nil {
log.Fatal(err)
}
fmt.Println("删除列表末尾元素并返回:" + v)
}
//redis命令:lrem list count val
func lrem(list,val string,count int){
n,err := redis.Int(c.Do("lrem",list,count,val))
if err != nil {
log.Fatal(err)
}
fmt.Println(n)
}
//redis命令:lset list index val
func lset(list,val string,index int){
result,err := c.Do("lset",list,index,val)
if err != nil {
log.Fatal(err)
}
//成功修改则返回OK
fmt.Println(result)
}
//redis命令:lindex list index
func lindex(list string,index int){
v,err := redis.String(c.Do("lindex",list,index))
if err != nil {
log.Fatal(err)
}
fmt.Printf("列表%s中索引为%d对应的字符串为%s\n",list,index,v)
}
//redis命令:lrange list start end
func lrange(list string,start,end int){
vals,err := redis.Values(c.Do("lrange",list,start,end))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:llen list
func llen(list string){
len,err := redis.Int(c.Do("llen",list))
if err != nil {
log.Fatal(err)
}
fmt.Println(len)
}
//redis命令:ltrim list start end
func ltrim(list string,start,end int){
//这里加不加redis.String都可以,如果操作成功返回的都是字符串OK
result,err := redis.String(c.Do("ltrim",list,start,end))
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
无序集合set类型数据操作
//无序集合set数据类型命令
//redis命令:sadd myset val1 val2 val3 ...
func sadd(myset,val1,val2,val3 string){
_,err := c.Do("sadd",myset,val1,val2,val3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:srem myset val
func srem(myset,val string){
//c.Do删除成功返回1,然后用redis.Bool转换成bool值true
isDel,err := redis.Bool(c.Do("srem",myset,val))
if err != nil {
log.Fatal(err)
}
fmt.Println(isDel)
}
//redis命令:spop myset
func spop(myset string){
val,err := redis.String(c.Do("spop",myset))
if err != nil {
log.Fatal(err)
}
fmt.Println("随机删除一个元素并返回:" + val)
}
//redis命令:smembers myset
func smembers(myset string){
vals,err := redis.Values(c.Do("smembers",myset))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:scard myset
func scard(myset string){
len,err := redis.Int(c.Do("scard",myset))
if err != nil {
log.Fatal(err)
}
fmt.Printf("集合长度为:%d\n",len)
}
//redis命令:sismember myset val
func sismember(myset,val string){
isMember,err := redis.Bool(c.Do("sismember",myset,val))
if err != nil {
log.Fatal(err)
}
fmt.Println(isMember)
}
//redis命令:srandmember myset count
func srandmember(myset string,count int){
vals,err := redis.Values(c.Do("srandmember",myset,count))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:smove myset myset2 val
func smove(myset,myset2,val string){
isMoveSuccessful,err := redis.Bool(c.Do("smove",myset,myset2,val))
if err != nil {
log.Fatal(err)
}
fmt.Printf("从集合%s中移动元素%s到集合%s,移动结果:" + strconv.FormatBool(isMoveSuccessful) + "\n",myset,val,myset2)
}
//redis命令:sunion myset myset2 ...
func sunion(myset,myset2 string){
vals,err := redis.Values(c.Do("sunion",myset,myset2))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:sunionstore myset3 myset myset2 ...
func sunionstore(myset,myset2,myset3 string){
//将集合myset、myset2取并集后存入myset3集合
len,err := redis.Int(c.Do("sunionstore",myset3,myset,myset2))
if err != nil {
log.Fatal(err)
}
fmt.Printf("集合%s与集合%s取交集后得到的集合%s有%d个元素\n",myset,myset2,myset3,len)
//打印集合myset3验证结果
smembers("myset3")
}
//redis命令:sinter myset myset2 ...
func sinter(myset,myset2 string){
vals,err := redis.Values(c.Do("sinter",myset,myset2))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:sinterstore myset3 myset myset2 ...
func sinterstore(myset,myset2,myset3 string){
len,err := redis.Int(c.Do("sinterstore",myset3,myset,myset2))
if err != nil {
log.Fatal(err)
}
fmt.Printf("集合%s与集合%s取交集后得到的集合%s有%d个元素\n",myset,myset2,myset3,len)
//打印集合myset3验证结果
smembers("myset3")
}
//redis命令:sdiff myset myset2 ...
func sdiff(myset,myset2 string){
vals,err := redis.Values(c.Do("sdiff",myset,myset2))
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:sdiffstore myset3 myset myset2 ...
func sdiffstore(myset,myset2,myset3 string){
len,err := redis.Int(c.Do("sdiffstore",myset3,myset,myset2))
if err != nil {
log.Fatal(err)
}
fmt.Printf("集合%s与集合%s取交集后得到的集合%s有%d个元素\n",myset,myset2,myset3,len)
//打印集合myset3验证结果
smembers("myset3")
}
有序集合zset类型数据操作
//有序集合zset数据类型命令
//redis命令:zadd myzset val1 val2 val3 ...
func zadd(myzset,val1,val2,val3 string,score1,score2,score3 float64){
_,err := c.Do("zadd",myzset,score1,val1,score2,val2,score3,val3)
if err != nil {
log.Fatal(err)
}
}
//redis命令:zrem myzset val1 val2 val3 ...
func zrem(myzset,val1,val2,val3 string){
len,err := redis.Int(c.Do("zrem",myzset,val1,val2,val3))
if err != nil {
log.Fatal(err)
}
fmt.Printf("本次有%d个元素被成功删除\n",len)
}
//redis命令:zrange myzset start end [withscores]
func zrange(myzset string,start,end,flag int){
var vals []interface{}
var err error
if flag == 0 {
//不加withscores
vals,err = redis.Values(c.Do("zrange","myzset",start,end))
}else if flag == 1{
//加withscores
vals,err = redis.Values(c.Do("zrange","myzset",start,end,"withscores"))
}
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:zrevrange myzset start end [withscores]
func zrevrange(myzset string,start,end,flag int){
var vals []interface{}
var err error
if flag == 0 {
//不加withscores
vals,err = redis.Values(c.Do("zrevrange",myzset,start,end))
}else if flag == 1 {
//加withscores
vals,err = redis.Values(c.Do("zrevrange",myzset,start,end,"withscores"))
}
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:redis zrangebyscore myzset start end [withscores]
func zrangebyscore(myzset string,start,end,flag int){
var vals []interface{}
var err error
if flag == 0 {
//不加withscores
vals,err = redis.Values(c.Do("zrangebyscore",myzset,start,end))
}else if flag == 1 {
//加withscores
vals,err = redis.Values(c.Do("zrangebyscore",myzset,start,end,"withscores"))
}
if err != nil {
log.Fatal(err)
}
for k,v := range vals {
fmt.Printf("k = %v v = %s\n",k,v)
}
}
//redis命令:zcard myzset
func zcard(myzset string){
len,err := redis.Int(c.Do("zcard",myzset))
if err != nil {
log.Fatal(err)
}
fmt.Printf("有序集合%s中有%d个元素\n",myzset,len)
}
//redis命令:zcount myzset minscore maxscore
func zcount(myzset string,minscore,maxscore float64){
len,err := redis.Int(c.Do("zcount",myzset,minscore,maxscore))
if err != nil {
log.Fatal(err)
}
fmt.Printf("有序集合%s位于%.2f和%.2f分数区间内的元素有%d个\n",myzset,minscore,maxscore,len)
}
//redis命令:zrank myzset val
func zrank(myzset,val string){
index,err := redis.Int(c.Do("zrank",myzset,val))
if err != nil {
log.Fatal(err)
}
fmt.Printf("有序集合%s中的元素%s对应的索引为%d\n",myzset,val,index)
}
//redis命令:zscore myzset val
func zscore(myzset,val string){
score,err := redis.Float64(c.Do("zscore",myzset,val))
if err != nil {
log.Fatal(err)
}
fmt.Printf("有序集合%s中的元素%s对应的分数为%.2f\n",myzset,val,score)
}
想要操作redis就需要先了解redis中的命令即各命令的输出。从上面示例代码可以看出,redigo框架操作redis其实就是对redis的命令做拆解,然后用函数参数的形式在框架底层进行命令的构建和执行(从代码中可以看出主要是调用Do方法来执行命令,通过参数的形式来组装命令,可以看到Do里面的各参数连接起来其实就是一条完整的redis命令,因此只要熟悉redis命令就可以轻而易举地填入各参数),整体来说只要知道redis命令怎么用的话这个框架还是比较简单易用的。