Example #1
0
 public function testEscapeQuotes()
 {
     $keys = $this->module->executeCommand('KEYS', ['stress:*']);
     foreach ($keys as $key) {
         $qkey = Redisman::quoteValue($key);
         Debug::debug($key . '  - ' . $qkey);
         $res = $this->module->executeCommand('EVAL', [$this->buildExistScript($qkey), 0]);
         $this->assertEquals(1, $res);
         $res = $this->module->executeCommand('EXISTS', [$key]);
         $this->assertEquals(1, $res);
     }
 }
Example #2
0
    /**
     * Prepare lua search script for greedy search type
     * @return string
     */
    protected function scriptBuilderGreedy()
    {
        $pattern = Redisman::quoteValue($this->pattern);
        $typecond = $this->typeCondBuilder();
        $scriptScan = <<<EOF
local all_keys = {};
local keys = {};
local done = false;
local cursor = "0"
local count=0;
local size=0
local tp
repeat
    local result = redis.call("SCAN", cursor, "match", {$pattern}, "count", 50)
    cursor = result[1];
    keys = result[2];
    for i, key in ipairs(keys) do
        tp=redis.call("TYPE", key)["ok"]
           if {$typecond} then
               if tp == "string" then
                   size=redis.call("STRLEN", key)
                elseif tp == "hash" then
                    size=redis.call("HLEN", key)
                elseif tp == "list" then
                    size=redis.call("LLEN", key)
                elseif tp == "set" then
                    size=redis.call("SCARD", key)
                elseif tp == "zset" then
                    size=redis.call("ZCARD", key)
                else
                    size=9999
                end
               all_keys[#all_keys+1] = {key, tp, size, redis.call("TTL", key)};
           end
        if {$typecond} then
           count=count+1
        end
    end
    if cursor == "0" then
        done = true;
    end
until done
all_keys[#all_keys+1]=count;
return all_keys;
EOF;
        $scriptKeys = <<<EOF
local all_keys = {};
local count=0;
local size=0
local tp
    local result = redis.call("KEYS", {$pattern})
    for i, key in ipairs(result) do
        tp=redis.call("TYPE", key)["ok"]
           if {$typecond} then
               if tp == "string" then
                   size=redis.call("STRLEN", key)
                elseif tp == "hash" then
                    size=redis.call("HLEN", key)
                elseif tp == "list" then
                    size=redis.call("LLEN", key)
                elseif tp == "set" then
                    size=redis.call("SCARD", key)
                elseif tp == "zset" then
                    size=redis.call("ZCARD", key)
                else
                    size=9999
                end
               all_keys[#all_keys+1] = {key, tp, size, redis.call("TTL", key)};
           end
       if {$typecond} then
           count=count+1
        end
    end
all_keys[#all_keys+1]=count;
return all_keys;
EOF;
        return Redisman::getInstance()->searchMethod == 'SCAN' ? $scriptScan : $scriptKeys;
    }
Example #3
0
    /**
     * Generate script for searching key information
     *
     * @param string $key
     *
     * @return string
     */
    protected static function infoScript($key)
    {
        $key = Redisman::quoteValue($key);
        $script = <<<EOF
local tp=redis.call("TYPE", {$key})["ok"]
local size=9999
if tp == "string" then
    size=redis.call("STRLEN", {$key})
elseif tp == "hash" then
    size=redis.call("HLEN", {$key})
elseif tp == "list" then
    size=redis.call("LLEN", {$key})
elseif tp == "set" then
    size=redis.call("SCARD", {$key})
elseif tp == "zset" then
    size=redis.call("ZCARD", {$key})
else
    size=9999
end
local info={tp, size, redis.call("TTL", {$key}),
            redis.call("OBJECT","REFCOUNT", {$key}),redis.call("OBJECT","IDLETIME", {$key}),
            redis.call("OBJECT", "ENCODING", {$key})};
return info;
EOF;
        return $script;
    }