{"id":15028737,"url":"https://github.com/cheprasov/php-memcached-tags","last_synced_at":"2025-04-09T20:32:19.042Z","repository":{"id":62501535,"uuid":"53231916","full_name":"cheprasov/php-memcached-tags","owner":"cheprasov","description":"MemcachedTags for PHP is a mechanism for adding tags to keys in Memcached. It is very useful, if you need to select or delete some keys by tags. And tags are really useful for group invalidation.","archived":false,"fork":false,"pushed_at":"2021-06-10T19:40:46.000Z","size":37,"stargazers_count":12,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-26T05:22:55.725Z","etag":null,"topics":["memcached","php","tags"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cheprasov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"cheprasov","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-03-06T01:32:15.000Z","updated_at":"2023-09-10T22:55:54.000Z","dependencies_parsed_at":"2022-11-02T12:01:46.637Z","dependency_job_id":null,"html_url":"https://github.com/cheprasov/php-memcached-tags","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-tags","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-tags/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-tags/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-tags/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheprasov","download_url":"https://codeload.github.com/cheprasov/php-memcached-tags/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248107303,"owners_count":21048897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["memcached","php","tags"],"created_at":"2024-09-24T20:08:58.583Z","updated_at":"2025-04-09T20:32:19.013Z","avatar_url":"https://github.com/cheprasov.png","language":"PHP","funding_links":["https://github.com/sponsors/cheprasov"],"categories":[],"sub_categories":[],"readme":"[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n[![Latest Stable Version](https://poser.pugx.org/cheprasov/php-memcached-tags/v/stable)](https://packagist.org/packages/cheprasov/php-memcached-tags)\n[![Total Downloads](https://poser.pugx.org/cheprasov/php-memcached-tags/downloads)](https://packagist.org/packages/cheprasov/php-memcached-tags)\n\n# MemcachedTags v1.0.5 for PHP \u003e= 5.5\n\n## About\nMemcachedTags for PHP is a mechanism for adding tags to keys in Memcached. It is very useful, if you need to select or delete some keys by tags. And tags are really useful for group invalidation.\n\n## Main features\n- Data modification functions such as delete/add/set use [Locks](https://github.com/cheprasov/php-memcached-lock) to prevent losing data.\n- MemcachedTags does not affect original keys. It creates own keys for service tags.\n\n## How it works\nI will try to explain a mechanism, how memcached stores tags.\n\nImagine, you have some 3 keys in memcached (user1, user2, user3):\n\n```\nMEMCACHED (key : value)\nuser1 : {\"name\":\"Alexander\", \"sex\":\"m\", \"city\":\"London\"}\nuser2 : {\"name\":\"Irina\", \"sex\":\"f\", \"city\":\"London\"}\nuser3 : {\"name\":\"Dima\", \"sex\":\"m\", \"city\":\"Murmansk\"}\n```\n\nNow, lets add tag 'London' to users:\n\n```php\n// php code\nMemcachedTags-\u003eaddTagsToKeys('London', ['user1', 'user2']);\n```\nAnd, as result, the memcached will contain:\n\n```\nMEMCACHED (key : value)\nuser1 : {\"name\":\"Alexander\", \"sex\":\"m\", \"city\":\"London\"}\nuser2 : {\"name\":\"Irina\", \"sex\":\"f\", \"city\":\"London\"}\nuser3 : {\"name\":\"Dima\", \"sex\":\"m\", \"city\":\"Murmansk\"}\n\ntag_k_user1 : London\ntag_k_user2 : London\n\ntag_t_London : user1||user2\n```\n\nAnd, lets add tags 'male' and 'female' to users:\n\n```php\n// php code\nMemcachedTags-\u003eaddTagsToKeys('male', ['user1', 'user3']);\nMemcachedTags-\u003eaddTagsToKeys('female', 'user2');\n```\n\nAnd, as result, the memcached will contain:\n\n```\nMEMCACHED (key : value)\nuser1 : {\"name\":\"Alexander\", \"sex\":\"m\", \"city\":\"London\"}\nuser2 : {\"name\":\"Irina\", \"sex\":\"f\", \"city\":\"London\"}\nuser3 : {\"name\":\"Dima\", \"sex\":\"m\", \"city\":\"Murmansk\"}\n\ntag_k_user1 : London||male\ntag_k_user2 : London||female\ntag_k_user3 : male\n\ntag_t_London : user1||user2\ntag_t_male   : user1||user3\ntag_t_female : user2\n```\n\n## Usage\n\n```php\n\u003c?php\nrequire ('./vendor/autoload.php');\nuse MemcachedTags\\MemcachedTags;\n\n// Example 1. Create new Instance\n\n$Memcached = new \\Memcached();\n$Memcached-\u003eaddServer('127.0.0.1', '11211');\n\n$MemcachedTags = new MemcachedTags($Memcached);\n\n// Example 2. Adding some tags to key\n\n// some test data\n$Memcached-\u003eset('user:1', '{\"name\": \"Alexander\", \"sex\": \"m\", \"country\": \"UK\",     \"city\": \"London\"}');\n$Memcached-\u003eset('user:2', '{\"name\": \"Irina\",     \"sex\": \"f\", \"country\": \"UK\",     \"city\": \"London\"}');\n$Memcached-\u003eset('user:3', '{\"name\": \"Ilya\",      \"sex\": \"m\", \"country\": \"Russia\", \"city\": \"Petersburg\"}');\n$Memcached-\u003eset('user:4', '{\"name\": \"Dima\",      \"sex\": \"m\", \"country\": \"Russia\", \"city\": \"Murmansk\"}');\n$Memcached-\u003eset('user:5', '{\"name\": \"Dom\",       \"sex\": \"m\", \"country\": \"UK\",     \"city\": \"London\"}');\n\n// Add tags to keys\n\n$MemcachedTags-\u003eaddTagsToKeys(['city:London', 'country:UK'], ['user:1', 'user:2', 'user:5']);\n$MemcachedTags-\u003eaddTagsToKeys(['city:Murmansk', 'country:Russia'], 'user:4');\n$MemcachedTags-\u003eaddTagsToKeys(['city:Petersburg', 'country:Russia'], 'user:3');\n\n$MemcachedTags-\u003eaddTagsToKeys('sex:m', ['user:1', 'user:3', 'user:4', 'user:5']);\n$MemcachedTags-\u003eaddTagsToKeys('sex:f', 'user:2');\n\n$MemcachedTags-\u003eaddTagsToKeys('all', ['user:1','user:2', 'user:3', 'user:4', 'user:5']);\n\n// or you can create key with tags\n\n$MemcachedTags-\u003esetKeyWithTags('user:1', 'Alexander', ['country:UK', 'city:London', 'sex:m', 'all']);\n\n// Example 3. Get keys by tags\n\n// Get users with tag \u003call\u003e\nvar_dump(\n    $MemcachedTags-\u003egetKeysByTag('all')\n);\n//    array(2) {\n//      [0]=\u003e string(6) \"user:1\"\n//      [1]=\u003e string(6) \"user:2\"\n//      [2]=\u003e string(6) \"user:3\"\n//      [3]=\u003e string(6) \"user:4\"\n//      [4]=\u003e string(6) \"user:5\"\n//    }\n\n// Get users with tag \u003ccountry:UK\u003e\nvar_dump(\n    $MemcachedTags-\u003egetKeysByTag('country:UK')\n);\n//    array(2) {\n//      [0]=\u003e string(6) \"user:1\"\n//      [1]=\u003e string(6) \"user:2\"\n//      [2]=\u003e string(6) \"user:5\"\n//    }\n\n// Get users with tag \u003ccity:Petersburg\u003e OR \u003ccity:Murmansk\u003e\nvar_dump(\n    $MemcachedTags-\u003egetKeysByTags(['city:Petersburg', 'city:Murmansk'], MemcachedTags::COMPILATION_OR)\n);\n//    array(2) {\n//      [0]=\u003e string(6) \"user:3\"\n//      [1]=\u003e string(6) \"user:4\"\n//    }\n\n// Get users with tags \u003ccountry:UK\u003e AND \u003csex:m\u003e\nvar_dump(\n    $MemcachedTags-\u003egetKeysByTags(['country:UK', 'sex:m'], MemcachedTags::COMPILATION_AND)\n);\n//    array(3) {\n//      [0]=\u003e string(6) \"user:1\"\n//      [1]=\u003e string(6) \"user:5\"\n//    }\n\n// Get users with tag \u003ccountry:UK\u003e AND WITHOUT \u003csex:m\u003e\nvar_dump(\n    $MemcachedTags-\u003egetKeysByTags(['country:UK', 'sex:m'], MemcachedTags::COMPILATION_XOR)\n);\n//    array(3) {\n//      [0]=\u003e string(6) \"user:2\"\n//    }\n\n// Example 4. Delete keys by tags\n\n// Delete keys with tag \u003ccity:Murmansk\u003e\nvar_dump(\n    $MemcachedTags-\u003edeleteKeysByTag('city:Murmansk')\n);\n// int(1) - Count of deleted keys\n\n// Delete keys with tag \u003ccity:London\u003e WITHOUT \u003csex:f\u003e\nvar_dump(\n    $MemcachedTags-\u003edeleteKeysByTags(['city:London', 'sex:f'], MemcachedTags::COMPILATION_XOR)\n);\n// int(2) - Count of deleted keys\n\n```\n\n## Methods\n\n#### MemcachedTags :: __construct ( `\\Memcached` **$Memcached** , `array` **$config** = null )\n---\nCreate a new instance of MemcachedTags.\n\n##### Method Parameters\n\n1. \\Memcached **$Memcached** - Instance of [Memcached](http://php.net/manual/en/book.memcached.php)\n2. array **$config**, default = null\n    * `prefix` - is a prefix for service keys in Memcached storage, like namespace.\n    * `separator` - special char(s) that , by default `||`. It is service parameter for the gluing of tags to the Memcached. This value should not use in the name tags or keys.\n\n##### Example\n\n```php\n$Lock = new MemcachedTags($Memcached);\n// or\n$Lock = new MemcachedTags($Memcached, [\n    'prefix' =\u003e 'myTag',\n    'separator' =\u003e '\u003c;\u003e',\n]);\n\n```\n\n\n#### `bool` MemcachedTags :: addTagsToKeys ( `string|string[]` **$tags** , `string|string[]` **$keys** )\n---\nAdds each key specified tags. Returns `true` on success or `false` on failure.\n\n##### Method Parameters\n\n1. string|string[] **$tags** - Tag or list of tags that will be added to each key.\n2. string|string[] **$keys** - Existing keys in Memcached for tags\n\n##### Example\n\n```php\n$MemcachedTags-\u003eaddTagsToKeys(['city:London', 'country:UK'], ['user:1', 'user:2', 'user:5']);\n$MemcachedTags-\u003eaddTagsToKeys(['city:Murmansk', 'country:Russia'], 'user:4');\n$MemcachedTags-\u003eaddTagsToKeys(['big', 'red'], 'apple');\n$MemcachedTags-\u003eaddTagsToKeys(['green', 'tasty'], 'orange');\n```\n\n\n#### `int` MemcachedTags :: deleteKey ( `string` **$key** )\n---\nDelete key and update dependent tags. Returns count of deleted keys (0 or 1).\n\n##### Method Parameters\n1. string **$key** - Name of key.\n\n##### Example\n\n```php\n$MemcachedTags-\u003edeleteKey('user:1');\n```\n\n\n#### `int` MemcachedTags :: deleteKeys ( `string[]` **$keys** )\n---\nDelete keys and update dependent tags. Returns count of deleted keys.\n\n##### Method Parameters\n1. string[] **$keys** - List of keys.\n\n##### Example\n\n```php\n$MemcachedTags-\u003edeleteKey(['user:1', 'user:2']);\n```\n\n#### `int` MemcachedTags :: deleteTag ( `string` **$tag** )\n---\nDelete tag. Keys will be not affected. Returns count of deleted tags. (0 or 1)\n\n##### Method Parameters\n1. string **$tag** - Name of tag.\n\n##### Example\n\n```php\n$MemcachedTags-\u003edeleteTag('big');\n```\n\n\n#### `int` MemcachedTags :: deleteTags ( `string[]` **$tags** )\n---\nDelete several tags. Keys will be not affected. Returns count of deleted tags.\n\n##### Method Parameters\n1. string[] **$tags** - List of tags\n\n##### Example\n\n```php\n$MemcachedTags-\u003edeleteTags(['big', 'tasty', 'old']);\n```\n\n\n#### `int` MemcachedTags :: deleteKeysByTag ( `string` **$tag** )\n---\nDelete keys by tag. Returns count of deleted keys.\n\n##### Method Parameters\n1. string **tag** - Name of tag.\n\n##### Example\n\n```php\n$MemcachedTags-\u003edeleteKeysByTag('city:London');\n// or\n$MemcachedTags-\u003edeleteKeysByTag('sql');\n```\n\n\n#### `int` MemcachedTags :: deleteKeysByTags ( `string[]` **$tags** [, `int` **$compilation** = MemcachedTags::COMPILATION_ALL] )\n---\nDelete keys by several tags. Returns count of deleted keys.\n\n##### Method Parameters\n1. string[] **tags** - List of tags\n2. int **$compilation**, default = MemcachedTags::COMPILATION_ALL - The method of combining tags.\n    * `MemcachedTags::COMPILATION_ALL` - The same as MemcachedTags::COMPILATION_OR.\n    * `MemcachedTags::COMPILATION_AND` - Delete keys that have every tags.\n    * `MemcachedTags::COMPILATION_OR` - Delete keys that have any tags.\n    * `MemcachedTags::COMPILATION_XOR` - Delete keys containing tag1 that are not have any of the other tags.\n\n##### Example\n\n```php\n// Delete all apples and oranges\n$MemcachedTags-\u003edeleteKeysByTags(['apple', 'oranges']);\n\n// Delete only big oranges\n$MemcachedTags-\u003edeleteKeysByTags(['big', 'oranges'], MemcachedTags::COMPILATION_AND);\n\n// Delete all orange expect big oranges\n$MemcachedTags-\u003edeleteKeysByTags(['oranges', 'big'], MemcachedTags::COMPILATION_XOR);\n\n```\n\n\n#### `string[]` MemcachedTags :: getKeysByTag ( `string` **$tag** )\n---\nReturns a list of keys with tag.\n\n##### Method Parameters\n1. string **tag** - Name of tag.\n\n##### Example\n\n```php\n$MemcachedTags-\u003egetKeysByTag('big');\n// or\n$MemcachedTags-\u003egetKeysByTag('red');\n```\n\n\n#### `string[]|array` MemcachedTags :: getKeysByTags ( `string[]` **$tags** [, `int` **$compilation** = MemcachedTags::COMPILATION_ALL] )\n---\nReturns a list of keys by several tags.\n\n##### Method Parameters\n1. string[] **tags** - List of tags.\n2. int **$compilation**, default = MemcachedTags::COMPILATION_ALL - The method of combining tags.\n    * MemcachedTags::COMPILATION_ALL - Returns array with keys for every tag. `array(tag1 =\u003e [key1, key2], ...)`\n    * MemcachedTags::COMPILATION_AND - Returns a list of keys that have every tags.\n    * MemcachedTags::COMPILATION_OR - Returns a list of keys that have any tags.\n    * MemcachedTags::COMPILATION_XOR - Returns a list of keys containing tag1 that are not have any of the other tags.\n\n##### Example\n\n```php\n// Get all apples and oranges\n$MemcachedTags-\u003egetKeysByTags(['apple', 'oranges']);\n\n// Get all apples or oranges\n$MemcachedTags-\u003egetKeysByTags(['apple', 'oranges'], MemcachedTags::COMPILATION_OR);\n\n// Get only big oranges\n$MemcachedTags-\u003egetKeysByTags(['big', 'oranges'], MemcachedTags::COMPILATION_AND);\n\n// Get all orange expect big oranges\n$MemcachedTags-\u003egetKeysByTags(['oranges', 'big'], MemcachedTags::COMPILATION_XOR);\n```\n\n\n#### `string[]` MemcachedTags :: getTagsByKey ( `string` **$key** )\n---\nReturns list of tags or empty list.\n\n##### Method Parameters\n1. string **$key** - Key in Memcached.\n\n##### Example\n\n```php\n$MemcachedTags-\u003egetTagsByKey('user:1');\n```\n\n\n#### `bool` MemcachedTags :: setKeyWithTags ( `string` **$key** , `string` **$value** , `string|string[]` **$tags** )\n---\nSet value and tags to key. Returns result as `bool`.\n\n##### Method Parameters\n1. string **$key** - The key under which to store the value.\n2. string **$value** - The value to store.\n3. string|string[] **$tags** - Tag or list of tags for the key.\n\n##### Example\n\n```php\n$MemcachedTags-\u003esetKeyWithTags('user:1', 'Alexander', ['sex:m', 'city:London']);\n```\n\n\n#### `bool` MemcachedTags :: setKeysWithTags ( `array` **$items** , `string|string[]` **$tags** )\n---\nSet values and tags to several keys. Returns result as `bool`.\n\n##### Method Parameters\n1. string **$items** - An array of key/value pairs to store on the server.\n3. string|string[] **$tags** - Tag or list of tags for the items.\n\n##### Example\n\n```php\n$MemcachedTags-\u003esetKeysWithTags(['user:1' =\u003e 'Alexander', 'user:2' =\u003e 'Irina'], 'city:London');\n```\n\n\n## Installation\n\n### Composer\n\nDownload composer:\n\n    wget -nc http://getcomposer.org/composer.phar\n\nand add dependency to your project:\n\n    php composer.phar require cheprasov/php-memcached-tags\n\n## Running tests\n\nTo run tests type in console:\n\n    ./vendor/bin/phpunit ./test/\n\n## Something doesn't work\n\nFeel free to fork project, fix bugs and finally request for pull\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-memcached-tags","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheprasov%2Fphp-memcached-tags","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-memcached-tags/lists"}