redis系列-redis的连接
2015-10-23 11:00
405 查看
转自:http://blog.csdn.net/whuqin/article/details/17530709
Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
redis客户端连接比较简单,但日常中redis的使用和维护会有很多地方需要学习。本文只简单介绍下常用函数。
[cpp] view
plaincopy
//hiredis/hiredis.h
/* Context for a connection to Redis */
typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
int fd;
int flags;
char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */
} redisContext;
/* This is the reply object returned by redisCommand() */
#define REDIS_REPLY_STRING 1
#define REDIS_REPLY_ARRAY 2
#define REDIS_REPLY_INTEGER 3
#define REDIS_REPLY_NIL 4
#define REDIS_REPLY_STATUS 5
#define REDIS_REPLY_ERROR 6
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
int len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);
void redisFree(redisContext *c);
//Issue a command to Redis, NULL if error, otherwise reply
void *redisCommand(redisContext *c, const char *format, ...);
/* Function to free the reply objects hiredis returns by default. */
void freeReplyObject(void *reply);
应用实例:
[cpp] view
plaincopy
#include "hiredis/hiredis.h"
redisContext* context = NULL;
redisReply* reply = NULL;
int retry = 0;
while (retry < 5)
{
reply != NULL ? freeReplyObject(reply):;
context != NULL ? redisFree(context):;
context = redisConnect("127.0.0.1", 8600);
if (NULL == context || context->err != 0)
{
cout << "connect err" << endl;
continue;
}
redisReply* reply = (redisReply*)redisCommand(context, "%s", "set name John");
if (NULL == reply || reply == REDIS_REPLY_ERROR)
{
cout << "exe command fail" << endl;
continue;
}
cout << "ok" << endl;
}
reply != NULL ? freeReplyObject(reply):;
context != NULL ? redisFree(context):;
Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
redis客户端连接比较简单,但日常中redis的使用和维护会有很多地方需要学习。本文只简单介绍下常用函数。
[cpp] view
plaincopy
//hiredis/hiredis.h
/* Context for a connection to Redis */
typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
int fd;
int flags;
char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */
} redisContext;
/* This is the reply object returned by redisCommand() */
#define REDIS_REPLY_STRING 1
#define REDIS_REPLY_ARRAY 2
#define REDIS_REPLY_INTEGER 3
#define REDIS_REPLY_NIL 4
#define REDIS_REPLY_STATUS 5
#define REDIS_REPLY_ERROR 6
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
int len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);
void redisFree(redisContext *c);
//Issue a command to Redis, NULL if error, otherwise reply
void *redisCommand(redisContext *c, const char *format, ...);
/* Function to free the reply objects hiredis returns by default. */
void freeReplyObject(void *reply);
应用实例:
[cpp] view
plaincopy
#include "hiredis/hiredis.h"
redisContext* context = NULL;
redisReply* reply = NULL;
int retry = 0;
while (retry < 5)
{
reply != NULL ? freeReplyObject(reply):;
context != NULL ? redisFree(context):;
context = redisConnect("127.0.0.1", 8600);
if (NULL == context || context->err != 0)
{
cout << "connect err" << endl;
continue;
}
redisReply* reply = (redisReply*)redisCommand(context, "%s", "set name John");
if (NULL == reply || reply == REDIS_REPLY_ERROR)
{
cout << "exe command fail" << endl;
continue;
}
cout << "ok" << endl;
}
reply != NULL ? freeReplyObject(reply):;
context != NULL ? redisFree(context):;
相关文章推荐
- java对redis的基本操作
- redis常用命令
- Redis学习笔记(三)类型之散列
- spring整合redis
- redis数据类型
- 基于nginx tomcat redis分布式web应用的session共享配置
- Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享
- Hadoop、Spark、HBase与Redis的适用性讨论
- redis unwatch discard
- redis watch multi exec 关系
- jedis从2.2.2升级后关于redis连接池的设置问题
- Python操作Redis之订阅与发布
- Redis生存时间--Redis系列八
- Redis事务--Redis系列七
- Redis命令总结
- 关于redis的pconnect和connect,使用中出现的问题
- Linux下redis的安装
- Redis 启动警告错误解决[转]
- centos安装Redis
- 批量删除redis key