Reids Lua fuzzy query and the total number of all key corresponding to the set

The total number of collection Redis Lua using fuzzy queries and all key corresponding to

  .Net 4.5.1    

       Need to introduce: StackExchange.Redis (test of 1.2.4.0)

method one:

  Advantages: atomic operations, for large data fast operation, lua syntax difficult to write.

            var Prefix = "Cache"; // Key prefix keyword
            var dic = new Dictionary <string, long> (); // convert key team
            var db = new Multiplexer(name).GetMultiplexer().GetDatabase();    //redis 连接

            var pattern = Prefix + "*"; // Fuzzy query
       // lua syntax details, see the official documentation
            var lua = @"local glob = @keypattern
                      local t = { }
                      local keys = redis.call('keys', glob)
                      for iter, value in ipairs(keys) do
                      table.insert(t, { value, redis.call('llen', value) })
                      end
                      return t";
var redisResult = db.ScriptEvaluate(LuaScript.Prepare(lua), new { @keypattern = pattern });
        if (!redisResult.IsNull) {// insert the key and value key team was Result = (RedisResult []) redisResult; foreach (var item in result) { var kvs = (RedisResult[])item; var key = ((RedisValue)kvs.First()).ToString(); var value = (long)(RedisValue)kvs.Last(); dic.Add(key, value); } }

Method Two:

    Pros: Easy to use a small amount of data query, we need to interact with multiple Redis statistics.

                    Because not atomic, there is a statistical amount of data out of the inaccurate;

var dic = new Dictionary<string, int>();
var _connMultiplexer = new Multiplexer(name).GetMultiplexer();
var _server = _connMultiplexer.GetServer(_connMultiplexer.GetEndPoints()[0]); 
// a default server
var pattern = this.Prefix + "*";
var keys = _server.Keys(database: this.database, pattern: pattern);
var db = this.GetDatabase();
foreach (var item in keys)
{
    dic.Add(item.ToString().Replace(this.Prefix, ""), db.ListLength(item));
}

  

Guess you like

Origin www.cnblogs.com/intotf/p/11101277.html