Rediska

Follow me on twitter…

Extending Rediska

You can add, delete or replace commands without creating your own class with command manager:

<?php

// Initialize Rediska
$rediska = new Rediska();

// Add command with name 'myCommand'
Rediska_Commands::add('myCommand', 'MyCommandClass');

// Execute you command
$rediska->myCommand();

// Remove you command
Rediska_Commands::remove('myCommand');

?>

Rediska command internally is a class implementing Rediska_Command_Interface:

<?php

interface Rediska_Command_Interface
{
    public function __construct(Rediska $rediska, $name, $arguments = array());
    public function write();
    public function read();
    public function execute();
    public function isAtomic();
    public function isQueued();
}

?>

If you need your own custom command, best choice is to use abstract class Rediska_Command_Abstract, which implements all basic functionality and allows you to add some custom logic:

<?php

class MyCommandClass extends Rediska_Command_Abstract
{
  // This method called when command initialized
  public function create($name)
  {
    $namespace = $this->getRediska()
                      ->getOption('namespace');

    $command = array('GET',
                     $namespace . $name);

    $connection = $this->getRediska()
                ->getConnectionByKeyName($name);

    // You may return array of execs
    return new Rediska_Connection_Exec($connection, $command);
  }

  // This method called after reading response from connection
  public function parseResponse($response)
  {
    $value = $this->getRediska()
                  ->getSerializer()
                  ->unserialize($response);
    // You may get command argument through object property
    return $this->name . ': ' .  $value;
  }
}

?>

Comments

Fork me on GitHub