有条件的Redis设置/仅更新最新版本? [英] Conditional Redis set / only update with newest version?

查看:153
本文介绍了有条件的Redis设置/仅更新最新版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Redis 中做一个条件集?

Is there some way to do a conditional set in Redis?

我想用Redis缓存一些对象.缓存的每个用户(服务器程序)将检查一个对象并在它有更新版本时更新它.我需要确保在更新步骤中只有最新版本才真正保存在 Redis 中.

I want to use Redis to cache some objects. Each user (server programs) of the cache will check for an object and update it if it has a newer version. I need to ensure that during the update step only the newest version really gets saved in Redis.

推荐答案

您可以编写一个 lua 脚本来检查键的当前值并在该值与新值不同时更改它.我在 c 中添加了一个通过 c 程序调用 lua 脚本的示例并完成所需的工作.

You could write a lua script which would check for current value of key and change it if the value differs from new one. I have added an example in c of invoking lua script via c-program and do the required job.

  //g++ -g -o condition condition.cpp  -I/usr/local/include/hiredis -L/usr/local/lib  -levent -lhiredis
/*----------------------
  EVAL
  ------------------------*/

#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 set
string 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;
}

这篇关于有条件的Redis设置/仅更新最新版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆