PHP redis usage details. Redis can be used in php in two ways: Predis and phpredis. Phpredis is an extension of php, which is a high-performance linked list written in C language. This article describes how to use Predis. Predis is written in PHP.
PHP redis usage details. Redis can be used in php in two ways: Predis and phpredis. Phpredis is an extension of php, which is a high-performance linked list written in C language. This article describes how to use Predis.
Predis is an official Redis client written in the PHP native language. Since Predis adopts the namespace method, Predis requires the PHP version to be 5.3 at the lowest.
Predis is open-source and hosted on GitHub https://github.com/nrk/predis /. Download the entire folder and copy it to the project directory.
// Introduce the autoload. Php file
Require './predis/autoload. Php ';
// Instantiate
$ Redis = New PredisClient ();
/* This is a simplified version, equivalent to $ redis = New PredisClient (array (
* 'Scheme '=> 'tcp ',
* 'Host' => '2017. 0.0.1'
* 'Port' => 6379
*));
*/
// GET
$ Redis-> get ('key ');
// LPUSH
$ Redis-> lpush ('key', '1', '2', '3 ');
// MSET is equivalent to $ redis-> MSET ('Article: 1: title', 'biaoti', 'Article: 1: content', 'neirong ', 'ctime ', 'shijian ');
$ Article = array ('Article: 1: title' => 'biaoti', 'Article: 1: content' => 'neirong ', 'Article: 1: ctime' => 'shijian ');
$ Redis-> MSET ('key', $ article );
// MGET
$ ArticleKeys = array_keys ($ article );
$ Redis-> MGET ($ articleKeys );
// SORT
// SORT articleList BY article: *-> time LIMIT 0 10 GET article: *-> title GET # desc alpha store storeKey
$ Sort = array (
'By' => 'Article: *-> time ',
'Limit' => array (0, 10 ),
'Get' => array ('Article: *-> title ','#'),
'Sort '=> 'desc ',
'Alpha' => true,
'Store' => 'storekey'
);
After Predis is encapsulated, it is very convenient to use. The introduction of correlated arrays is very efficient in development.