2015年3月18日 星期三

Redis筆記-EVAL 使用if判斷對應欄位值範例

語言: c#
使用函式庫: StackExchange.Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//設定連線字串
using ( ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:10001"))
{
 // 取得操作介面
 IDatabase cache = conn.GetDatabase();
 
 RedisKey custKey1 = "Key1";
 RedisValue newTest1 = "first";
 
 // 設定Script 字串
 string tEval = @"local res = redis.call('get', KEYS[1])
    if res  == ""value1"" then
     return ARGV[1]
    else
     return res
    end";
 
 // 設定 custKey1 的值為 "value1"
 cache.StringSet(custKey1, "value1");
 
 // 呼叫執行Script (Eval)
 RedisResult tResult = cache.ScriptEvaluate(tEval
  , new RedisKey[] { custKey1 }, new RedisValue[] { newTest1 });
 
 if (tResult.IsNull == false)
 {
  Console.WriteLine(tResult.ToString());
 }
}

沒有留言:

張貼留言