cheprasov / php-memcached-tags
MemcachedTags for PHP 是在 Memcached 中为键添加标签的机制。如果您需要根据标签选择或删除某些键,这非常有用。标签对于分组无效化也非常有用。
1.0.5
2018-01-17 21:12 UTC
Requires
- php: >=5.5
- cheprasov/php-memcached-lock: ~1.0.4
Requires (Dev)
- cheprasov/php-parallel: 1.0.*
- phpunit/phpunit: 4.8.*
This package is not auto-updated.
Last update: 2024-09-18 19:23:07 UTC
README
MemcachedTags v1.0.5 for PHP >= 5.5
关于
MemcachedTags for PHP 是在 Memcached 中为键添加标签的机制。如果您需要根据标签选择或删除某些键,这非常有用。标签对于分组无效化也非常有用。
主要功能
- 数据修改功能,如删除、添加和设置,使用 锁 防止数据丢失。
- MemcachedTags 不会影响原始键。它为服务标签创建自己的键。
工作原理
我将尝试解释一个机制,即 memcached 如何存储标签。
想象一下,您在 memcached 中有 3 个键(user1、user2、user3)
MEMCACHED (key : value)
user1 : {"name":"Alexander", "sex":"m", "city":"London"}
user2 : {"name":"Irina", "sex":"f", "city":"London"}
user3 : {"name":"Dima", "sex":"m", "city":"Murmansk"}
现在,让我们将标签 'London' 添加到用户
// php code MemcachedTags->addTagsToKeys('London', ['user1', 'user2']);
结果,memcached 将包含
MEMCACHED (key : value)
user1 : {"name":"Alexander", "sex":"m", "city":"London"}
user2 : {"name":"Irina", "sex":"f", "city":"London"}
user3 : {"name":"Dima", "sex":"m", "city":"Murmansk"}
tag_k_user1 : London
tag_k_user2 : London
tag_t_London : user1||user2
然后,让我们将标签 'male' 和 'female' 添加到用户
// php code MemcachedTags->addTagsToKeys('male', ['user1', 'user3']); MemcachedTags->addTagsToKeys('female', 'user2');
结果,memcached 将包含
MEMCACHED (key : value)
user1 : {"name":"Alexander", "sex":"m", "city":"London"}
user2 : {"name":"Irina", "sex":"f", "city":"London"}
user3 : {"name":"Dima", "sex":"m", "city":"Murmansk"}
tag_k_user1 : London||male
tag_k_user2 : London||female
tag_k_user3 : male
tag_t_London : user1||user2
tag_t_male : user1||user3
tag_t_female : user2
用法
<?php require ('./vendor/autoload.php'); use MemcachedTags\MemcachedTags; // Example 1. Create new Instance $Memcached = new \Memcached(); $Memcached->addServer('127.0.0.1', '11211'); $MemcachedTags = new MemcachedTags($Memcached); // 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"}'); // Add tags to keys $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" // [4]=> string(6) "user:5" // } // Get users with tag <country:UK> var_dump( $MemcachedTags->getKeysByTag('country:UK') ); // array(2) { // [0]=> string(6) "user:1" // [1]=> string(6) "user:2" // [2]=> string(6) "user:5" // } // Get users with tag <city:Petersburg> OR <city:Murmansk> var_dump( $MemcachedTags->getKeysByTags(['city:Petersburg', 'city:Murmansk'], MemcachedTags::COMPILATION_OR) ); // array(2) { // [0]=> string(6) "user:3" // [1]=> string(6) "user:4" // } // Get users with tags <country:UK> AND <sex:m> var_dump( $MemcachedTags->getKeysByTags(['country:UK', 'sex:m'], MemcachedTags::COMPILATION_AND) ); // array(3) { // [0]=> string(6) "user:1" // [1]=> string(6) "user:5" // } // Get users with tag <country:UK> AND WITHOUT <sex:m> var_dump( $MemcachedTags->getKeysByTags(['country:UK', 'sex:m'], MemcachedTags::COMPILATION_XOR) ); // array(3) { // [0]=> string(6) "user:2" // } // Example 4. Delete keys by tags // Delete keys with tag <city:Murmansk> var_dump( $MemcachedTags->deleteKeysByTag('city:Murmansk') ); // int(1) - Count of deleted keys // Delete keys with tag <city:London> WITHOUT <sex:f> var_dump( $MemcachedTags->deleteKeysByTags(['city:London', 'sex:f'], MemcachedTags::COMPILATION_XOR) ); // int(2) - Count of deleted keys
方法
MemcachedTags :: __construct ( \Memcached
$Memcached , array
$config = null )
创建 MemcachedTags 的新实例。
方法参数
- \Memcached $Memcached - Memcached 的实例
- array $config, 默认 = null
prefix
- 是 Memcached 存储中服务键的前缀,类似于命名空间。separator
- 特殊字符,默认为||
。这是将标签粘接到 Memcached 的服务参数。此值不应用于标签或键的名称。
示例
$Lock = new MemcachedTags($Memcached); // or $Lock = new MemcachedTags($Memcached, [ 'prefix' => 'myTag', 'separator' => '<;>', ]);
bool
MemcachedTags :: addTagsToKeys ( string|string[]
$tags , string|string[]
$keys )
为指定的每个键添加标签。成功时返回 true
,失败时返回 false
。
方法参数
- string|string[] $tags - 将添加到每个键的标签或标签列表。
- string|string[] $keys - 存储在 Memcached 中的现有键。
示例
$MemcachedTags->addTagsToKeys(['city:London', 'country:UK'], ['user:1', 'user:2', 'user:5']); $MemcachedTags->addTagsToKeys(['city:Murmansk', 'country:Russia'], 'user:4'); $MemcachedTags->addTagsToKeys(['big', 'red'], 'apple'); $MemcachedTags->addTagsToKeys(['green', 'tasty'], 'orange');
int
MemcachedTags :: deleteKey ( string
$key )
删除键并更新相关标签。返回删除键的数量(0 或 1)。
方法参数
- string $key - 键的名称。
示例
$MemcachedTags->deleteKey('user:1');
int
MemcachedTags :: deleteKeys ( string[]
$keys )
删除键并更新相关标签。返回删除键的数量。
方法参数
- string[] $keys - 键的列表。
示例
$MemcachedTags->deleteKey(['user:1', 'user:2']);
int
MemcachedTags :: deleteTag ( string
$tag )
删除标签。键将不受影响。返回删除标签的数量。(0 或 1)
方法参数
- string $tag - 标签的名称。
示例
$MemcachedTags->deleteTag('big');
int
MemcachedTags :: deleteTags ( string[]
$tags )
删除多个标签。键将不受影响。返回删除标签的数量。
方法参数
- string[] $tags - 标签的列表
示例
$MemcachedTags->deleteTags(['big', 'tasty', 'old']);
int
MemcachedTags :: deleteKeysByTag ( string
$tag )
通过标签删除键。返回删除键的数量。
方法参数
- string tag - 标签的名称。
示例
$MemcachedTags->deleteKeysByTag('city:London'); // or $MemcachedTags->deleteKeysByTag('sql');
int
MemcachedTags :: deleteKeysByTags ( string[]
$tags [, int
$compilation = MemcachedTags::COMPILATION_ALL] )
通过多个标签删除键。返回删除键的数量。
方法参数
- string[] tags - 标签的列表
- int $compilation, 默认 = MemcachedTags::COMPILATION_ALL - 标签合并的方法。
MemcachedTags::COMPILATION_ALL
- 与 MemcachedTags::COMPILATION_OR 相同。MemcachedTags::COMPILATION_AND
- 删除具有所有标签的键。MemcachedTags::COMPILATION_OR
- 删除具有任何标签的键。MemcachedTags::COMPILATION_XOR
- 删除包含tag1且不包含其他任何标签的键。
示例
// Delete all apples and oranges $MemcachedTags->deleteKeysByTags(['apple', 'oranges']); // Delete only big oranges $MemcachedTags->deleteKeysByTags(['big', 'oranges'], MemcachedTags::COMPILATION_AND); // Delete all orange expect big oranges $MemcachedTags->deleteKeysByTags(['oranges', 'big'], MemcachedTags::COMPILATION_XOR);
string[]
MemcachedTags :: getKeysByTag ( string
$tag )
返回带有标签的键列表。
方法参数
- string tag - 标签的名称。
示例
$MemcachedTags->getKeysByTag('big'); // or $MemcachedTags->getKeysByTag('red');
string[]|array
MemcachedTags :: getKeysByTags ( string[]
$tags [, int
$compilation = MemcachedTags::COMPILATION_ALL] )
返回多个标签的键列表。
方法参数
- string[] tags - 标签列表。
- int $compilation, 默认 = MemcachedTags::COMPILATION_ALL - 标签合并的方法。
- MemcachedTags::COMPILATION_ALL - 返回包含每个标签的键的数组。
array(tag1 => [key1, key2], ...)
- MemcachedTags::COMPILATION_AND - 返回具有所有标签的键列表。
- MemcachedTags::COMPILATION_OR - 返回具有任何标签的键列表。
- MemcachedTags::COMPILATION_XOR - 返回包含tag1但不包含其他任何标签的键列表。
- MemcachedTags::COMPILATION_ALL - 返回包含每个标签的键的数组。
示例
// Get all apples and oranges $MemcachedTags->getKeysByTags(['apple', 'oranges']); // Get all apples or oranges $MemcachedTags->getKeysByTags(['apple', 'oranges'], MemcachedTags::COMPILATION_OR); // Get only big oranges $MemcachedTags->getKeysByTags(['big', 'oranges'], MemcachedTags::COMPILATION_AND); // Get all orange expect big oranges $MemcachedTags->getKeysByTags(['oranges', 'big'], MemcachedTags::COMPILATION_XOR);
string[]
MemcachedTags :: getTagsByKey ( string
$key )
返回标签列表或空列表。
方法参数
- string $key - Memcached中的键。
示例
$MemcachedTags->getTagsByKey('user:1');
bool
MemcachedTags :: setKeyWithTags ( string
$key , string
$value , string|string[]
$tags )
设置键的值和标签。返回结果为bool
。
方法参数
- string $key - 存储值的键。
- string $value - 要存储的值。
- string|string[] $tags - 键的标签或标签列表。
示例
$MemcachedTags->setKeyWithTags('user:1', 'Alexander', ['sex:m', 'city:London']);
bool
MemcachedTags :: setKeysWithTags ( array
$items , string|string[]
$tags )
设置多个键的值和标签。返回结果为bool
。
方法参数
- string $items - 要存储在服务器上的键/值对数组。
- string|string[] $tags - 项目标签或标签列表。
示例
$MemcachedTags->setKeysWithTags(['user:1' => 'Alexander', 'user:2' => 'Irina'], 'city:London');
安装
Composer
下载composer
wget -nc https://getcomposer.org.cn/composer.phar
并将其添加到您的项目中
php composer.phar require cheprasov/php-memcached-tags
运行测试
在控制台中输入运行测试的命令
./vendor/bin/phpunit ./test/
如果有什么问题
请随意fork项目,修复错误,并最终请求合并