📜 ⬆️ ⬇️

New Redis 2.0 and Rediska 0.5.0!

Rediska Dear friends! Last week there was a stable release of the wonderful key-value base Redis version 2.0 with an impressive amount of innovations. This news has especially pleased us, since we have been using Redis for a year in our loaded projects and the impressions are only positive. We have updated the PHP Rediska client, adding support for new features.




The main innovations of Redis 2.0


Transactions (MULTI / EXEC / DISCARD)

Transactions allow you to perform a series of commands as one atomic operation. Here is a social example:
')
The first user is willing to be friends with the second user:
  1. We add the first user ID to the set ( Set is an unordered set of unique elements) of the second user users: 2: requests in which we store requests.
The second user does not mind:
  1. Remove from the set of users: 2: requests the ID of the first user.
  2. Add the first user ID to the set of users: 2: friends with the friends of the second user.
  3. Add the second user ID to the set of users: 1: friends with friends of the first, for reciprocity.
If one of the operations fails, or we have competitive operations, then a collapse cannot be avoided. Here transactions come to our aid.

<?php $rediska = new Rediska(); /* *         */ $rediska->addToSet('users:2:requests', 1); /* *     */ $rediska->transaction()->deleteFromSet('users:2:requests', 1) ->addToSet('users:2:friends', 1) ->addToSet('users:1:friends', 2) ->execute(); ?> 

BLPOP / BRPOP blocking operation

Atomic operations of BLPOP and BRPOP receive and delete the first or last element from the list ( List is an ordered list of elements). And if the list is empty, then the operations block the client's connection until another client puts an element there.

For example, a user uploads a Britney mp3 track in 320 Kb / s. We need to convert it to 192 Kb / s. To do this, we add a conversion task to the queue. The daemon gets the task from the queue and converts the file. For the implementation of the queue lists are great.

 <?php $rediska = new Rediska(); //      $queue = new Rediska_Key_List('queue'); $queue[] = 'britney_spears__and_then_we_kiss.mp3'; //    while(true) { //      //   ,           $file = $queue->popBlocking(); convertFile($file); } ?> 

Publish / Subscribe

One of the most remarkable innovations is the implementation of the Publish / Subscribe message queue paradigm. The PUBLISH operation adds a message to the channel, not to specific recipients and does not know anything about them. The SUBSCRIBE operation subscribes to a channel or channels and receives messages.

The simplest example that comes to mind is the implementation of chat rooms (although Publish / Subscribe has much more useful applications).

 <?php $rediska = new Rediska(); //        //     timeout   foreach($rediska->subscribe('main') as $nickAndMessage) { list($nick, $message) = $nickAndMessage; print "$nick: $message"; } ?> 
 <?php $rediska = new Rediska(); //       $rediska->publish('main', array('', ' -   !')); ?> 

Many thanks to Jura octave for the initiative and assistance in the implementation!

New Key Type Hash

A hash is a key whose value is essentially an associative PHP array, but unlike storing a string in a serialized array, it provides atomic operations for working with fields and their values.

In hashes, it is very convenient to store objects or group string keys into them for more efficient use.

 <?php $rediska = new Rediska(); class User extends Rediska_Key_Hash { public function __construct($id) { parent::__construct("users:$id"); } } //    $user = new User(1); $user->id = 1; $user['name'] = ''; //         $user->friendsCount = 0; //    $user = new User(1); $user->increment('friendsCount'); //    foreach($user as $field => $value) { print "$field => $value"; } ?> 

Virtual memory

Virtual memory helps to store more data in Redis than the size of RAM allows. In short, Redis removes key values ​​to which you are least likely to use from RAM to disk.

Virtual memory is effective if you often use only a small percentage of the keys, or if the keys have large values.

Redis server configuration

The new CONFIG operation allows you to read and reconfigure the Redis server.

 <?php $rediska = new Rediska(); //    $config = $rediska->config(); //   print $config->maxmemory; //         print $config['maxmemory']; //   $config->maxmemory = 10000; //      (glob) foreach($config['max*'] as $name => $value) { print "$name => $value\n"; } //     foreach($config as $name => $value) { print "$name => $value\n"; } ?> 

New operations for working with string keys

 <?php $rediska = new Rediska(); //     'value' $rediska->set('key', 'value'); //   '-shmalue'   $rediska->append('key', '-shmalue'); //    print $rediska->substring('key', 6); #=> malue // ""  set + expire $rediska->setAndExpire('key', 'value', 60 * 5); ?> 

One more thing from the new Radish


Instance manager

Your application may have components (cache, sessions, ...) that require different Radish instances with different options (namespace, server, ...).
The manager class is committed to storing these instances and providing methods for obtaining, adding, and deleting them.
The manager can also store, in addition to objects, also arrays of Radish options and create objects upon the first request to them (lazy-load).

 <?php //  'default'  $rediska = new Rediska(); //  'default'    $rediska = Rediska_Manager::get(); print $rediska->getName(); #=> default //  'cache'  $rediska = new Rediska(array('name' => 'cache', 'namespace' => 'Cache_')); //  'cache'    $rediska = Rediska_Manager::get('cache'); print $rediska->getName(); #=> cache //   'sessions'  Rediska_Manager::add(array('name' => 'sessions', 'namespace' => 'Sessions_')); //        $rediska = Rediska_Manager::get('sessions'); print $rediska->getName(); #=> sessions ?> 

New serializer

In the new version, Radish serializes only arrays and objects, strings and numbers are saved as is (there are no problems with the data saved by previous versions).

Using the serializerAdapter option, you can specify the serialization method:
Autoloader

Radish got rid of require_once and the need to add a path to include_path .

Transaction monitor

A MONITOR operation has been implemented that allows you to monitor real-time operations running on Redis servers.

 <?php $rediska = new Rediska(); //         $monitor = $rediska->monitor(60 * 2); //      Redis  $monitor = $rediska->on('server1')->monitor(); //   foreach($monitor as $timestamp => $command) { print "$timestamp => $command"; } ?> 

In custody...

I will try not to load you anymore, let me just say that we began to rewrite the Radish in C ++ in the form of a PHP extension and we will be happy to participate.

Source: https://habr.com/ru/post/103823/


All Articles