PHP code example of dynamonet / redis

1. Go to this page and download the library: Download dynamonet/redis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dynamonet / redis example snippets


use Dynamo\Redis\Client as RedisClient;

$redis = new RedisClient([
    'host' => 'localhost',
    'port' => 6379,
    //'timeout' => xxx,
    //'retry_interval' => xxx, //
]);

// you can now start sending commands, without the need for an explicit "connect" call
$redis->set('mykey', 'value');

$result = $redis->pipeline()
    ->set('key1', 'value1')
    ->incr('key2')
    ->get('key2')
    ->exec();

var_dump($result); // an array with the result for every queued command

// Register my custom functions
$redis->loadFunctionScript($path_to_my_functions_script, [
    'functionA' => 1,
    'functionB' => 2,
    'functionC' => 0,
]);

// Invoking my functions
$redis->functionA('Hello');
$redis->functionB('World', 2);
$redis->functionA();


use Dynamo\Redis\LuaScript;

$redis->loadScript(
    LuaScript::fromFile(
        $path_to_my_lua_script,
        2, // amount of mandatory arguments
        'myluacmd' //alias for the script
    ),
    true //register the script NOW. If "false" is provided, it will be lazy-loaded
);

//now, you can call your Lua command as if it were a regular Redis command
$redis->myluacmd('blablabla', 2);

//you can also call your custom commands in a pipeline
$result = $redis->pipeline()
    ->set('key1', 'value1')
    ->incr('key2')
    ->get('key2')
    ->myluacmd('blablabla', 2)
    ->exec();