public function test_parallel()
 {
     $MC = $this->getMemcached();
     $MC->flush();
     unset($MC);
     $Storage = new MemcachedStorage(['servers' => [explode(':', MEMCACHED_TEST_SERVER)]]);
     $Parallel = new Parallel($Storage);
     $start = microtime(true) + 2;
     for ($i = 1; $i <= 5; $i++) {
         $Parallel->run('foo' . $i, function () use($start) {
             $MemcachedTags = new MemcachedTags($MC = $this->getMemcached());
             while (microtime(true) < $start) {
                 // wait for start
             }
             for ($i = 1; $i <= 1000; ++$i) {
                 $j = mt_rand(1, 100);
                 $MemcachedTags->setKeyWithTags('key_1_' . $j, $j, 'tag1');
                 $MemcachedTags->setKeysWithTags(['key_2_' . $j => $j, 'key_3_' . $j => $j], ['tag2', 'tag3']);
             }
             return 1;
         });
     }
     $MemcachedTags = new MemcachedTags($MC = $this->getMemcached());
     while (microtime(true) < $start) {
         // wait for start
     }
     for ($i = 1; $i <= 1000; ++$i) {
         $MemcachedTags->deleteKeysByTag('tag1');
         $MemcachedTags->deleteKeysByTags(['tag2', 'tag3']);
     }
     $Parallel->wait(['foo1', 'foo2', 'foo3', 'foo4', 'foo5']);
     $keys1 = $MemcachedTags->getKeysByTag('tag1');
     $keys2 = $MemcachedTags->getKeysByTag('tag2');
     $keys3 = $MemcachedTags->getKeysByTag('tag3');
     for ($i = 1; $i <= 100; ++$i) {
         $value = $MC->get('key_1_' . $i);
         $this->assertSame($value ? true : false, in_array('key_1_' . $i, $keys1));
         $value = $MC->get('key_2_' . $i);
         $this->assertSame($value ? true : false, in_array('key_2_' . $i, $keys2));
         $value = $MC->get('key_3_' . $i);
         $this->assertSame($value ? true : false, in_array('key_3_' . $i, $keys3));
     }
     $MC->flush();
 }
 /**
  * @see MemcachedTags::setKeysWithTags
  */
 public function test_setKeysWithTags()
 {
     $MC = static::$Memcached;
     $MemcachedTags = new MemcachedTags($MC);
     $MC->flush();
     $this->assertSame(true, $MemcachedTags->setKeysWithTags(['user:1' => 'Alexander', 'user:2' => 'Irina', 'user:5' => 'Dom'], ['city:London', 'country:UK']));
     $this->assertSame('user:1||user:2||user:5', $MC->get('tag_t_city:London'));
     $this->assertSame('user:1||user:2||user:5', $MC->get('tag_t_country:UK'));
     $this->assertSame('city:London||country:UK', $MC->get('tag_k_user:1'));
     $this->assertSame('city:London||country:UK', $MC->get('tag_k_user:2'));
     $this->assertSame('city:London||country:UK', $MC->get('tag_k_user:5'));
     $this->assertSame('Alexander', $MC->get('user:1'));
     $this->assertSame('Irina', $MC->get('user:2'));
     $this->assertSame('Dom', $MC->get('user:5'));
     $this->assertSame(true, $MemcachedTags->setKeysWithTags(['user:4' => 'Dima'], ['city:Murmansk', 'country:Russia', 'sex:m']));
     $this->assertSame('user:4', $MC->get('tag_t_city:Murmansk'));
     $this->assertSame('user:4', $MC->get('tag_t_country:Russia'));
     $this->assertSame('user:4', $MC->get('tag_t_sex:m'));
     $this->assertSame('city:Murmansk||country:Russia||sex:m', $MC->get('tag_k_user:4'));
     $this->assertSame('Dima', $MC->get('user:4'));
     $this->assertSame(true, $MemcachedTags->setKeysWithTags(['user:3' => 'Ilya'], ['city:Petersburg', 'country:Russia', 'sex:m']));
     $this->assertSame('user:3', $MC->get('tag_t_city:Petersburg'));
     $this->assertSame('user:4||user:3', $MC->get('tag_t_country:Russia'));
     $this->assertSame('user:4||user:3', $MC->get('tag_t_sex:m'));
     $this->assertSame('city:Petersburg||country:Russia||sex:m', $MC->get('tag_k_user:3'));
     $this->assertSame('Ilya', $MC->get('user:3'));
 }
Example #3
0
/**
 * This file is part of MemcachedTags.
 * git: https://github.com/cheprasov/php-memcached-tags
 *
 * (C) Alexander Cheprasov <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require './vendor/autoload.php';
use MemcachedTags\MemcachedTags;
// Example 1. Create new Instance
$Memcached = new \Memcached();
$Memcached->addServer('127.0.0.1', '11211');
$Memcached->flush();
$MemcachedTags = new MemcachedTags($Memcached, ['prefix' => 'users']);
// Example 2. Adding some tags to key
// some test data
$Memcached->set('user:1', '{"name": "Alexander", "sex": "m", "country": "UK",     "city": "London"}');
$Memcached->set('user:2', '{"name": "Irina",     "sex": "f", "country": "UK",     "city": "London"}');
$Memcached->set('user:3', '{"name": "Ilya",      "sex": "m", "country": "Russia", "city": "Petersburg"}');
$Memcached->set('user:4', '{"name": "Dima",      "sex": "m", "country": "Russia", "city": "Murmansk"}');
$Memcached->set('user:5', '{"name": "Dom",       "sex": "m", "country": "UK",     "city": "London"}');
$MemcachedTags->addTagsToKeys(['city:London', 'country:UK'], ['user:1', 'user:2', 'user:5']);
$MemcachedTags->addTagsToKeys(['city:Murmansk', 'country:Russia'], 'user:4');
$MemcachedTags->addTagsToKeys(['city:Petersburg', 'country:Russia'], 'user:3');
$MemcachedTags->addTagsToKeys('sex:m', ['user:1', 'user:3', 'user:4', 'user:5']);
$MemcachedTags->addTagsToKeys('sex:f', 'user:2');
$MemcachedTags->addTagsToKeys('all', ['user:1', 'user:2', 'user:3', 'user:4', 'user:5']);
// or you can create key with tags
$MemcachedTags->setKeyWithTags('user:1', 'Alexander', ['country:UK', 'city:London', 'sex:m', 'all']);