Documentation

Redis

Presentation

Temma can manage non-relational databases running on the Redis server.

If you have correctly configured the connection parameters to the Redis database, Temma automatically creates an object of type \Temma\Base\NDatabase. By convention, we will assume that you have named this connection ndb in the temma.json file (see the configuration documentation).

The connection is then available in the controller by writing:

$this->ndb

In the other objects managed by the dependency injection component, the connection to the database is accessible by writing:

$loader->dataSources['ndb']

Array-like access

It is possible to access the data as through an associative array:

// write data
$ndb['key1'] = 'value1';

// read data
$value1 = $ndb['key1'];

// delete data
unset($ndb['key1']);

// check data
if (isset($ndb['key1']))
    print("???");

Methods

set
set(mixed $key, mixed $value, [int $timeout=0],
    [bool $createOnly=false]) : mixed

This method is used to add a key-value pair in the Redis database. It returns the old value associated with this key.
The $timeout parameter is used to give the data a lifetime (in seconds).
The $createOnly parameter is used to say that the data should only be added if it does not already exist.

$ndb->set('key', 'value');

It can also be used to define several values at once:

$ndb->set([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
]);

It returns null in the case of a multiple addition.


get
get(mixed $key, [Closure $callback]) : mixed

The get() method is used to retrieve a value from the Redis database.

$result = $ndb->get('key');

It can also be used to retrieve many values at once:

$values = $ndb->get(['key1', 'key2', 'key3');
print_r($values);
/*
  Will show :
  Array
  (
    "key1" => "value1",
    "key2" => "value2",
    "key3" => "value3"
  )
*/

remove
remove(mixed $key) : \Temma\Base\NDatabase

The remove() method is used to delete one or more keys from the Redis database:

// single removal
$ndb->remove('key');

// multiple removals
$ndb->remove(['key1', 'key2', 'key3']);

It returns the instance of the object.


search
search(string $pattern, [bool $getValues=false]) : array

The search() method returns a list of keys whose name matches the pattern given in parameter.

$list = $ndb->search('key*');
print_r($list);
/*
  Will show :
  Array
  (
    "key1",
    "key2",
    "key3
  )
*/

If the second parameter is set to true, the data associated with the keys is returned at the same time.

$list = $ndb->search('key*', true);
print_r($list);
/*
  Will show :
  Array
  (
    "key1" => "value1", 
    "key2" => "value2",
    "key3" => "value3"
  )
*/
Previous: SQL
Next: Generic DAO

Table of Contents