栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有条件的Redis集/仅使用最新版本进行更新?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有条件的Redis集/仅使用最新版本进行更新?

您可以编写一个lua脚本,该脚本将检查密钥的当前值,如果该值与新密钥不同,则将其更改。我在c中添加了一个通过c程序调用lua脚本的示例,并完成了所需的工作。

  //g++ -g -o condition condition.cpp  -I/usr/local/include/hiredis -L/usr/local/lib  -levent -lhiredis#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string>#include <hiredis/hiredis.h>using namespace std;struct timeval _timeout ;redisContext *_redisContext;const long long SEC_TO_USEC = 1000000 ;void connect(const std::string &ip,    int port,    int timeoutInUsec ){  _timeout.tv_sec = timeoutInUsec / SEC_TO_USEC ;  _timeout.tv_usec = timeoutInUsec % SEC_TO_USEC ;  _redisContext = redisConnectWithTimeout(ip.c_str(), port, _timeout);  if (_redisContext->err)  {    std::cout << "Cannot connect to redis server. "      << " Error : " << _redisContext->errstr      << std::endl ;    exit(1);  }}//lua scrip for conditional setstring scriptMultipleCommands ="local res = redis.call("GET", KEYS[1])   ""if res == ARGV[1] then          "" return nil          ""else      ""redis.call("SET", KEYS[1],  ARGV[1])     ""end       ""local data = redis.call("GET",KEYS[1])   ""return data          ";void luaCommand(char** argv){  string command;  command.append( scriptMultipleCommands );  redisReply *reply =    ( redisReply * ) redisCommand( _redisContext,        "eval %s %d %s %s ",command.c_str(),1,argv[1],argv[2]);  cout<<"Redis reply type "<<reply->type<<endl;  if (reply->type == REDIS_REPLY_ARRAY)  {    cout<<"Redis reply size "<<reply->elements<<endl;    for (int j = 0; j < reply->elements; j++)    {      if((j+1) < reply->elements)      {        cout<<(reply->element[j]->str)<<","<<(reply->element[j+1]->str)<<endl;        ++j;      }    }  }  else if (reply->type == REDIS_REPLY_INTEGER) {    cout<<"Key value "<<reply->integer<<endl;  }  else    cout<<endl<<"eval: "<< reply->str<<endl;  freeReplyObject(reply);}int main(int argc,char** argv){  connect("10.0.0.30",6379,1500000);  luaCommand(argv);  return 0;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393217.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号