C语言操作Redis总结
#include "hiredis.h" #define NO_QFORKIMPL
#pragma comment(lib,"hiredis.lib")
#pragma comment(lib,"Win32_Interop.lib") int get_int_command(char int_command[])
{
reply = (redisReply *)redisCommand(c, int_command);
//printf("exists命令执行结果: %d\n", reply->type);
if (reply->type == ) //返回整型标识
{
//printf("%s命令执行结果: %d\n", int_command, reply->integer);
return reply->integer;
}
else if (reply->type == ) //返回nil对象
{
return -;
}
else if (reply->type == ) //返回错误
{
return -;
}
freeReplyObject(reply);
return ;
} char* get_string_command(char string_command[])
{
reply = (redisReply *)redisCommand(c, string_command);
//printf("lindex MA_h1_K 0命令执行结果 reply type: %d\n", reply->type);
if (reply->type == ) //返回字符串标识
{
//printf("lindex MA_h1_K 0命令执行结果 reply type: %s\n", reply->str);
return reply->str;
}
else if (reply->type == ) //返回nil对象
{
return "不存在要访问的数据";
}
else if (reply->type == ) //返回错误
{
return reply->str;
}
freeReplyObject(reply);
return "";
} void run_command(char run_command[])
{
reply = (redisReply *)redisCommand(c, run_command);
//printf("reply type: %d\n", reply->type);
if (reply->type == )
{
//printf("run_command执行结果: %s\n", reply->str);
}
freeReplyObject(reply);
} int main()
{
SYSTEMTIME sys;
char local_time[] = ""; c = redisConnect((char*)redis_host, redis_port);
if (c->err) { /* Error flags, 0 when there is no error */
printf("连接Redis失败: %s\n", c->errstr);
exit();
}
else
{
printf("连接Redis成功!\n");
} reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password);
if (reply->type == REDIS_REPLY_ERROR) {
printf("Redis认证失败!\n");
}
else
{
printf("Redis认证成功!\n");
}
freeReplyObject(reply); reply = (redisReply *)redisCommand(c, "SELECT 1"); //选择数据库
printf("SELECT: 1 %s\n", reply->str);
freeReplyObject(reply); //delete命令
run_command("DEL foo"); //set命令
run_command("SET foo hello world"); //get命令
printf("GET foo命令执行结果 : %s\n", get_string_command("GET foo")); //exists命令
printf("exists test1命令执行结果: %d\n", get_int_command("exists test1"));
printf("exists MA_h1_K命令执行结果: %d\n", get_int_command("exists MA_h1_K")); //llen命令
printf("llen MA_h1_K命令执行结果: %d\n", get_int_command("llen MA_h1_K")); //lrange命令
reply = (redisReply *)redisCommand(c, "lrange MA_h1_K 0 7");
//printf("lrange MA_h1_K 0 7命令执行结果 reply type: %d\n", reply->type);
if (reply->type == )
{
printf("队列数量为: %d\n", reply->elements);
if (reply->element[]->type == )
{
for (int i = ; i < reply->elements; i++)
{
printf("lrange MA_h1_K 0 7命令执行结果: %s\n", reply->element[i]->str);
}
} }
freeReplyObject(reply); //lindex命令
printf("lindex MA_h1_K 0命令执行结果 : %s\n", get_string_command("lindex MA_h1_K 0")); //lpush命令
run_command("lpush list test1 test2 test3"); //lpop命令
printf("lpop list命令执行结果 : %s\n", get_string_command("lpop list")); //rpop命令
printf("rpop list命令执行结果 : %s\n", get_string_command("rpop list")); //rpoplpush命令
printf("rpoplpush list list1命令执行结果 : %s\n", get_string_command("rpoplpush list list1")); printf("lpop list1命令执行结果 : %s\n", get_string_command("lpop list1")); //lpush rpush lpop rpop RPOPLPUSH char test;
test = getchar();
}
C语言操作Redis总结的更多相关文章
- Go语言操作Redis
Go语言操作Redis Redis介绍 Redis是一个开源的内存数据库,Redis提供了多种不同类型的数据结构,很多业务场景下的问题都可以很自然地映射到这些数据结构上.除此之外,通过复制.持久化和客 ...
- GO学习-(24) Go语言操作Redis
Go语言操作Redis 在项目开发中redis的使用也比较频繁,本文介绍了Go语言中go-redis库的基本使用. Redis介绍 Redis是一个开源的内存数据库,Redis提供了多种不同类型的数据 ...
- Go语言基础之操作Redis
Go语言操作Redis 在项目开发中redis的使用也比较频繁,本文介绍了Go语言如何操作Redis. Redis介绍 Redis是一个开源的内存数据库,Redis提供了5种不同类型的数据结构,很多业 ...
- 【原创】自己动手写一个能操作redis的客户端
引言 redis大家在项目中经常会使用到.官网也提供了多语言的客户端供大家操作redis,如下图所示 但是,大家有思考过,这些语言操作redis背后的原理么?其实,某些大神会说 只要按照redis的协 ...
- go语言之行--golang操作redis、mysql大全
一.redis 简介 redis(REmote DIctionary Server)是一个由Salvatore Sanfilippo写key-value存储系统,它由C语言编写.遵守BSD协议.支持网 ...
- Go语言之进阶篇操作redis
1.windows安装redis 软件包下载地址: https://github.com/MicrosoftArchive/redis/releases 1.1.安装--->下一步---> ...
- Java Spring mvc 操作 Redis 及 Redis 集群
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...
- Python操作Redis、Memcache、RabbitMQ、SQLAlchemy
Python操作 Redis.Memcache.RabbitMQ.SQLAlchemy redis介绍:redis是一个开源的,先进的KEY-VALUE存储,它通常被称为数据结构服务器,因为键可以包含 ...
- Python操作Redis及连接方式
前沿:随着互联网的高速发展,数据变得越来越重要,Python成为了人工智能的热门语言,而Nosql数据库已成为日常开发用品. 今天要写的是Python操作Redis Redis的安装我就不介绍了,你可 ...
随机推荐
- Java并发编程的艺术(八)——闭锁、同步屏障、信号量详解
1. 闭锁:CountDownLatch 1.1 使用场景 若有多条线程,其中一条线程需要等到其他所有线程准备完所需的资源后才能运行,这样的情况可以使用闭锁. 1.2 代码实现 // 初始化闭锁,并设 ...
- webrequest HttpWebRequest webclient/HttpClient
webrequest(abstract类,不可直接用) <--- (继承)---- HttpWebRequest(更好的控制请求) <--- (继承)---- webclient (简单快 ...
- Guava之Iterables使用示例
这是一个常量工具类.Iterables类包含了一系列的静态方法,来操作或返回Iterable对象. public final class Iterables { private Iterables() ...
- verilog语法实例学习(8)
常用的时序电路介绍 在电平敏感的锁存器时钟信号有效期(高电平)期间,锁存器的状态随着输入信号的变化而变化.有时候,我们需要存储器的状态在一个时钟周期只改变一次,这个时候就用到了触发器.触发器(flip ...
- cull/clip distance example
http://www.gamedev.net/topic/578866-d3d10-how-to-increase-maxcount-of-sv_clipdistance/ The D3D#_CL ...
- Spring(AbstractRoutingDataSource)实现动态数据源切换
转自: http://blog.51cto.com/linhongyu/1615895 一.前言 近期一项目A需实现数据同步到另一项目B数据库中,在不改变B项目的情况下,只好选择项目A中切换数据源,直 ...
- Asp.net WebAPi gzip压缩和json格式化
现在webapi越来越流行了,很多时候它都用来做接口返回json格式的数据,webapi原本是根据客户端的类型动态序列化为json和xml的,但实际很多时候我们都是序列化为json的,所以webapi ...
- iOS开发-UIScrollView图片无限循环
关于UIScrollView图片浏览的例子有很多,之前也写过类似方面的文章,关于UIScrollView的图片循环在新闻类的App基本上是比较常见的一种情况就是图片浏览,然后根据不同的图片显示不同的内 ...
- java 反射机制--根据属性名获取属性值
1.考虑安全访问范围内的属性,没有权限访问到的属性不读取 /** * 根据属性名获取属性值 * * @param fieldName * @param object * @return */ priv ...
- Kalman滤波器从原理到实现
Kalman滤波器的历史渊源 We are like dwarfs on the shoulders of giants, by whose grace we see farther than the ...