继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

GO(9[TCP/Redis/原子操作])

holdtom
关注TA
已关注
手记 1846
粉丝 240
获赞 991


Tcp编程

.....

Redis

使⽤第三⽅开源的redis库: github.com/garyburd/redigo/redis

import(


“github.com/garyburd/redigo/redis"

)

9. 链接redis

    

redis

package main

import (

"fmt"

"github.com/garyburd/redigo/redis"

)

func main() {

    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

    fmt.Println("conn redis failed,", err)

    return

    }

    defer c.Close()

}

10. Set 接⼝

package main

import (

"fmt"

"github.com/garyburd/redigo/redis"

)

func main() {

    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

    fmt.Println("conn redis failed,", err)

    return

    }

    defer c.Close()

    _, err = c.Do("Set", "abc", 100)

    if err != nil {

    fmt.Println(err)

    return

    }

    r, err := redis.Int(c.Do("Get", "abc"))

    if err != nil {

    fmt.Println("get abc failed,", err)

    return

    }

    fmt.Println(r)

}

11. Hash表

    

package main

import (

"fmt"

"github.com/garyburd/redigo/redis"

)

func main() {

    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

    fmt.Println("conn redis failed,", err)

    return

    }

    defer c.Close()

    _, err = c.Do("HSet", "books", "abc", 100)

    if err != nil {

    fmt.Println(err)

    return

    }

    r, err := redis.Int(c.Do("HGet", "books", "abc"))

    if err != nil {

    fmt.Println("get abc failed,", err)

    return

    }

    fmt.Println(r)

}

11. 批量Set

    

package main

import (

"fmt"

"github.com/garyburd/redigo/redis"

)

func main() {

    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

    fmt.Println("conn redis failed,", err)

    return

    }

    defer c.Close()

    _, err = c.Do("MSet", "abc", 100, "efg", 300)

    if err != nil {

    fmt.Println(err)

    return

    }

    r, err := redis.Ints(c.Do("MGet", "abc", "efg"))

    if err != nil {

    fmt.Println("get abc failed,", err)

    return

    }

    for _, v := range r {

    fmt.Println(v)

    }

}

11. 过期时间

    

package main

import (

"fmt"

"github.com/garyburd/redigo/redis"

)

func main() {

    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

    fmt.Println("conn redis failed,", err)

    return

    }

    defer c.Close()

    _, err = c.Do("expire", "abc", 10)

    if err != nil {

    fmt.Println(err)

    return

    }

}

12. 队列操作

    

package main

import (

"fmt"

"github.com/garyburd/redigo/redis"

)

func main() {

    c, err := redis.Dial("tcp", "localhost:6379")

    if err != nil {

    fmt.Println("conn redis failed,", err)

    return

    }

    defer c.Close()

    _, err = c.Do("lpush", "book_list", "abc", "ceg", 300)

    if err != nil {

    fmt.Println(err)

    return

    }

r, err := redis.String(c.Do("lpop", "book_list"))

    if err != nil {

    fmt.Println("get abc failed,", err)

    return

    }

fmt.Println(r)

©著作权归作者所有:来自51CTO博客作者zhaichaoqun的原创作品,如需转载,请注明出处,否则将追究法律责任


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP