Example #1
0
        public object Any(SearchRedis request)
        {
            var limit = request.Take.GetValueOrDefault(AppSettings.Get("query-limit", 100));

            const string LuaScript = @"
local limit = tonumber(ARGV[2])
local pattern = ARGV[1]
local cursor = 0
local len = 0
local keys = {}

repeat
    local r = redis.call('scan', cursor, 'MATCH', pattern, 'COUNT', limit)
    cursor = tonumber(r[1])
    for k,v in ipairs(r[2]) do
        table.insert(keys, v)
        len = len + 1
        if len == limit then break end
    end
until cursor == 0 or len == limit

if len == 0 then
    return '[]'
end

local keyAttrs = {}
for i,key in ipairs(keys) do
    local type = redis.call('type', key)['ok']
    local pttl = redis.call('pttl', key)
    local size = 0
    if type == 'string' then
        size = redis.call('strlen', key)
    elseif type == 'list' then
        size = redis.call('llen', key)
    elseif type == 'set' then
        size = redis.call('scard', key)
    elseif type == 'zset' then
        size = redis.call('zcard', key)
    elseif type == 'hash' then
        size = redis.call('hlen', key)
    end

    local attrs = {['id'] = key, ['type'] = type, ['ttl'] = pttl, ['size'] = size}

    table.insert(keyAttrs, attrs)    
end

return cjson.encode(keyAttrs)";

            var json = Redis.ExecCachedLua(LuaScript, sha1 =>
                                           Redis.ExecLuaShaAsString(sha1, request.Query, limit.ToString()));

            var searchResults = json.FromJson <List <SearchResult> >();

            return(new SearchRedisResponse
            {
                Results = searchResults
            });
        }
        public object Any(SearchRedis request)
        {
            var limit = request.Take.GetValueOrDefault(AppSettings.Get("query-limit", 100));

            const string LuaScript = @"
            local limit = tonumber(ARGV[2])
            local pattern = ARGV[1]
            local cursor = 0
            local len = 0
            local keys = {}

            repeat
            local r = redis.call('scan', cursor, 'MATCH', pattern, 'COUNT', limit)
            cursor = tonumber(r[1])
            for k,v in ipairs(r[2]) do
            table.insert(keys, v)
            len = len + 1
            if len == limit then break end
            end
            until cursor == 0 or len == limit

            if len == 0 then
            return '[]'
            end

            local keyAttrs = {}
            for i,key in ipairs(keys) do
            local type = redis.call('type', key)['ok']
            local pttl = redis.call('pttl', key)
            local size = 0
            if type == 'string' then
            size = redis.call('strlen', key)
            elseif type == 'list' then
            size = redis.call('llen', key)
            elseif type == 'set' then
            size = redis.call('scard', key)
            elseif type == 'zset' then
            size = redis.call('zcard', key)
            elseif type == 'hash' then
            size = redis.call('hlen', key)
            end

            local attrs = {['id'] = key, ['type'] = type, ['ttl'] = pttl, ['size'] = size}

            table.insert(keyAttrs, attrs)
            end

            return cjson.encode(keyAttrs)";

            var json = Redis.ExecCachedLua(LuaScript, sha1 =>
                Redis.ExecLuaShaAsString(sha1, request.Query, limit.ToString()));

            var searchResults = json.FromJson<List<SearchResult>>();

            return new SearchRedisResponse
            {
                Results = searchResults
            };
        }
Example #3
0
        public object Any(SearchRedis request)
        {
            var limit = request.Take.GetValueOrDefault(100);

            var keys = Redis.ScanAllKeys(pattern: request.Query, pageSize: limit)
                .Take(limit).ToList();

            var keyTypes = new Dictionary<string, string>();
            var keyTtls = new Dictionary<string, long>();
            var keySizes = new Dictionary<string, long>();

            if (keys.Count > 0)
            {
                using (var pipeline = Redis.CreatePipeline())
                {
                    keys.Each(key =>
                        pipeline.QueueCommand(r => ((RedisNativeClient)r).Type(key), x => keyTypes[key] = x));

                    keys.Each(key =>
                        pipeline.QueueCommand(r => ((RedisNativeClient)r).PTtl(key), x => keyTtls[key] = x));

                    pipeline.Flush();
                }

                using (var pipeline = Redis.CreatePipeline())
                {
                    foreach (var entry in keyTypes)
                    {
                        var key = entry.Key;
                        switch (entry.Value)
                        {
                            case "string":
                                pipeline.QueueCommand(r => ((RedisNativeClient)r).StrLen(key), x => keySizes[key] = x);
                                break;
                            case "list":
                                pipeline.QueueCommand(r => r.GetListCount(key), x => keySizes[key] = x);
                                break;
                            case "set":
                                pipeline.QueueCommand(r => r.GetSetCount(key), x => keySizes[key] = x);
                                break;
                            case "zset":
                                pipeline.QueueCommand(r => r.GetSortedSetCount(key), x => keySizes[key] = x);
                                break;
                            case "hash":
                                pipeline.QueueCommand(r => r.GetHashCount(key), x => keySizes[key] = x);
                                break;
                        }
                    }

                    pipeline.Flush();
                }
            }

            return new SearchRedisResponse
            {
                Results = keys.Map(x => new SearchResult
                {
                    Id = x,
                    Type = keyTypes.GetValueOrDefault(x),
                    Ttl = keyTtls.GetValueOrDefault(x),
                    Size = keySizes.GetValueOrDefault(x),
                })
            };
        }