redis系列-redis的连接
来源:互联网 发布:sql解决方案资源管理器 编辑:程序博客网 时间:2024/05/01 22:40
Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
redis客户端连接比较简单,但日常中redis的使用和维护会有很多地方需要学习。本文只简单介绍下常用函数。
//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 6typedef 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 replyvoid *redisCommand(redisContext *c, const char *format, ...);/* Function to free the reply objects hiredis returns by default. */void freeReplyObject(void *reply);
应用实例:
#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):;
0 0
- redis系列-redis的连接
- redis系列-redis的连接
- Redis系列-远程连接redis
- Redis系列-远程连接redis
- Redis系列-远程连接redis redis-cli
- 【Redis系列】Redis的事务处理
- Redis系列-远程连接redis并给redis加锁
- Redis系列-远程连接redis并给redis加锁
- Redis系列-远程连接redis并给redis加锁
- Redis系列-远程连接redis并给redis加锁
- Redis系列-远程连接redis并给redis加锁
- Redis的连接
- 连接redis的方法
- redis系列-redis的使用场景
- redis系列-redis的持久化
- redis系列(三)redis的安装
- redis的事务-redis系列教程
- redis的集群-redis系列教程
- cocos2d-x编译问题集合之error C2440: “类型转换”: 无法从“void (__thiscall ::* )(void)”转换为“
- 进程和线程
- 编译时给出警告的小trick,利用下标溢出的警告
- ccjoystick 代码 (学习 )
- 色彩空间
- redis系列-redis的连接
- java桥牌发牌算法
- 排序算法(四):快速排序
- 基于STM32的USB程序开发笔记(四)——USB设备的枚举(上)
- 利用正则表达式判断字符串是否为数字(c++)
- Document Object Model (DOM) MS MSDN
- android应用程序的安装与卸载
- maven工程将main函数打成jar包
- mount远程目录并让本地非root用户可读可写