1. 程式人生 > >golang中使用redis

golang中使用redis

獲取redis

g

o get github.com/garyburd/redigo/redis

1、連線redis

package main
import (
    "fmt"
    "github.com/garyburd/redigo/redis"
)

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()
}

2、寫入

func saveToDB(c redis.Conn) {
    _, err := c.Do("SET", "name", "cjq", "EX", "60")
    if err != nil {
        fmt.Println("redis set failed:", err)
    } else {
        fmt.Println("save success")
    }
}

批量寫入 

_, err := c.Do("MSET", "name", "cjq", "SEX", "M", "EX", "70")
    if err != nil {
        fmt.Println("redis set failed:", err)
    } else {
        fmt.Println("save success")
    }


tips:EX是這個值的過期時間

這裡寫入的值永遠不會過期

package main

import (
    "fmt"
    "github.com/garyburd/redigo/redis"
)

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()

    _, err = c.Do("SET", "mykey", "laolao")
    if err != nil {
        fmt.Println("redis set failed:", err)
    }

    username, err := redis.String(c.Do("GET", "mykey"))
    if err != nil {
        fmt.Println("redis get failed:", err)
    } else {
        fmt.Printf("Get mykey: %v \n", username)
    }
}

3、讀取

func readFromDB(c redis.Conn) {
    username, err := redis.String(c.Do("GET", "name"))
    if err != nil {
        fmt.Println("redis get failed:", err)
    } else {
        fmt.Printf("Get mykey: %v \n", username)
    }

}

批量讀取

func readFromDB(c redis.Conn) {
    username, err := redis.Strings(c.Do("MGET", "SEX", "name"))
    if err != nil {
        fmt.Println("redis get failed:", err)
    } else {
        fmt.Printf("Get mykey: %v \n", username)
    }

}

4、刪除

func delFromDB(c redis.Conn) {
    _, err := c.Do("DEL", "name", "SEX")
    if err != nil {
        fmt.Println("redis delete failed:", err)
    } else {
        fmt.Println("delete success")
    }
}

5、讀寫json到redis

//寫json
func saveJsonDataToDB(c redis.Conn) {
    imap := map[string]string{"name": "laote", "phone": "13912345678"}
    value, _ := json.Marshal(imap)
    n, err := c.Do("SETNX", "jsonkey", value)
    if err != nil {
        fmt.Println(err)
    }
    if n == int64(1) {
        fmt.Println("success")
    }
}

//讀json
func readJsonFromDB(c redis.Conn) {
    var imapGet map[string]string
    valueGet, err := redis.Bytes(c.Do("GET", "jsonkey"))
    if err != nil {
        fmt.Println(err)
    }

    errShal := json.Unmarshal(valueGet, &imapGet)
    if errShal != nil {
        fmt.Println(err)
    }
    fmt.Println(imapGet["name"])
    fmt.Println(imapGet["phone"])
}

6、列表操作,存入一組資料
//存列表

func saveListToDB(c redis.Conn) {
    _, err := c.Do("lpush", "username", "zhangsan")
    if err != nil {
        fmt.Println("redis set failed:", err)
    }

    _, err = c.Do("lpush", "username", "lisi")
    if err != nil {
        fmt.Println("redis set failed:", err)
    }
    _, err = c.Do("lpush", "username", "wangwu")
    if err != nil {
        fmt.Println("redis set failed:", err)
    }
}

//讀列表
func readListFromDB(c redis.Conn) {
    values, _ := redis.Values(c.Do("lrange", "username", "0", "2"))
    fmt.Printf("count%d", len(values))
    for _, v := range values {
        fmt.Println(string(v.([]byte)))
    }
}