Rediska

Follow me on twitter…

Strings

String key is a basic Redis structure. Rediska provides object-oriented way to work with keys.

Example

<?php

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

// Initialize key with name "keyName"
$key = new Rediska_Key('keyName');

// Set value
$key->setValue('value');

// Get length
print count($key); #=> 5

// Expire key after 5 minutes
$key->expire(5 * 60);

// Вы можете сохранять также масивы и объекты
$key->setValue(array('value', 'value2', 'value3'));

// Get value
$value = $key->getValue();

// Delete key
$key->delete();

?>

"getOrSetValue" magic method

Also Rediska_Key provides a handy proxying method to set new value if key is not currently set:

<?php

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

// Initialize key
$key = new Rediska_Key('keyName');

// Old way
$value = $key->getValue();
if ($value === null) {
    $value = $exampleObject->getNewValue();
    $key->setValue($value);
}

// New way
$value = $key->getOrSetValue($exampleObject)
             ->getNewValue();

// You can also get and set value from object's propery
$value = $key->getOrSetValue($exampleObject)
             ->newValue;
// or set the returning value of function (time() for example)
$value = $key->getOrSetValue()->time();

?>

See all available methods.

Comments

Fork me on GitHub