redigo使用

手册地址:http://godoc.org/github.com/garyburd/redigo/redis

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

获取地址,进入GOPATH使用go get下载包

go get github.com/garyburd/redigo/redis

连接redis读写数据

package main

import (
"fmt" "github.com/garyburd/redigo/redis"
) func main() {
conn, err := redis.Dial("tcp", "localhost:6379") if err != nil {
fmt.Println(err)
return
} _, err = conn.Do("set", "name", "golang redis test") if err != nil {
fmt.Println(err)
}
}

从redis中读出数据

package main

import (
"fmt" "github.com/garyburd/redigo/redis"
) func rr(conn redis.Conn, key string) {
line, err := redis.String(conn.Do("get", key)) if err != nil {
fmt.Println(err)
}
fmt.Println(line)
} func main() { conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println(err)
return
} rr(conn, "name") }

注意:写入的值永远不会过期

设置key过期时间

func wr(conn redis.Conn, key, val interface{}, time int) {
line, err := conn.Do("set", key, val, "EX", time) if err != nil {
fmt.Println(err)
}
fmt.Println(line)
} wr(conn, "name", "zhangsan", 120)

golang使用redis的更多相关文章

  1. go语言之行--golang操作redis、mysql大全

    一.redis 简介 redis(REmote DIctionary Server)是一个由Salvatore Sanfilippo写key-value存储系统,它由C语言编写.遵守BSD协议.支持网 ...

  2. golang 操作 Redis & Mysql & RabbitMQ

    golang 操作 Redis & Mysql & RabbitMQ Reids 安装导入 go get github.com/garyburd/redigo/redis import ...

  3. Golang 实现 Redis(4): AOF 持久化与AOF重写

    本文是使用 golang 实现 redis 系列的第四篇文章,将介绍如何使用 golang 实现 Append Only File 持久化及 AOF 文件重写. 本文完整源代码在作者GithubHDT ...

  4. Golang 实现 Redis(5): 用跳表实现SortedSet

    本文是使用 golang 实现 redis 系列的第五篇, 将介绍如何使用跳表实现有序集合(SortedSet)的相关功能. 跳表(skiplist) 是 Redis 中 SortedSet 数据结构 ...

  5. Golang 实现 Redis(5): 使用跳表实现 SortedSet

    本文是使用 golang 实现 redis 系列的第五篇, 将介绍如何使用跳表实现有序集合(SortedSet)的相关功能. 跳表(skiplist) 是 Redis 中 SortedSet 数据结构 ...

  6. Golang 实现 Redis(6): 实现 pipeline 模式的 redis 客户端

    本文是使用 golang 实现 redis 系列的第六篇, 将介绍如何实现一个 Pipeline 模式的 Redis 客户端. 本文的完整代码在Github:Godis/redis/client 通常 ...

  7. Golang 实现 Redis(7): Redis 集群与一致性 Hash

    本文是使用 golang 实现 redis 系列的第七篇, 将介绍如何将单点的缓存服务器扩展为分布式缓存.godis 集群的源码在Github:Godis/cluster 单台服务器的CPU和内存等资 ...

  8. Golang 实现 Redis(8): TCC分布式事务

    本文是使用 golang 实现 redis 系列的第八篇, 将介绍如何在分布式缓存中使用 Try-Commit-Catch 方式来解决分布式一致性问题. godis 集群的源码在Github:Godi ...

  9. Golang 实现 Redis(9): 使用GeoHash 搜索附近的人

    本文是使用 golang 实现 redis 系列的第九篇,主要介绍如何使用 GeoHash 实现搜索附近的人. 搜索附近的POI是一个非常常见的功能,它的技术难点在于地理位置是二维的(经纬度)而我们常 ...

  10. Go语言学习笔记(八)golang 操作 Redis & Mysql & RabbitMQ

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 Reids 安装导入 go get github.com/garyburd/redigo/redis import ...

随机推荐

  1. Iroha and a Grid AtCoder - 1974(思维水题)

    就是一个组合数水题 偷个图 去掉阴影部分  把整个图看成上下两个矩形 对于上面的矩形求出起点到每个绿点的方案 对于下面的矩形 求出每个绿点到终点的方案 上下两个绿点的方案相乘后相加 就是了 想想为什么 ...

  2. Ionic3 新增 Service

    service是单例模式的 新增Service类 search.service.ts import {Injectable} from '@angular/core'; @Injectable() e ...

  3. jqGrid 手册 - 搜索

    搜索 类型: colModel Options Toolbar Searching Custom Searching Single field searching Advanced Searching ...

  4. 20165223 《JAVA程序设计》第六周学习总结

    教材学习内容总结 第八章-常用实用类-要点 基础:String类 重点:StringTokenizer类,Scanner类 难点:Class类与Console类,Pattern类与Match类 其他特 ...

  5. C语言中类型转换#大写字母转小写字母和小写字母转大写字母案例。

    先让我们来看个很重要的东西,还是ASCII码. 十进制:A-Z:65-90十进制:a-z:97-122 了解这个很重要. 现在我们先举例子大写字母转小写字母案例: #include <stdio ...

  6. Servlet -- 重定向

    重定向的两种方式: 1: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws S ...

  7. jqgrid 时间戳转换成日期格式

    原文 :http://blog.csdn.net/caoyuancsdn/article/details/52984524 Java script  接收到的时间参数是时间戳*1000 functio ...

  8. An Introduction to OAuth 2

    PostedJuly 21, 2014 1.1mviews SECURITY API CONCEPTUAL Mitchell Anicas Introduction OAuth 2 is an aut ...

  9. css border 三角形

    当元素的宽高都为0时,只写border,就会发现形成的正方形有4个三角形组成. .triangle { width: 0px; height: 0px; border: 20px solid tran ...

  10. 【SDOI2008】仪仗队

    //裸的欧拉函数//y=kx//求不同的k有多少#include<bits/stdc++.h> #define ll long long #define N 40010 using nam ...