{"id":13828284,"url":"https://github.com/yiisoft/cache","last_synced_at":"2025-04-07T05:12:57.303Z","repository":{"id":32727645,"uuid":"140870991","full_name":"yiisoft/cache","owner":"yiisoft","description":"PSR-16 compatible cache library","archived":false,"fork":false,"pushed_at":"2024-07-19T13:07:56.000Z","size":489,"stargazers_count":40,"open_issues_count":1,"forks_count":22,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-10-29T14:36:11.331Z","etag":null,"topics":["cache","hacktoberfest","psr-16","yii3"],"latest_commit_sha":null,"homepage":"https://www.yiiframework.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yiisoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"yiisoft","github":["yiisoft"]}},"created_at":"2018-07-13T16:42:25.000Z","updated_at":"2024-10-21T14:44:39.000Z","dependencies_parsed_at":"2023-02-14T00:55:18.742Z","dependency_job_id":"717f23d9-9846-41a0-9908-f20b7dd1bc80","html_url":"https://github.com/yiisoft/cache","commit_stats":{"total_commits":300,"total_committers":29,"mean_commits":"10.344827586206897","dds":0.54,"last_synced_commit":"9fddac5ef489a0bf02013c65dc1c7fe4cb5c052a"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiisoft","download_url":"https://codeload.github.com/yiisoft/cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419598,"owners_count":20936009,"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","hacktoberfest","psr-16","yii3"],"created_at":"2024-08-04T09:02:39.782Z","updated_at":"2025-04-07T05:12:57.269Z","avatar_url":"https://github.com/yiisoft.png","language":"PHP","funding_links":["https://opencollective.com/yiisoft","https://github.com/sponsors/yiisoft"],"categories":["PHP"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://yiisoft.github.io/docs/images/yii_logo.svg\" height=\"100px\" alt=\"Yii\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eYii Caching Library\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\n[![Latest Stable Version](https://poser.pugx.org/yiisoft/cache/v)](https://packagist.org/packages/yiisoft/cache)\n[![Total Downloads](https://poser.pugx.org/yiisoft/cache/downloads)](https://packagist.org/packages/yiisoft/cache)\n[![Build status](https://github.com/yiisoft/cache/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/cache/actions/workflows/build.yml)\n[![Code Coverage](https://codecov.io/gh/yiisoft/cache/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/cache)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fcache%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/cache/master)\n[![static analysis](https://github.com/yiisoft/cache/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/cache/actions?query=workflow%3A%22static+analysis%22)\n[![psalm-level](https://shepherd.dev/github/yiisoft/cache/level.svg)](https://shepherd.dev/github/yiisoft/cache)\n[![type-coverage](https://shepherd.dev/github/yiisoft/cache/coverage.svg)](https://shepherd.dev/github/yiisoft/cache)\n\nThis library is a wrapper around [PSR-16](https://www.php-fig.org/psr/psr-16/) compatible caching libraries\nproviding own features. It is used in [Yii Framework](https://www.yiiframework.com/) but is usable separately.\n\n## Features\n\n- Built on top of PSR-16, it can use any PSR-16 cache as a handler.\n- Ability to set default TTL and key prefix per cache instance.\n- Provides a built-in behavior to cache stampede prevention.\n- Adds cache invalidation dependencies on top of PSR-16.\n\n## Requirements\n\n- PHP 8.1 or higher.\n- `Mbstring` PHP extension.\n  \n## Installation\n\nThe package could be installed with [Composer](https://getcomposer.org):\n\n```shell\ncomposer require yiisoft/cache\n```\n\n## Configuration\n\nThere are two ways to get cache instance. If you need PSR-16 instance, you can simply create it:\n\n```php\n$arrayCache = new \\Yiisoft\\Cache\\ArrayCache();\n```\n\nIn order to set a global key prefix:\n\n```php\n$arrayCacheWithPrefix = new \\Yiisoft\\Cache\\PrefixedCache(new \\Yiisoft\\Cache\\ArrayCache(), 'myapp_');\n```\n\nIf you need a simpler yet more powerful way to cache values based on recomputation callbacks use `getOrSet()`\nand `remove()`, additional features such as invalidation dependencies and \"Probably early expiration\"\nstampede prevention, you should wrap PSR-16 cache instance with `\\Yiisoft\\Cache\\Cache`:\n\n```php\n$cache = new \\Yiisoft\\Cache\\Cache($arrayCache);\n```\n\nSet a default TTL:\n\n```php\n$cache = new \\Yiisoft\\Cache\\Cache($arrayCache, 60 * 60); // 1 hour\n```\n\n## General usage\n\nTypical PSR-16 cache usage is the following:\n\n```php\n$cache = new \\Yiisoft\\Cache\\ArrayCache();\n$parameters = ['user_id' =\u003e 42];\n$key = 'demo';\n\n// Try retrieving $data from cache.\n$data = $cache-\u003eget($key);\nif ($data === null) {\n    // $data is not found in cache, calculate it from scratch.\n    $data = calculateData($parameters);\n    \n    // Store $data in cache for an hour so that it can be retrieved next time.\n    $cache-\u003eset($key, $data, 3600);\n}\n\n// $data is available here.\n```\n\nIn order to delete value you can use:\n\n```php\n$cache-\u003edelete($key);\n// Or all cache\n$cache-\u003eclear();\n```\n\nTo work with values in a more efficient manner, batch operations should be used:\n\n- `getMultiple()`\n- `setMultiple()`\n- `deleteMultiple()`\n\nWhen using extended cache i.e. PSR-16 cache wrapped with `\\Yiisoft\\Cache\\Cache`, you can use alternative syntax that\nis less repetitive:\n\n```php\n$cache = new \\Yiisoft\\Cache\\Cache(new \\Yiisoft\\Cache\\ArrayCache());\n$key = ['top-products', $count = 10];\n\n$data = $cache-\u003egetOrSet($key, function (\\Psr\\SimpleCache\\CacheInterface $cache) use ($count) {\n    return getTopProductsFromDatabase($count);\n}, 3600);\n```\n\nNormalization of the key occurs using the `Yiisoft\\Cache\\CacheKeyNormalizer`.\n\nIn order to delete value you can use:\n\n```php\n$cache-\u003eremove($key);\n```\n\nYou can use PSR-16 methods the following way, but remember that getting and\nsetting the cache separately violates the \"Probably early expiration\" algorithm.\n\n```php\n$value = $cache\n    -\u003epsr()\n    -\u003eget('myKey');\n```\n\n### Invalidation dependencies\n\nWhen using `\\Yiisoft\\Cache\\Cache`, additionally to TTL for `getOrSet()` method you can specify a dependency\nthat may trigger cache invalidation. Below is an example using tag dependency:\n\n```php\n/**\n * @var callable $callable\n * @var \\Yiisoft\\Cache\\CacheInterface $cache\n */\n\nuse Yiisoft\\Cache\\Dependency\\TagDependency;\n\n// Set multiple cache values marking both with a tag.\n$cache-\u003egetOrSet('item_42_price', $callable, null, new TagDependency('item_42'));\n$cache-\u003egetOrSet('item_42_total', $callable, 3600, new TagDependency('item_42'));\n\n// Trigger invalidation by tag.\nTagDependency::invalidate($cache, 'item_42');\n```\n\nOther dependencies:\n\n- `Yiisoft\\Cache\\Dependency\\CallbackDependency` - invalidates the cache when callback result changes.\n- `Yiisoft\\Cache\\Dependency\\FileDependency` - invalidates the cache based on file modification time.\n- `Yiisoft\\Cache\\Dependency\\ValueDependency` - invalidates the cache when specified value changes.\n\nYou may combine multiple dependencies using `Yiisoft\\Cache\\Dependency\\AnyDependency`\nor `Yiisoft\\Cache\\Dependency\\AllDependencies`.\n\nIn order to implement your own dependency extend from `Yiisoft\\Cache\\Dependency\\Dependency`.\n\n### Cache stampede prevention\n\n[A cache stampede](https://en.wikipedia.org/wiki/Cache_stampede) is a type of cascading failure that can occur when massively\nparallel computing systems with caching mechanisms come under very high load. This behaviour is sometimes also called dog-piling.\nThe `\\Yiisoft\\Cache\\Cache` uses a built-in \"Probably early expiration\" algorithm that prevents cache stampede.\nThis algorithm randomly fakes a cache miss for one user while others are still served the cached value.\nYou can control its behavior with the fifth optional parameter of `getOrSet()`, which is a float value called `$beta`.\nBy default, beta is `1.0`, which is sufficient in most cases. The higher the value the earlier cache will be re-created.\n\n```php\n/**\n * @var mixed $key\n * @var callable $callable\n * @var \\DateInterval $ttl\n * @var \\Yiisoft\\Cache\\CacheInterface $cache\n * @var \\Yiisoft\\Cache\\Dependency\\Dependency $dependency\n */\n\n$beta = 2.0;\n$cache-\u003egetOrSet($key, $callable, $ttl, $dependency, $beta);\n```\n\n### Cache handlers\n\n\u003e Below the handler refers to the implementations of [PSR-16](https://www.php-fig.org/psr/psr-16/).\n\nThis package contains two handlers:\n\n- `Yiisoft\\Cache\\ArrayCache` - provides caching for the current request only by storing the values in an array.\n- `Yiisoft\\Cache\\NullCache` - does not cache anything reporting success for all methods calls.\n\nExtra cache handlers are implemented as separate packages:\n\n- [APCu](https://github.com/yiisoft/cache-apcu)\n- [Database](https://github.com/yiisoft/cache-db)\n- [File](https://github.com/yiisoft/cache-file)\n- [Memcached](https://github.com/yiisoft/cache-memcached)\n- [Redis](https://github.com/yiisoft/cache-redis)\n- [Wincache](https://github.com/yiisoft/cache-wincache)\n\n### Data serialization\n\nThe package provides `Yiisoft\\Cache\\Serializer\\SerializerInterface` for data serialization. It can be useful in database, file\nor Redis cache implementations. Out of box, you can use `Yiisoft\\Cache\\Serializer\\PhpSerializer` that works via PHP functions\n`serialize()` and `unserialize()`. You can make own implementation, for example:\n\n```php\nuse Yiisoft\\Cache\\Serializer\\SerializerInterface;\n\nfinal class IgbinarySerializer implements SerializerInterface \n{\n    public function serialize(mixed $value) : string\n    {\n        return igbinary_serialize($value);\n    }\n\n    public function unserialize(string $data) : mixed\n    {\n        return igbinary_unserialize($data);\n    }\n}\n```\n\n## Documentation\n\n- [Internals](docs/internals.md)\n\nIf you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for\nthat. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).\n\n## License\n\nThe Yii Caching Library is free software. It is released under the terms of the BSD License.\nPlease see [`LICENSE`](./LICENSE.md) for more information.\n\nMaintained by [Yii Software](https://www.yiiframework.com/).\n\n## Support the project\n\n[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective\u0026logoColor=7eadf1\u0026labelColor=555555)](https://opencollective.com/yiisoft)\n\n## Follow updates\n\n[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)\n[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter\u0026logoColor=1DA1F2\u0026labelColor=555555?style=flat)](https://twitter.com/yiiframework)\n[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat\u0026logo=telegram)](https://t.me/yii3en)\n[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat\u0026logo=facebook\u0026logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)\n[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat\u0026logo=slack)](https://yiiframework.com/go/slack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiisoft%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fcache/lists"}