skip to Main Content

We can use below commands in laravel.

$user = Redis::get('user:profile:'.$id); 
$values = Redis::lrange('names', 5, 10);
$values = Redis::command('lrange', ['name', 5, 10]);

but can’t use memory usage keyname command with laravel redis facade.

2

Answers


  1. Short answer no you can’t execute. Let me explain;

    public function createCommand($commandID, array $arguments = array())
    {
        $commandID = strtoupper($commandID);
    
        if (!isset($this->commands[$commandID])) {
            throw new ClientException("Command '$commandID' is not a registered Redis command.");
        }
    
        $commandClass = $this->commands[$commandID];
        $command = new $commandClass();
        $command->setArguments($arguments);
    
        if (isset($this->processor)) {
            $this->processor->process($command);
        }
    
        return $command;
    }
    

    this method is used when you invoke methods and it uses this abstract method to decide whether you can invoke a method or not.

    abstract protected function getSupportedCommands();
    

    getSupportedCommands method looks like this;

    public function getSupportedCommands()
    {
        return array(
            /* ---------------- Redis 1.2 ---------------- */
    
            /* commands operating on the key space */
            'EXISTS'                    => 'PredisCommandKeyExists',
            'DEL'                       => 'PredisCommandKeyDelete',
            'TYPE'                      => 'PredisCommandKeyType',
            'KEYS'                      => 'PredisCommandKeyKeys',
            'RANDOMKEY'                 => 'PredisCommandKeyRandom',
            'RENAME'                    => 'PredisCommandKeyRename',
            'RENAMENX'                  => 'PredisCommandKeyRenamePreserve',
            'EXPIRE'                    => 'PredisCommandKeyExpire',
            'EXPIREAT'                  => 'PredisCommandKeyExpireAt',
            'TTL'                       => 'PredisCommandKeyTimeToLive',
            'MOVE'                      => 'PredisCommandKeyMove',
            'SORT'                      => 'PredisCommandKeySort',
            'DUMP'                      => 'PredisCommandKeyDump',
            'RESTORE'                   => 'PredisCommandKeyRestore',
            ....
            ...
            ..
    

    memory usage command is not available in this method. You may check /vendor/predis/predis/src/Profile/RedisVersion300.php or any other class in Profile folder – it is not defined in there.

    The reason is memory usage method is available since Redis version 4.0.0. This package is supporting commands until Redis version 3.0.0 as it can be seen from class names such as RedisVersion240, RedisVersion300 etc.

    Login or Signup to reply.
  2. The command is not defined for that, but you can use eval:

    Redis::connection('connection')->eval("return redis.call('memory', 'usage', 'keyname')", 0)
    

    Or if you want to use the keyname as a paramater:

    Redis::connection('connection')->eval("return redis.call('memory', 'usage', KEYS[1])", 1, 'keyname');
    

    Redis LUA scripts are executed the same way in laravel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search