查看key的过期时间
Code
func SingleKeyTtl(c redis.Conn) {
colorlog.Info("Func SingleKeyTtl()...")
_, err := c.Do("SET", "ttlKey", "Dunk", "EX", "360")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
time.Sleep(3 * time.Second)
// get the remaining time(seconds)
remainSeconds, err := c.Do("TTL", "ttlKey")
if err != nil {
colorlog.Error(err.Error())
} else {
fmt.Println("ttlKey remains seconds:", remainSeconds)
}
// get the remaining time(milliseconds)
remainMilliSeconds, err := c.Do("PTTL", "ttlKey")
if err != nil {
colorlog.Error(err.Error())
} else {
fmt.Println("ttlKey remains milliseconds:", remainMilliSeconds)
}
// Once persist key, ttl and pttl will return -1
result, err := c.Do("PERSIST", "ttlKey")
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")
}
// get the remaining time(seconds)
remainSeconds, err = c.Do("TTL", "ttlKey")
if err != nil {
colorlog.Error(err.Error())
} else {
fmt.Println("ttlKey remains seconds:", remainSeconds)
}
// get the remaining time(milliseconds)
remainMilliSeconds, err = c.Do("PTTL", "ttlKey")
if err != nil {
colorlog.Error(err.Error())
} else {
fmt.Println("ttlKey remains milliseconds:", remainMilliSeconds)
}
// If check the remaining time of key that doesn't exist, ttl and pttl will return -2
// get the remaining time(seconds)
remainSeconds, err = c.Do("TTL", "fakeTtlKey")
if err != nil {
colorlog.Error(err.Error())
} else {
fmt.Println("fakeTtlKey remains seconds:", remainSeconds)
}
// get the remaining time(milliseconds)
remainMilliSeconds, err = c.Do("PTTL", "fakeTtlKey")
if err != nil {
colorlog.Error(err.Error())
} else {
fmt.Println("fakeTtlKey remains milliseconds:", remainMilliSeconds)
}
}
Output

1.png
用RENAME命令修改key
Code
func SingleKeyRename(c redis.Conn) {
colorlog.Info("Func SingleKeyRename()...")
// Rename an existing key
_, err := c.Do("SET", "OldKey", "ray")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
_, err = c.Do("RENAME", "OldKey", "NewKey")
if err != nil {
colorlog.Error(err.Error())
return
}
isExist, _ := c.Do("EXISTS", "OldKey")
fmt.Println("OldKey doesn't exist:", isExist)
isExist, _ = c.Do("EXISTS", "NewKey")
fmt.Println("NewKey exist:", isExist)
// Rename a key that does not exist
_, err = c.Do("RENAME", "OldKey", "MoreNewKey")
if err != nil {
colorlog.Error(err.Error())
}
// Rename an existing key to an another existing key by RENAME command
_, err = c.Do("SET", "whiteBird", "white")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
_, err = c.Do("SET", "blackBird", "black")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
// value of whiteBird will cover value of blackBird. And whiteBird key will disappear.
_, err = c.Do("RENAME", "whiteBird", "blackBird")
if err != nil {
colorlog.Error(err.Error())
return
}
valueOfBlackBird, _ := redis.String(c.Do("GET", "blackBird"))
fmt.Println("Get value of blackBird:", valueOfBlackBird)
}
Output

2.png
用RENAMENX命令修改key
Code
func SingleKeyRenamenx(c redis.Conn) {
colorlog.Info("Func SingleKeyRenamenx()...")
// Rename an existing key
_, err := c.Do("SET", "nx1", "wade")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
_, err = c.Do("SET", "nx2", "wade")
if err != nil {
colorlog.Error("redis set failed: " + err.Error())
return
}
// RENAMENX command only can rename an existing key to a key that doesn't exist
res1, err := c.Do("RENAMENX", "nx1", "nx2")
if err != nil {
colorlog.Error(err.Error())
}
// if no error, here will get 0, means rename failed.
fmt.Println("The result is ",res1)
res2, err := c.Do("RENAMENX", "nx1", "nx3")
if err != nil {
colorlog.Error(err.Error())
}
// if no error, here will get 1, means rename successfully.
fmt.Println("The result is ",res2)
// RENAMENX also can not rename a key that doesn't exist
_, err = c.Do("RENAMENX", "fakeNx", "newFakeNx")
if err != nil {
colorlog.Error(err.Error())
}
}
Output

image.png
查看单个key的value的type
Code
func SingleValueOfKeyType(c redis.Conn) {
colorlog.Info("Func SingleValueOfKeyType()...")
// To save space, ignore the set error
c.Do("SET", "ball", "don't lie")
c.Do("SET", "soccer", 99)
c.Do("SET", "NBA", false)
c.Do("SET", "complex", 3.1415)
str := "hello"
by := []byte(str)
c.Do("SET", "tree", by)
// get type of value-> all results should be string. Because string is the basic data type of redis.
re1, _ := c.Do("TYPE", "ball")
fmt.Println(re1)
re2, _ := c.Do("TYPE", "soccer")
fmt.Println(re2)
re3, _ := c.Do("TYPE", "NBA")
fmt.Println(re3)
re4, _ := c.Do("TYPE", "complex")
fmt.Println(re4)
re5, _ := c.Do("TYPE", "tree")
fmt.Println(re5)
// if key doesn't exist, will return none.
re6, _ := c.Do("TYPE", "nothing")
fmt.Println(re6)
}
Output

4.png
随意返回一个key
Code
func RandomKey(c redis.Conn) {
colorlog.Info("Func SingleValueOfKeyType()...")
key, err := redis.String(c.Do("RANDOMKEY"))
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println(key)
}
Output

5.png
查找所有符合给定模式( pattern)的 key
Code
func PatternKeys(c redis.Conn) {
colorlog.Info("Func PatternKeys()...")
// To keep things short, do not handle set errors.
c.Do("SET", "computer1", "c1")
c.Do("SET", "computer2", "c2")
c.Do("SET", "computer3", "c3")
results, err := redis.Strings(c.Do("KEYS", "computer*"))
if err != nil {
colorlog.Error(err.Error())
return
}
for _, k := range results {
fmt.Println(k)
}
}
Output

6.png
使用SETNX命令设定一个全新的key
Code
func SingleKeySetNx(c redis.Conn) {
colorlog.Info("Func PatternKeys()...")
// SETNX command only set a key which doesn't exist
c.Do("SET", "bxj1", "1")
result, err := c.Do("SETNX", "bxj1", "2")
if err != nil {
colorlog.Error(err.Error())
return
}
// here will be failed, result is 0
fmt.Println(result)
// and value of bxj1 still is 1
val, _ := redis.Int(c.Do("GET", "bxj1"))
fmt.Println(val)
// SETNX can set a key which doesn't exist(assuming that this NewestKey doesn't exist)
resultNew, err := c.Do("SETNX", "NewestKey", "24")
if err != nil {
colorlog.Error(err.Error())
return
}
// here will be successful, result is 1
fmt.Println(resultNew)
// and value of NewestKey is 24
val, _ = redis.Int(c.Do("GET", "NewestKey"))
fmt.Println(val)
}
Output

7.png
使用SETEX命令设定会过期的key
Code
func SingleKeySetEx(c redis.Conn) {
colorlog.Info("Func SingleKeySetEx()...")
_, err := c.Do("SETEX", "bingo", "20", "3000")
if err != nil {
colorlog.Error(err.Error())
return
}
time.Sleep(3 * time.Second)
remainedTime, _ := c.Do("TTL", "bingo")
fmt.Println(remainedTime)
}
Output

8.png
网友评论