Data source: Redis


1Presentation

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\Datasources\Redis. By convention, we will assume that you have named this connection ndb in the etc/temma.php file (see the configuration documentation).

The connection is then available in the controller by writing:

$ndb = $this->ndb;

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

$ndb = $loader->dataSources->ndb;
$ndb = $loader->dataSources['ndb'];

2Configuration

In the etc/temma.php file (see configuration documentation), you declare the DSN (Data Source Name) used to connect to the Redis server.

The DSN used to connect to a Redis server is written as: redis://SERVEUR[:PORT][/BASE]
The default port number is 6379.
The base number defaults to 0 (zero).
Example: redis://localhost

If the Redis server is running on the same machine, it's possible to connect using a Unix socket, to avoid network latency. In this case, the DSN is: redis-sock://PATH[#BASE]
The path must lead to the Unix socket (often /var/run/redis.sock).
The default base number is 0 (zero).
Example: redis-sock:///var/run/redis.sock#2


3Unified calls

3.1Array-like access

// verification of data existence
if (isset($ndb['key1']))
doSomething();

// read data (deserialized)
$data = $ndb['key1'];

// data writing (serialized)
$ndb['key1'] = $value;

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

// elements count
$nbr = count($ndb);

3.2General methods

// verification of data existence
if ($ndb->isSet('user:1'))
doSomething();

// data deletion
$ndb->remove('user:1');

// multiple data deletion
$ndb->mRemove(['user:1', 'user:2', 'user:3']);

// deleting data from a regular expression
$ndb->clear('user:.*');

// delete all data
$ndb->flush();

3.3Management of complex serialized data

// search for keys matching a regular expression
$users = $ndb->search('user:.*');

// prefix-based key search, with data recovery (deserialized)
$users = $ndb->search('user:.*', true);

// read data (deserialized)
$user = $ndb->get('user:1');
// read data with default value
$color = $ndb->get('color', 'blue');
// read data with data creation if necessary
$user = $ndb->get("user:$userId", function() use ($userId) {
return $this->dao->get($userId);
});

// read multiple data (deserialized)
$users = $ndb->mGet(['user:1', 'user:2', 'user:3']);

// data writing (serialized)
$ndb->set('user:1', $userData);
// write (serialized) data with a lifetime of one hour
$ndb->set('user:1', $userData, 3600);

// multiple data writing (serialized)
$ndb->mSet([
'user:1' => $user1data,
'user:2' => $user2data,
'user:3' => $user3data,
]);
// write multiple data, with a lifetime of one hour
$ndb->mSet([
'user:1' => $user1data,
'user:2' => $user2data,
], 3600);

3.4Raw data management

// read data (raw)
$html = $ndb->read('page:home');
// read data with default value
$html = $ndb->read('page:home',
		 '<html><body><h1>Homepage</h1><body><html>');
// read data with data creation if necessary
$html = $ndb->read('page:home', function() {
return file_get_contents('/path/to/homepage.html');
});

// read multiple (raw) data
$pages = $ndb->mRead(['page:home', 'page:admin', 'page:products']);

// copy data to a local file
$ndb->copyFrom('page:home', '/path/to/newpage.html');
// copy data to local file, with default value
$ndb->copyFrom('page:home', '/path/to/newpage.html', $defaultHtml);
// copy data to a local file, with data creation if necessary
$ndb->copyFrom('page:home', '/path/to/newpage.html', function() {
return file_get_contents('/path/to/oldpage.html');
});
// copy data to a local file, with data creation if necessary
// (with a lifetime of one hour)
$ndb->copyFrom('page:home', '/path/to/newpage.html', function() {
return file_get_contents('/path/to/oldpage.html');
}, 3600);

// data writing (raw)
$ndb->write('color:blue', '#0000ff');
// data writing (raw) with a lifetime of one hour
$ndb->write('color:blue', '#0000ff', 3600);

// write multiple (raw) data
$ndb->mWrite({
'color:blue'  => '#0000ff',
'color:red'   => '#ff0000',
'color:green' => '#00ff00',
]);
// write multiple data with a lifetime of one hour
$ndb->mWrite({
'color:blue'  => '#0000ff',
'color:red'   => '#ff0000',
], 3600);

// write (raw) data from a local file
$ndb->copyTo('page:home', '/path/to/homepage.html');
// write data from a local file, with a lifetime of one hour
$ndb->copyTo('page:home', '/path/to/homepage.html', 3600);

// write multiple (raw) data from local files
$ndb->mCopyTo([
'page:home'     => '/path/to/homepage.html',
'page:admin'    => '/path/to/admin.html',
'page:products' => '/path/to/products.html',
]);
// write multiple data from local files with a lifetime of one hour
$ndb->mCopyTo([
'page:home'  => '/path/to/homepage.html',
'page:admin' => '/path/to/admin.html',
], 3600);