Documentation
Data source: Redis
Table of Contents ▼
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\Datasources\Redis. 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']
Configuration
In the temma.json 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
Unified calls
Array-like access
// verification of data existence
if (isset($this->ndb['key1']))
doSomething();
// read data (deserialized)
$data = $this->ndb['key1'];
// data writing (serialized)
$this->ndb['key1'] = $value;
// data deletion
unset($this->ndb['key1']);
// elements count
$nbr = count($this->ndb);
General methods
// verification of data existence
if ($this->ndb->isSet('user:1'))
doSomething();
// data deletion
$this->ndb->remove('user:1');
// multiple data deletion
$this->ndb->mRemove(['user:1', 'user:2', 'user:3']);
// deleting data from a regular expression
$this->ndb->clear('user:.*');
// delete all data
$this->ndb->flush();
Management of complex serialized data
// search for keys matching a regular expression
$users = $this->ndb->search('user:.*');
// prefix-based key search, with data recovery (deserialized)
$users = $this->ndb->search('user:.*', true);
// read data (deserialized)
$user = $this->ndb->get('user:1');
// read data with default value
$color = $this->ndb->get('color', 'blue');
// read data with data creation if necessary
$user = $this->ndb->get("user:$userId", function() use ($userId) {
return $this->dao->get($userId);
});
// read multiple data (deserialized)
$users = $this->ndb->mGet(['user:1', 'user:2', 'user:3']);
// data writing (serialized)
$this->ndb->set('user:1', $userData);
// write (serialized) data with a lifetime of one hour
$this->ndb->set('user:1', $userData, 3600);
// multiple data writing (serialized)
$this->ndb->mSet([
'user:1' => $user1data,
'user:2' => $user2data,
'user:3' => $user3data,
]);
// write multiple data, with a lifetime of one hour
$this->ndb->mSet([
'user:1' => $user1data,
'user:2' => $user2data,
], 3600);
Raw data management
// read data (raw)
$html = $this->ndb->read('page:home');
// read data with default value
$html = $this->ndb->read('page:home',
'<html><body><h1>Homepage</h1><body><html>');
// read data with data creation if necessary
$html = $this->ndb->read('page:home', function() {
return file_get_contents('/path/to/homepage.html');
});
// read multiple (raw) data
$pages = $this->ndb->mRead(['page:home', 'page:admin', 'page:products']);
// copy data to a local file
$this->ndb->copyFrom('page:home', '/path/to/newpage.html');
// copy data to local file, with default value
$this->ndb->copyFrom('page:home', '/path/to/newpage.html', $defaultHtml);
// copy data to a local file, with data creation if necessary
$this->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)
$this->ndb->copyFrom('page:home', '/path/to/newpage.html', function() {
return file_get_contents('/path/to/oldpage.html');
}, 3600);
// data writing (raw)
$this->ndb->write('color:blue', '#0000ff');
// data writing (raw) with a lifetime of one hour
$this->ndb->write('color:blue', '#0000ff', 3600);
// write multiple (raw) data
$this->ndb->mWrite({
'color:blue' => '#0000ff',
'color:red' => '#ff0000',
'color:green' => '#00ff00',
]);
// write multiple data with a lifetime of one hour
$this->ndb->mWrite({
'color:blue' => '#0000ff',
'color:red' => '#ff0000',
], 3600);
// write (raw) data from a local file
$this->ndb->copyTo('page:home', '/path/to/homepage.html');
// write data from a local file, with a lifetime of one hour
$this->ndb->copyTo('page:home', '/path/to/homepage.html', 3600);
// write multiple (raw) data from local files
$this->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
$this->ndb->mCopyTo([
'page:home' => '/path/to/homepage.html',
'page:admin' => '/path/to/admin.html',
], 3600);
Previous: | Memcache |
Next: | File |
Table of Contents
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- External libraries : How to use external function libraries
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependency injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
-
Data sources :
Unified data access management
- SQL : Access to relational databases
- Memcache : Access to Memcached servers
- Redis : Access to Redis servers
- File : Access to local file storage
- S3 : Access to Amazon S3 storage
- Socket : Network communication
- SQS : Access to Amazon SQS message queues
- Beanstalk : Access to Beanstalkd message queue servers
- Smsmode : To send text messages to mobile phones
- Slack : To send notifications on Slack
- Pushover : To send push notifications to cell phones
- Model : How to use DAOs to access databases
- Execution flow : How to manage the execution flow, between plugins and the controller
- Plugins : How to use plugins, and create your own plugins to modularize your code
- Attributes : How to filter access to controllers and actions
- Tests : To write automated integration tests.
- Command-line interface : To create executable scripts on the command line, automatically initialized by Temma
- Helpers : Items offered by Temma to help you in various circumstances