-1

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.

Ersoy
  • 8,816
  • 6
  • 34
  • 48

2 Answers2

1

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

dragontree
  • 1,709
  • 11
  • 14
0

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'                    => 'Predis\Command\KeyExists',
        'DEL'                       => 'Predis\Command\KeyDelete',
        'TYPE'                      => 'Predis\Command\KeyType',
        'KEYS'                      => 'Predis\Command\KeyKeys',
        'RANDOMKEY'                 => 'Predis\Command\KeyRandom',
        'RENAME'                    => 'Predis\Command\KeyRename',
        'RENAMENX'                  => 'Predis\Command\KeyRenamePreserve',
        'EXPIRE'                    => 'Predis\Command\KeyExpire',
        'EXPIREAT'                  => 'Predis\Command\KeyExpireAt',
        'TTL'                       => 'Predis\Command\KeyTimeToLive',
        'MOVE'                      => 'Predis\Command\KeyMove',
        'SORT'                      => 'Predis\Command\KeySort',
        'DUMP'                      => 'Predis\Command\KeyDump',
        'RESTORE'                   => 'Predis\Command\KeyRestore',
        ....
        ...
        ..

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.

Ersoy
  • 8,816
  • 6
  • 34
  • 48
  • thanks, for your answer but how do I know which key store how much memory? we can check by `memory usage keyname` in redis-cli but I want to display how much memory used by each key – parekh pankaj May 20 '20 at 14:43
  • it will be costly to scan all the `keys` in redis and executing `memory usage` one by one. I am not sure about the domain but you may try to run `redis-cli --bigkeys` to see some of the biggest ones depending on the data type(string, hash, sets etc) @parekhpankaj – Ersoy May 20 '20 at 18:41
  • @parekhpankaj please check this link https://gist.github.com/epicserve/5699837 (i am not the owner of this script) it looks like it covers your case. – Ersoy May 20 '20 at 18:43
  • I can't run this script in laravel. I need to run through the laravel controller and then need to display keys memory in view. – parekh pankaj Jun 03 '20 at 09:50
  • @parekhpankaj you shouldn't run this one regularly too. it has `keys` command. still if you want use this https://stackoverflow.com/questions/46208801/laravel-5-how-to-create-an-artisan-command-to-execute-bash-script - save the results in a text file/database table and in another method read from the file/database table – Ersoy Jun 03 '20 at 11:21