Golang Redis操作

1. Redis简介

Redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value数据库。

1.1 特点

  • 支持更多数据类型 
    和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set 有序集合)和hash(哈希类型)。[1]
  • 支持复杂操作 
    这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,Redis支持各种不同方式的排序。[2]
  • 支持主从同步。 
    与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是Redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。从盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

Redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。Redis的官网地址,非常好记,是redis.io。目前,Vmware在资助着Redis项目的开发和维护。

2. redigo

redigo是GO语言的一个redis客户端实现。项目位于https://github.com/garyburd/redigo

2.1 安装redigo

redigo没有其他依赖项,可以直接通过go get进行安装 
go get github.com/garyburd/redigo/redis

2.2 连接

Conn接口是与Redis协作的主要接口,可以使用Dial,DialWithTimeout或者NewConn函数来创建连接,当任务完成时,应用程序必须调用Close函数来完成

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/garyburd/redigo/redis"
  5. )
  6. func main() {
  7. c, err := redis.Dial("tcp", "localhost:6379")
  8. if err != nil {
  9. fmt.Println("Connect to redis error", err)
  10. return
  11. }
  12. defer c.Close()
  13. ...

2.3 命令执行

Conn接口中有一个通用方法来执行

n, err := c.Do("APPEND", "key", "value")

Do函数会必要时将参数转化为二进制字符串

Go Type Conversion
[]byte Sent as is
string Sent as is
int, int64 strconv.FormatInt(v)
float64 strconv.FormatFloat(v, ‘g‘, -1, 64)
bool true -> "1", false -> "0"
nil ""
all other types fmt.Print(v)

Redis 命令响应会用以下Go类型表示:

Redis type Go type
error redis.Error
integer int64
simple string string
bulk string []byte or nil if value not present.
array []interface{} or nil if value not present.

可以使用GO的类型断言或者reply辅助函数将返回的interface{}转换为对应类型

常用命令[3]

读写

GET key
SET key value [EX seconds] [PX milliseconds] [NX|XX]`

GET取对应键值,如果键值不存在则nil会返回,

  1. // 写入值永不过期
  2. _, err = c.Do("SET", "username", "nick")
  3. if err != nil {
  4. fmt.Println("redis set failed:", err)
  5. }
  6. username, err := redis.String(c.Do("GET", "username"))
  7. if err != nil {
  8. fmt.Println("redis get failed:", err)
  9. } else {
  10. fmt.Printf("Got username %v \n", username)
  11. }

输出: 
Got username nick 
SET命令还支持附加参数

  • EX seconds -- 指定过期时间,单位为秒.
  • PX milliseconds -- 指定过期时间,单位为毫秒.
  • NX -- 仅当键值不存在时设置键值.
  • XX -- 仅当键值存在时设置键值. 
    设置键值过期时间为10s
  1. // 写入值10S后过期
  2. _, err = c.Do("SET", "password", "123456", "EX", "10")
  3. if err != nil {
  4. fmt.Println("redis set failed:", err)
  5. }
  6. time.Sleep(11 * time.Second)
  7. password, err := redis.String(c.Do("GET", "password"))
  8. if err != nil {
  9. fmt.Println("redis get failed:", err)
  10. } else {
  11. fmt.Printf("Got password %v \n", password)
  12. }

输出 
redis get failed: redigo: nil returned 
批量写入读取

MGET key [key ...]
MSET key value [key value ...]

批量写入读取对象(Hashtable)

HMSET key field value [field value ...]
HMGET key field [field ...]

检测值是否存在

EXISTS key

删除

DEL key [key ...]

设置过期时间

EXPIRE key seconds

2.4 管道化(Pipelining)

请求/响应服务可以实现持续处理新请求,即使客户端没有准备好读取旧响应。这样客户端可以发送多个命令到服务器而无需等待响应,最后在一次读取多个响应。这就是管道化(pipelining),这个技术在多年就被广泛使用了。距离,很多POP3协议实现已经支持此特性,显著加速了从服务器下载新邮件的过程。 
Redis很早就支持管道化,所以无论你使用任何版本,你都可以使用管道化技术

连接支持使用Send(),Flush(),Receive()方法支持管道化操作

Send(commandName string, args ...interface{}) error
Flush() error
Receive() (reply interface{}, err error)

Send向连接的输出缓冲中写入命令。Flush将连接的输出缓冲清空并写入服务器端。Recevie按照FIFO顺序依次读取服务器的响应。下例展示了一个简单的管道:

  1. c.Send("SET", "foo", "bar")
  2. c.Send("GET", "foo")
  3. c.Flush()
  4. c.Receive() // reply from SET
  5. v, err = c.Receive() // reply from GET

Do方法组合了Send,Flush和 Receive方法。Do方法先写入命令,然后清空输出buffer,最后接收全部挂起响应包括Do方发出的命令的结果。如果任何响应中包含一个错误,Do返回错误。如果没有错误,Do方法返回最后一个响应。如果Do函数的命令参数为"",那么Do方法会清空out缓冲并接受挂起响应而不发送任何命令。

2.5 并发

连接并不支持并发调用写入方法(Send,Flush)或者读取方法(Receive)。但是连接支持并发的读写。 
因为Do方法组合了Send,Flush和Receive,Do不可以与其他方法并发执行。

为了能够完全并发访问Redis,在gorouotine中使用线程安全的连接池来获取和释放连接。

2.6 发布和订阅(Pub/Sub)

使用Send,Flush和Receive方法来是实现Pub/Sb订阅者。

  1. c.Send("SUBSCRIBE", "example")
  2. c.Flush()
  3. for {
  4. reply, err := c.Receive()
  5. if err != nil {
  6. return err
  7. }
  8. // process pushed message
  9. }

PubSubConn类型封装了Conn提供了便捷的方法来实现订阅者模式。Subscribe,PSubscribe,Unsubscribe和PUnsubscribe方法发送和清空订阅管理命令。Receive将一个推送消息 
转化为一个在type switch更为方便使用的类型。

  1. psc := redis.PubSubConn{c}
  2. psc.Subscribe("example")
  3. for {
  4. switch v := psc.Receive().(type) {
  5. case redis.Message:
  6. fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
  7. case redis.Subscription:
  8. fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  9. case error:
  10. return v
  11. }
  12. }

2.7 响应辅助函数

Bool,Int,Bytes,String,Strings和Values函数将响应转化为特定类型值。为了允许更方便的封装连接的Do和Receive方法的调用,函数使用第二个参数类型为error类型。如果这个error为non-nil,辅助函数会返回error。如果error为nil,则function转化响应为特定类型。

  1. exists, err := redis.Bool(c.Do("EXISTS", "foo"))
  2. if err != nil {
  3. // handle error return from c.Do or type conversion error.
  4. }
  5. The Scan function converts elements of a array reply to Go types:
  6. var value1 int
  7. var value2 string
  8. reply, err := redis.Values(c.Do("MGET", "key1", "key2"))
  9. if err != nil {
  10. // handle error
  11. }
  12. if _, err := redis.Scan(reply, &value1, &value2); err != nil {
  13. // handle error
  14. }

2.8 事务支持(Transaction)

MULTI, EXEC,DISCARD和WATCH是构成Redis事务的基础。它们允许在一个步骤中执行一组命令,并提供两点总要保证:

  • 事务中的全部命令被序列化并且顺序执行。它保证不会在Redis事务的处理过程中处理其他客户端发起的请求。这样保证命令如同一个单独操作被执行。
  • 事务中的命令全部被执行或者一个都没有被执行,这样一个Redis事务是原子的。Exec命令触发事务中全部命令的执行,这样如果客户端启动事务后失去连接,在调用MULTI命令后的命令都不会被执行,否则如果EXEC命令被调用,全部操作都会被执行。当配置用只允许添加的文件,Redis保证使用连个独立的write(2) 系统调用来将事务写入磁盘。如果Redis服务崩溃或者被系统管理强行关闭可能只有一部分操作被注册。Redis会在重启时检测到这种状态,并且从中退出附带一个错误。使用redis-check-aoft工具它可以修复只允许添加的文件并且移除事务碎片这样可以让服务重启。

Starting with version 2.2, Redis allows for an extra guarantee to the above two, in the form of optimistic locking in a way very similar to a check-and-set (CAS) operation. This is documented later on this page. 
Usage 
自从版本2.2,Redis允许在以上两点基础上增加一点保证, 
A Redis transaction is entered using the MULTI command. The command always replies with OK. At this point the user can issue multiple commands. Instead of executing these commands, Redis will queue them. All the commands are executed once EXEC is called. 
Calling DISCARD instead will flush the transaction queue and will exit the transaction. 
The following example increments keys foo and bar atomically.

MULTI 
OK 
INCR foo 
QUEUED 
INCR bar 
QUEUED 
EXEC 
1) (integer) 1 
2) (integer) 1 
As it is possible to see from the session above, EXEC returns an array of replies, where every element is the reply of a single command in the transaction, in the same order the commands were issued. 
When a Redis connection is in the context of a MULTI request, all commands will reply with the string QUEUED (sent as a Status Reply from the point of view of the Redis protocol). A queued command is simply scheduled for execution when EXEC is called.

使用Send和Do方法来实现管道化事务

  1. c.Send("MULTI")
  2. c.Send("INCR", "foo")
  3. c.Send("INCR", "bar")
  4. r, err := c.Do("EXEC")
  5. fmt.Println(r) // prints [1, 1]

[1] Redis 数据类型支持 ?
[2] Redis Commands ?
[3] Redis Commands ?

阅读原文

时间: 2024-07-30 12:09:36

Golang Redis操作的相关文章

python笔记7:mysql、redis操作

模块安装: 数据操作用到的模块pymysql,需要通过pip install pymysql进行安装. redis操作用的模块是redis,需要通过pip install redis进行安装. 检验是否安装成功:进入到Python命令行模式,输入import pymysql. import redis ,无报错代表成功: mysql操作方法如下: 查询数据:fetchone.fetchmany(n).fetchall() import pymysql #建立mysql连接,ip.端口.用户名.密

redis 操作常用命令

首先看一下redis操作常用的命令: exists key 测试制定的key是否存在 del key1 key2 .... keyn   删除制定的key type key 查看key的类型 keys pattern  返回匹配制定模式的所有的key raname oldkey newkey  修改key的名称 dbsize  查看当前数据库的key的数量 exprie key  seconds   为key指定过期时间 ttl  key  查看key的过期时间 select db-index

redis 操作大全 PHP-redis中文文档

转自  : http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__construct构造函数$redis = new Redis(); connect,

使用python对redis操作

写在前面 首先声明,这是为了学习python对redis操作而写的一个小demo,包括了这几天网站找到的一些资料,综合总结出来一些东西,最后附上我写的一个用python操作redis的一个demo: 模块安装 python提供了一个模块redis-py来使我们很方便的操作redis数据库,安装该模块也很简单,直接使用pip安装就行,命令如下: pip install redis 安装完之后,使用import调用一下就能知道是否安装成功,在python界面下输入import redis,如果不报错

使用Spring Data Redis操作Redis(二)

上一篇讲述了Spring Date Redis操作Redis的大部分主题,本篇介绍Redis的订阅和发布功能在Spring应用中的使用. 1. Redis的Pub/Sub命令 Redis的订阅和发布服务有如下图6个命令,下面分别对每个命令做简单说明. publish: 向指定的channel(频道)发送message(消息) subscribe:订阅指定channel,可以一次订阅多个 psubscribe:订阅指定pattern(模式,具有频道名的模式匹配)的频道 unsubscribe:取消

使用Leopard Redis操作Redis

使用Leopard Redis操作Redis 学习如何在旧项目中使用Leopard Redis. 本指南将引导您完成使用Leopard Redis操作Redis. How to complete this guide 你可以从头开始并完成每一个步骤,或者您可以绕过你已经熟悉的基本设置步骤.无论哪种方式,你最终都可以得到可工作的代码. 1.配置maven依赖 在dao模块的pom.xml加入 <dependencies> [...] <dependency> <groupId&

设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类

1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个数的控制并节约系统资源.假设希望在系统中某个类的对象仅仅能存在一个.单例模式是最好的解决方式. 2 模式核心思想 1)某个类仅仅能有一个实例: 2)它必须自行创建这个实例: 3)它必须自行向整个系统提供这个实例. 3 模式架构图 4 项目应用 4.1 需求说明 CleverCode在实际的PHP项目

PHP连接Redis操作函数

phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__construct构造函数$redis = new Redis(); connect, open 链接redis服务参数host: string,服务地址port: int,端口号timeout: float,链接时长 (

redis操作

字符串命令set name fsq 设置name的值为fsq,如果存在name会覆盖.setnx name fsq 不存在name则设置,如果存在不会覆盖setex haircolor 10 red 设置超时10秒,10秒后此健值对失效mset key1 fsq1 key2 fsq 设置多个msetnx key2 fsq2 key3 fsq3 不存在则设置,防止覆盖 setrange name 8 gmail.com 设置name的值,从第8个字符开始,逐个字符设置,如果后边的字符串比gmail