{"id":22352273,"url":"https://github.com/italystrap/cache","last_synced_at":"2025-10-12T20:43:12.064Z","repository":{"id":56994064,"uuid":"244348915","full_name":"ItalyStrap/cache","owner":"ItalyStrap","description":"PSR-6 and PSR-16 cache implementations for WordPress Transient API and Object Cache API the OOP way","archived":false,"fork":false,"pushed_at":"2023-05-20T16:51:42.000Z","size":54,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-22T17:09:49.637Z","etag":null,"topics":["cache","php","psr-16","psr-6","psr-6-psr-16","wordpress","wordpress-php-library"],"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/ItalyStrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-02T11:00:37.000Z","updated_at":"2024-09-29T00:01:55.000Z","dependencies_parsed_at":"2023-02-15T18:10:50.313Z","dependency_job_id":null,"html_url":"https://github.com/ItalyStrap/cache","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ItalyStrap%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ItalyStrap%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ItalyStrap%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ItalyStrap%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ItalyStrap","download_url":"https://codeload.github.com/ItalyStrap/cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228106304,"owners_count":17870438,"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":["cache","php","psr-16","psr-6","psr-6-psr-16","wordpress","wordpress-php-library"],"created_at":"2024-12-04T12:17:58.597Z","updated_at":"2025-10-12T20:43:11.963Z","avatar_url":"https://github.com/ItalyStrap.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ItalyStrap Cache API\n\n[![Build status](https://github.com/ItalyStrap/cache/actions/workflows/test.yml/badge.svg)](https://github.com/ItalyStrap/cache/actions/workflows/test.yml?query=workflow%3Atest)\n[![Latest Stable Version](https://img.shields.io/packagist/v/italystrap/cache.svg)](https://packagist.org/packages/italystrap/cache)\n[![Total Downloads](https://img.shields.io/packagist/dt/italystrap/cache.svg)](https://packagist.org/packages/italystrap/cache)\n[![Latest Unstable Version](https://img.shields.io/packagist/vpre/italystrap/cache.svg)](https://packagist.org/packages/italystrap/cache)\n[![License](https://img.shields.io/packagist/l/italystrap/cache.svg)](https://packagist.org/packages/italystrap/cache)\n![PHP from Packagist](https://img.shields.io/packagist/php-v/italystrap/cache)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FItalyStrap%2Fcache%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/ItalyStrap/cache/master)\n\nPSR-16 \u0026 PSR-6 Cache implementations for WordPress transient and cache the OOP way\n\n**Version 2.0 is a BC breaks please read the following documentation**\n\n## Table Of Contents\n\n* [Installation](#installation)\n* [Basic Usage](#basic-usage)\n* [Advanced Usage](#advanced-usage)\n* [Contributing](#contributing)\n* [License](#license)\n\n## Installation\n\nThe best way to use this package is through Composer:\n\n```CMD\ncomposer require italystrap/cache\n```\nThis package adheres to the [SemVer](http://semver.org/) specification and will be fully backward compatible between minor versions.\n\nFor more information about the WordPress API:\n\n* [WordPress Transients API docs](https://developer.wordpress.org/apis/transients/)\n* [WordPress Cache API docs](https://developer.wordpress.org/reference/classes/wp_object_cache/)\n\n## Why this package?\n\nInitially I created this package to use the [PSR-16](https://www.php-fig.org/psr/psr-16/) cache interface in WordPress, but starting from this issue [#4](https://github.com/ItalyStrap/cache/issues/4) I decided to extract the drivers and also add the [PSR-6](https://www.php-fig.org/psr/psr-6/) cache interface too.\n\nFrom version 2.0 I also added the PSR-6 implementation, so you can also use the `Pool` class to cache your data if you need it.\n\nSo this package now support both the [PSR-16](https://www.php-fig.org/psr/psr-16/) and [PSR-6](https://www.php-fig.org/psr/psr-6/) cache interfaces.\n\nThe driver required by this package uses the Transients and Object Cache APIs from WordPress to store the data, but if you need to use other APIs, you can create your own driver because just implements the interface `\\ItalyStrap\\Storage\\CacheInterface` from [Storage API](https://github.com/ItalyStrap/storage).\n\n### Moving from Version 1 to Version 2\n\nThe first important thing is from the version 2 you need to pass the driver object and the expiration object to the constructor of the class you want to use.\n\n`SimpleCache` and `Pool` are the two classes that need the driver and the expiration to be passed to the constructor.\n\nThe driver is an object wrapper for the WordPress Transient API and the WordPress Object Cache API.\n\nThe expiration object is used to set the expiration time of the cache.\n\nBelow in the documentation you will find the name of the drivers that you can use and the expiration object.\n\nThe second important thing is that the driver must implement the `CacheInterface` from [Cache API](https://github.com/ItalyStrap/storage), this way if you need to create your own driver you can do it simply by implementing the interface.\n\n### Why the needs of an Expiration object?\n\nThe expiration object is used to set the expiration time of the cache.\n\nBecause I want to be as close as possible to the PSR-16 and PSR-6 specifications, I have created an object that is responsible for setting the expiration time, this way I can reuse the same logic across all PSR-16 and PSR-6 implementations and I didn't need to create more methods that are not in the specifications.\n\n## Basic Usage\n\n### Timer constants\n\nInside WordPress there are some constants that can be used to express time in seconds.\nHere is a list of them:\n\n```php\nconst MINUTE_IN_SECONDS  = 60; // (seconds)\nconst HOUR_IN_SECONDS    = 60 * MINUTE_IN_SECONDS;\nconst DAY_IN_SECONDS     = 24 * HOUR_IN_SECONDS;\nconst WEEK_IN_SECONDS    = 7 * DAY_IN_SECONDS;\nconst MONTH_IN_SECONDS   = 30 * DAY_IN_SECONDS;\nconst YEAR_IN_SECONDS    = 365 * DAY_IN_SECONDS;\n```\n\nOr you can use the built-in constant from `ItalyStrap\\Cache\\ExpirationInterface`:\n\n```php\nuse ItalyStrap\\Cache\\ExpirationInterface;\n\n$expirationTime = ExpirationInterface::MINUTE_IN_SECONDS;\n$expirationTime = ExpirationInterface::HOUR_IN_SECONDS;\n$expirationTime = ExpirationInterface::DAY_IN_SECONDS;\n$expirationTime = ExpirationInterface::WEEK_IN_SECONDS;\n$expirationTime = ExpirationInterface::MONTH_IN_SECONDS;\n$expirationTime = ExpirationInterface::YEAR_IN_SECONDS;\n```\n\nOr you can use the built-in PHP function `strtotime()` to express time in seconds.\n\n### Common usage with builtin WordPress Transients API\n\n```php\nif (false === ($special_data_to_save = \\get_transient('special_data_to_save'))) {\n    // It wasn't there, so regenerate the data and save the transient\n    $special_data_to_save = ['some-key' =\u003e 'come value'];\n    \\set_transient('special_data_to_save', $special_data_to_save, 12 * HOUR_IN_SECONDS);\n}\n```\n\nThe data you can save can be anything that is supported by the [Serialization API](https://developer.wordpress.org/reference/functions/maybe_serialize/).\nIn short, you can save any scalar value, array, object.\n\n### Common usage with the Pool cache\n\n```php\nuse ItalyStrap\\Cache\\Pool;\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n$expiration = new Expiration();\n\n$pool = new Pool($driver, $expiration);\n\n// Pass the pool object to other classes that need to save data\n// then retrieve the data from the pool\n$item = $pool-\u003egetItem('special_data_to_save');\nif (!$item-\u003eisHit()) {\n    // It wasn't there, so regenerate the data and save the transient\n    $item-\u003eset(['some-key' =\u003e 'some value']);\n    $item-\u003eexpiresAfter(12 * HOUR_IN_SECONDS);\n    $pool-\u003esave($item);\n}\n$special_data_to_save = $item-\u003eget();\n\n['some-key' =\u003e 'some value'] === $special_data_to_save; // True\n```\n\n### Common usage with the SimpleCache\n\nOnly if you need to parse binary data (as for example image files) you can use the `BinaryCacheDecorator` class, but remember that if the file is more than 1MB it is better to not use this to save in the database.\n\nAnd because you are a good developer, you will not save binary data in the database, and you will use the `Transient` class instead of the `BinaryCacheDecorator` class.\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n// Pay attention to `SimpleCacheInterface::get()` method because if there is no value will return `null` and not `false` as the WordPress Transient API does.\nif (null === ($special_data_to_save = $cache-\u003eget('special_data_to_save'))) {\n    // It wasn't there, so regenerate the data and save the transient\n    $special_data_to_save = ['some-key' =\u003e 'some value'];\n    $cache-\u003eset('special_data_to_save', $special_data_to_save, 12 * HOUR_IN_SECONDS);\n}\n\n['some-key' =\u003e 'some value'] === $special_data_to_save; // True\n```\n\n### Deleting cache with Pool\n\n```php\nuse ItalyStrap\\Cache\\Pool;\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n$expiration = new Expiration();\n\n$pool = new Pool($driver, $expiration);\n\n$item = $pool-\u003egetItem('special_data_to_save');\n$item-\u003eset(['some-key' =\u003e 'some value']);\n$item-\u003eexpiresAfter(12 * HOUR_IN_SECONDS);\n$pool-\u003esave($item);\n\n$pool-\u003edeleteItem('special_data_to_save'); // Return bool\n\n// `::getItem()` will return a new item instance, always\n$pool-\u003egetItem('special_data_to_save')-\u003eisHit(); // Return false\n```\n\n\n### Deleting cache with SimpleCache\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n$cache-\u003eset('special_data_to_save', ['some-key' =\u003e 'some value'], 12 * HOUR_IN_SECONDS);\n\n$cache-\u003edelete('special_data_to_save'); // Return bool\n\n$cache-\u003eget('special_data_to_save'); // Return null\n```\n\n### Check cache exists with Pool\n\n```php\nuse ItalyStrap\\Cache\\Pool;\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n$expiration = new Expiration();\n    \n$pool = new Pool($driver, $expiration);\n\n$item = $pool-\u003egetItem('special_data_to_save');\n$item-\u003eset(['some-key' =\u003e 'some value']);\n$item-\u003eexpiresAfter(12 * HOUR_IN_SECONDS);\n$pool-\u003esave($item);\n\n$pool-\u003ehasItem('special_data_to_save'); // Return true\n\n// But also this will return false if the item is expired or not exists\n$pool-\u003ehasItem('expired_or_not_existent_value'); // Return false\n```\n\n\n### Check cache exists with SimpleCache\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n$cache-\u003eset('special_data_to_save', ['some-key' =\u003e 'some value'], 12 * HOUR_IN_SECONDS);\n$cache-\u003ehas('special_data_to_save'); // Return true\n\n// But also this will return false if the item is expired or not exists\n$cache-\u003ehas('expired_or_not_existent_value'); // Return false\n```\n\n### Saving multiple cache with SimpleCache\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n$values = [\n    'key'       =\u003e 'value',\n    'key2'      =\u003e 'value2',\n];\n\n$cache-\u003esetMultiple($values, 12 * HOUR_IN_SECONDS); // Return bool\n```\n\n### Fetching multiple cache with SimpleCache\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n$values = [\n    'key'       =\u003e 'value',\n    'key2'      =\u003e 'value2',\n    'key3'      =\u003e false, // This will be replaced with 'some default value' because the method pass a default value\n];\n\n$fetched_values = $cache-\u003egetMultiple(\\array_keys($values), 'some default value'); // Return values\n```\n\n### Deleting multiple cache with SimpleCache\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n$values = [\n    'key'       =\u003e 'value',\n    'key2'      =\u003e 'value2',\n    'key3'      =\u003e false,\n];\n\n$cache-\u003edeleteMultiple(\\array_keys($values)); // Return bool\n```\n\n### Clearing cache with SimpleCache\n\nThis method do not clear the entire WordPress cache, only the cache used by client with\n::set() and ::setMultiple() methods.\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n$cache-\u003eset('special_data_to_save',['some-key' =\u003e 'come value'], 12 * HOUR_IN_SECONDS);\n\n$values = [\n    'key'       =\u003e 'value',\n    'key2'      =\u003e 'value2',\n];\n\n$cache-\u003esetMultiple($values, 12 * HOUR_IN_SECONDS);\n\n$cache-\u003eclear(); // Return bool\n\n$cache-\u003eget('special_data_to_save'); // Return null\n$cache-\u003eget('key'); // Return null\n$cache-\u003eget('key2'); // Return null\n```\n\nCache::clear() will flush 'special_data_to_save', 'key' and 'key2'.\n\n## Other examples\n\n```php\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Cache\\SimpleCache;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n\n$expiration = new Expiration();\n$cache = new SimpleCache($driver, $expiration);\n\n// Get any existing copy of our transient data\nif (false === ($special_data_to_save = $cache-\u003eget('special_data_to_save'))) {\n    // It wasn't there, so regenerate the data and save the transient\n     $cache-\u003eset('special_data_to_save', ['some-key' =\u003e 'some value'], 12 * HOUR_IN_SECONDS);\n}\n// Use the data like you would have normally...\n\n//Or\n\n// Get any existing copy of our transient data\nif (!$cache-\u003ehas('special_data_to_save')) {\n    // It wasn't there, so regenerate the data and save the transient\n     $cache-\u003eset('special_data_to_save', ['some-key' =\u003e 'some value'], 12 * HOUR_IN_SECONDS);\n}\n// Use the data like you would have normally...\n```\n\nYou could also use a Bridge to use the \\Psr\\SimpleCache\\CacheInterface and inject the \\Psr\\Cache\\CacheItemPoolInterface\n\n```php\nuse ItalyStrap\\Cache\\Pool;\nuse ItalyStrap\\Cache\\Expiration;\nuse ItalyStrap\\Storage\\BinaryCacheDecorator;\nuse ItalyStrap\\Storage\\Transient;\n\n$driver = new BinaryCacheDecorator(new Transient()); // Or use new Cache()\n$expiration = new Expiration();\n    \n$pool = new Pool($driver, $expiration);\n\n$cache = new \\ItalyStrap\\Cache\\SimpleCacheBridge($pool);\n// and use the $cache as \\Psr\\SimpleCache\\CacheInterface\n```\n\n### Use the Factory to simplify the creation of the Cache\n\n```php\nuse ItalyStrap\\Cache\\Factory;\n\n$cache = (new Factory())-\u003emakePool();\n$cache = (new Factory())-\u003emakePoolTransient();\n$cache = (new Factory())-\u003emakeSimpleCache();\n$cache = (new Factory())-\u003emakeSimpleCacheTransient();\n$cache = (new Factory())-\u003emakeSimpleCacheBridge();\n$cache = (new Factory())-\u003emakeSimpleCacheBridgeTransient();\n```\n\n## Contributing\n\nAll feedback / bug reports / pull requests are welcome.\n\n## License\n\nCopyright (c) 2019 Enea Overclokk, ItalyStrap\n\nThis code is licensed under the [MIT](LICENSE).\n\n## Credits\n\n* [DoctrineSimpleCache](https://github.com/Roave/DoctrineSimpleCache)\n* [PHP Cache organisation](https://github.com/php-cache/integration-tests)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitalystrap%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fitalystrap%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitalystrap%2Fcache/lists"}