/** * @param \Memcached $MC * @param MemcachedTags $MemcachedTags */ protected function addTags(\Memcached $MC, MemcachedTags $MemcachedTags) { $this->assertSame(true, $MemcachedTags->addTagsToKeys(['city:London', 'country:UK'], ['user:1', 'user:2', 'user:5'])); $this->assertSame(true, $MemcachedTags->addTagsToKeys(['city:Murmansk', 'country:Russia'], 'user:4')); $this->assertSame(true, $MemcachedTags->addTagsToKeys(['city:Petersburg', 'country:Russia'], 'user:3')); $this->assertSame(true, $MemcachedTags->addTagsToKeys('sex:m', ['user:1', 'user:3', 'user:4', 'user:5'])); $this->assertSame(true, $MemcachedTags->addTagsToKeys('sex:f', 'user:2')); $this->assertSame(true, $MemcachedTags->addTagsToKeys('all', ['user:1', 'user:2', 'user:3', 'user:4', 'user:5'])); $this->assertSame('user:4', $MC->get('tag_t_city:Murmansk')); $this->assertSame('user:3', $MC->get('tag_t_city:Petersburg')); $this->assertSame('user:1||user:2||user:5', $MC->get('tag_t_city:London')); $this->assertSame('user:4||user:3', $MC->get('tag_t_country:Russia')); $this->assertSame('user:1||user:2||user:5', $MC->get('tag_t_country:UK')); $this->assertSame('user:1||user:3||user:4||user:5', $MC->get('tag_t_sex:m')); $this->assertSame('user:2', $MC->get('tag_t_sex:f')); $this->assertSame('user:1||user:2||user:3||user:4||user:5', $MC->get('tag_t_all')); $this->assertSame('city:London||country:UK||sex:m||all', $MC->get('tag_k_user:1')); $this->assertSame('city:London||country:UK||sex:f||all', $MC->get('tag_k_user:2')); $this->assertSame('city:Petersburg||country:Russia||sex:m||all', $MC->get('tag_k_user:3')); $this->assertSame('city:Murmansk||country:Russia||sex:m||all', $MC->get('tag_k_user:4')); $this->assertSame('city:London||country:UK||sex:m||all', $MC->get('tag_k_user:5')); $this->assertSame(true, isset($MC->get('user:1')[0])); $this->assertSame(true, isset($MC->get('user:2')[0])); $this->assertSame(true, isset($MC->get('user:3')[0])); $this->assertSame(true, isset($MC->get('user:4')[0])); $this->assertSame(true, isset($MC->get('user:5')[0])); }
*/ 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']); // Example 3. Get keys by tags // Get users with tag <all> var_dump($MemcachedTags->getKeysByTag('all')); // array(2) { // [0]=> string(6) "user:1" // [1]=> string(6) "user:2" // [2]=> string(6) "user:3" // [3]=> string(6) "user:4"