32.Go语言·Redis

ReadME.md

#./bin/redis-cli -h 127.0.0.1  -p 6379 -a [email protected]
#Redis的数据类型(http://redisdoc.com/index.html):
    String(字符串)
        set/get/del
    Hash(哈希)

    List(列表)
    Set(集合)
    zset(有序集合)

github地址: 
https://github.com/garyburd/redigo

文档地址: 
http://godoc.org/github.com/garyburd/redigo/redis

获取:
go get github.com/garyburd/redigo/redis


github地址: 
https://github.com/go-redis/redis

文档地址: 
https://godoc.org/github.com/go-redis/redis

获取:
go get -u github.com/go-redis/redis

main01.go

package main

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

func main() {
    // 1.连接redis
    conn, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialPassword("[email protected]"))
    if err != nil {
        fmt.Println(err)
        return
    }

    defer conn.Close()
    // 2.通过go,向redis写入数据 string [key-val]
    _, err = conn.Do("Set", "name", "IloveUer!")
    if err != nil {
        fmt.Println(err)
        return
    }

    // 3.通过go 向redis读取数据  string [key-val]

    val, err := redis.String(conn.Do("Get", "name"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(val)
}

main02.go

package main

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

func main() {
    // 1.连接redis
    conn, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialPassword("[email protected]"))
    if err != nil {
        fmt.Println(err)
        return
    }

    defer conn.Close()
    // 2.通过go,向redis写入数据 hash
    // _, err = conn.Do("HSet", "student", "name", "Uzi")
    _, err = conn.Do("HMSet", "student", "name", "Uzi", "age", 18, "address", "成都")
    if err != nil {
        fmt.Println(err)
        return
    }

    // 3.通过go 向redis读取数据

    // val, err := redis.String(conn.Do("HGet", "student", "name"))
    val, err := redis.Strings(conn.Do("HMGet", "student", "name", "age", "address"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(val)
}

redis连接池.redis.go

package main

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

var content string = `
————————————————Go语言·redis连接池————————————————————

`

var pool *redis.Pool

func init() {
    pool = &redis.Pool{
        MaxIdle:     8,   //最大空闲链接数
        MaxActive:   0,   //表示和数据库的最大连接数,0表示没有限制
        IdleTimeout: 100, //最大空闲时间
        Dial: func() (redis.Conn, error) {
            // 初始化连接
            return redis.Dial("tcp", "127.0.0.1:6379", redis.DialPassword("[email protected]"))
        },
    }
}

func main() {

    conn := pool.Get()

    defer conn.Close()

    _, err := conn.Do("HMSet", "student", "name", "Uzi", "age", 18, "address", "成都")
    if err != nil {
        fmt.Println(err)
        return
    }

    val, err := redis.Strings(conn.Do("HMGet", "student", "name", "age", "address"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(val)

    pool.Close()

    conn2 := pool.Get()
    value, err := redis.Strings(conn2.Do("HMGet", "student", "name", "age", "address"))
    if err != nil {
        // conn2.Do: redigo: get on closed pool
        fmt.Println("conn2.Do:",err)
        return
    }
    fmt.Println(value)
}

你可能感兴趣的:(32.Go语言·Redis)