golang 插入redis 集合,并判断元素是否存在

代码如下:
1. "server/service" 是当前的报名
2. 用的go-redis这个库
 
判断元素是否在集合中:

conn.SIsMember("User", sign).Result()

完整代码:
package cache

import (
"server/service"
"fmt"
"github.com/go-redis/redis"
)

type Cache struct {
conn *redis.Client
}

func (this *Cache) CacheInit() {
this.connect()

}
func (this *Cache) connect() {
conf := service.ReadRedisConfig()
this.conn = redis.NewClient(&redis.Options{
Addr: conf.Addr,
Password: conf.Password,
DB: conf.DB,
})

}
func (this *Cache) Get(id string) (string, bool) {
result, err := this.conn.Get(id).Result()
if err != nil {
fmt.Println(err)
return "", false
}
return result, true
}

func (this *Cache) Set(id string, content string) bool {
_, err := this.conn.Set(id, content, 0).Result()
if err != nil {
fmt.Println(err)
return false
} else {
return true
}
}

func (this *Cache) CheckUserExist(sign string) bool {
result, err := this.conn.SIsMember("User", sign).Result()
if err != nil {
fmt.Println(err)
return false
}
return result
}

func (this *Cache) AddUser(name string) bool {
_, err := this.conn.SAdd("User", name).Result()
if err != nil {
fmt.Println(err)
return false
} else {
return true
}

}

github地址:
https://github.com/Rockyzsu/BondInfoServer.git
 

0 个评论

要回复文章请先登录注册