学习-1
2020-04-07 本文已影响0人
NotFoundW
日志包使用
"github.com/yezihack/colorlog"
操作redis使用redigo包
"github.com/garyburd/redigo/redis"
在main函数中连接数据库,再调用其他函数,其他函数里实现学习的内容。
func main() {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
SingleKey(c)
SingleExpireKey(c)
SingleKeyExist(c)
SingleKeyDelete(c)
SinglePersistKey(c)
}
接下来其他函数里学习一些对单个Key的操作。
给单个key赋值,然后获取值。
Code
func SingleKey(c redis.Conn) {
colorlog.Info("Func OneKey...")
_, err := c.Do("SET", "FoxKey", "Fox")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
valueOfFoxKey, err := redis.String(c.Do("GET", "FoxKey"))
if err != nil {
colorlog.Error("redis get failed: " + err.Error())
} else {
fmt.Printf("Get FoxKey: %v \n", valueOfFoxKey)
}
}
Output
1.png给单个key设置过期时间并赋值,然后获取值。
Code
func SingleExpireKey(c redis.Conn) {
colorlog.Info("Func SingleExpireKey...")
_, err := c.Do("SET", "FoxExpireKey", "Fox", "EX", "2")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
valueOfFoxExpireKey, err := redis.String(c.Do("GET", "FoxExpireKey"))
if err != nil {
colorlog.Error("redis get failed: " + err.Error())
} else {
fmt.Printf("Get Value of Fox Expire Key: %v \n", valueOfFoxExpireKey)
}
colorlog.Debug("Waiting for the key expired")
time.Sleep(3 * time.Second)
valueOfFoxExpireKey, err = redis.String(c.Do("GET", "FoxExpireKey"))
if err != nil {
colorlog.Error("redis get failed: " + err.Error())
} else {
fmt.Printf("Get Value of Fox Expire Key %v \n", valueOfFoxExpireKey)
}
}
Output
2.png判断单个key是否存在
Code
func SingleKeyExist(c redis.Conn) {
colorlog.Info("Func SingleKeyExist...")
_, err := c.Do("SET", "KobeKey", "Kobe")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
isExist, _ := c.Do("EXISTS", "KobeKey")
if isExist == int64(1) {
fmt.Println("The KobeKey exists")
} else {
fmt.Println("The KobeKey doesn't exist")
}
isExist, _ = c.Do("EXISTS", "FakeKey")
if isExist == int64(1) {
fmt.Println("The FakeKey exists")
} else {
fmt.Println("The FakeKey doesn't exist")
}
}
Output
3.png删除单个key
Code
func SingleKeyDelete(c redis.Conn) {
colorlog.Info("Func SingleKeyDelete...")
_, err := c.Do("SET", "AllenKey", "Allen")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
del, err := c.Do("DEL", "AllenKey")
if err != nil {
colorlog.Error(err.Error())
} else {
if del == int64(1) {
fmt.Println("Allen key deleted successfully")
} else {
fmt.Println("Allen key deleted failed, key may doesn't exist")
}
}
del, err = c.Do("DEL", "TracyKey")
if err != nil {
colorlog.Error(err.Error())
} else {
if del == int64(1) {
fmt.Println("Tracy key deleted successfully")
} else {
fmt.Println("Tracy key deleted failed, key may doesn't exist")
}
}
}
Output
4.png持久化单个key
Code
func SinglePersistKey(c redis.Conn) {
colorlog.Info("Func SinglePersistKey...")
_, err := c.Do("SET", "PersistKey", "Vince", "EX", "10")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
valueOfPersistKey, err := redis.String(c.Do("GET", "PersistKey"))
if err != nil {
colorlog.Error("redis get failed: " + err.Error())
} else {
fmt.Println(valueOfPersistKey)
}
// make key persistent
result, err := c.Do("PERSIST", "PersistKey")
if err != nil {
colorlog.Error(err.Error())
} else if result == int64(1) {
fmt.Println("Persist key successfully")
} else if result == int64(0) {
fmt.Println("key persisted already")
}
colorlog.Debug("After 10 seconds, the key will not expire, we can still get the value")
time.Sleep(10 * time.Second)
valueOfPersistKey, err = redis.String(c.Do("GET", "PersistKey"))
if err != nil {
colorlog.Error("redis get failed: " + err.Error())
} else {
fmt.Println(valueOfPersistKey)
}
}