C 程序 redis环境配置 访问Redis(二)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: 接上一篇继续给大家分享

项目属性中包含一下头文件和库文件的目录

image.pngimage.png


这三项若能正常运行则不用加 若有问题 看着调试吧…这里已经涉及到知识盲区了

  1. 项目->属性->配置属性->C/C+±>代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)


  1. 项目->属性->配置属性->链接器->命令行中输入/NODEFAULTLIB:libcmt.lib


  1. 项目->属性->配置属性->C/C+±>预处理器->预处理器定义->添加“_CRT_SECURE_NO_WARNINGS”



代码

#include<stdio.h>

#include<stdlib.h>

#include<string.h>



#include <hiredis.h>


#include "./Win32_Interop/win32fixes.h"


#define NO_QFORKIMPL

#include <Win32_Interop/win32fixes.h>

#pragma comment(lib,"hiredis.lib")

#pragma comment(lib,"Win32_Interop.lib")





int main(int argc, char** argv) {

//链接 redis server

struct timeval timeout = { 1, 500000 }; // 1.5 秒 超时 {秒,微秒}

redisContext* c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);

if (c->err) {

 //超时 链接错误

 printf("Connection error: %s\n", c->errstr);


 goto end;

}


//操作

//若有错误 返回null  

redisReply* replay = redisCommand(c, "select %d", 15);

if (replay) {

 printf("%d, %s\n", replay->type, replay->str);

 freeReplyObject(replay);

}

else {

 printf("relay == NULL");

}


replay = redisCommand(c, "zadd world_rank 3000 xt");

if (replay) {

 printf("%d %d\n", replay->type, replay->integer);

 freeReplyObject(replay);

}


replay = redisCommand(c, "zrange world_rank 0 10 withscores");

if (replay) {

 if (replay->type == REDIS_REPLY_ARRAY) {

  for (int i = 0; i < replay->elements; i++) {

   printf("%d: %s\n", i, replay->element[i]->str);

  }

 }


 freeReplyObject(replay);

}


end:

//释放

redisFree(c);


system("pause");

return 0;

}


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
2月前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
|
18天前
|
安全 NoSQL 网络安全
漏洞检测与防御:Redis未授权访问漏洞复现
漏洞检测与防御:Redis未授权访问漏洞复现
30 0
|
2月前
|
缓存 负载均衡 NoSQL
【Azure Redis】Azure Redis添加了内部虚拟网络后,其他区域的主机通过虚拟网络对等互连访问失败
【Azure Redis】Azure Redis添加了内部虚拟网络后,其他区域的主机通过虚拟网络对等互连访问失败
|
2月前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】在Azure Redis中,如何限制只允许Azure App Service访问?
【Azure Redis 缓存】在Azure Redis中,如何限制只允许Azure App Service访问?
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】C#程序是否有对应的方式来优化并缩短由于 Redis 维护造成的不可访问的时间
【Azure Redis 缓存】C#程序是否有对应的方式来优化并缩短由于 Redis 维护造成的不可访问的时间
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Redis加入VNET后,在另一个区域(如中国东部二区)的VNET无法访问Redis服务(注:两个VNET已经结对,相互之间可以互ping)
【Azure Redis 缓存】Azure Redis加入VNET后,在另一个区域(如中国东部二区)的VNET无法访问Redis服务(注:两个VNET已经结对,相互之间可以互ping)
|
2月前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置
【Azure Redis 缓存】如何使得Azure Redis可以仅从内网访问? Config 及 Timeout参数配置
|
2月前
|
网络协议 NoSQL 网络安全
【Azure 应用服务】由Web App“无法连接数据库”而逐步分析到解析内网地址的办法(SQL和Redis开启private endpoint,只能通过内网访问,无法从公网访问的情况下)
【Azure 应用服务】由Web App“无法连接数据库”而逐步分析到解析内网地址的办法(SQL和Redis开启private endpoint,只能通过内网访问,无法从公网访问的情况下)
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】遇见Azure Redis不能创建成功的问题:至少一个资源部署操作失败,因为 Microsoft.Cache 资源提供程序未注册。
【Azure Redis 缓存】遇见Azure Redis不能创建成功的问题:至少一个资源部署操作失败,因为 Microsoft.Cache 资源提供程序未注册。
|
17天前
|
存储 缓存 NoSQL
数据的存储--Redis缓存存储(一)
数据的存储--Redis缓存存储(一)
53 1