{"id":18929364,"url":"https://github.com/thecodingmachine/cache-utils","last_synced_at":"2025-04-15T15:30:54.364Z","repository":{"id":57067878,"uuid":"191381232","full_name":"thecodingmachine/cache-utils","owner":"thecodingmachine","description":"Store file related cache items easily","archived":false,"fork":false,"pushed_at":"2024-06-25T17:17:21.000Z","size":35,"stargazers_count":2,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T14:32:46.723Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-11T13:49:43.000Z","updated_at":"2020-09-20T02:41:18.000Z","dependencies_parsed_at":"2024-06-20T18:56:58.854Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/cache-utils","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"2da4dca218106d8e5902a054e44cab378821c70f"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcache-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcache-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcache-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcache-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/cache-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249097803,"owners_count":21212356,"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":[],"created_at":"2024-11-08T11:32:13.269Z","updated_at":"2025-04-15T15:30:54.109Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/thecodingmachine/cache-utils/v/stable)](https://packagist.org/packages/thecodingmachine/cache-utils)\n[![Total Downloads](https://poser.pugx.org/thecodingmachine/cache-utils/downloads)](https://packagist.org/packages/thecodingmachine/cache-utils)\n[![Latest Unstable Version](https://poser.pugx.org/thecodingmachine/cache-utils/v/unstable)](https://packagist.org/packages/thecodingmachine/cache-utils)\n[![License](https://poser.pugx.org/thecodingmachine/cache-utils/license)](https://packagist.org/packages/thecodingmachine/cache-utils)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/cache-utils/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/thecodingmachine/cache-utils/?branch=master)\n[![Build Status](https://travis-ci.org/thecodingmachine/cache-utils.svg?branch=master)](https://travis-ci.org/thecodingmachine/cache-utils)\n[![Coverage Status](https://coveralls.io/repos/thecodingmachine/cache-utils/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/thecodingmachine/cache-utils?branch=master)\n\n## Why?\n\nThis package contains a number of utility classes to play with PSR-6 caches.\n\n### File bound cache\n\nMost PHP cache systems (like PSR-6 or PSR-16) are storing items in cache and attributing to items a time to live (TTL).\n\nIf you are developing a PHP framework or a PHP analysis library that relies a lot on reflection, it is quite common \nto have cache items that are related to PHP files or PHP classes.\n\nFor instance, Doctrine Annotations in a class do not change unless the class file(s) is changed. Therefore, it makes\nsense to bind the cache invalidation to the modification date of the file. *thecodingmachine/cache-utils* provides just that.\n\n```php\nuse TheCodingMachine\\CacheUtils\\FileBoundCache;\n\n$fileBoundCache = new FileBoundCache($psr6Cache);\n\n// Put the $myDataToCache object in cache.\n// If 'FooBar.php' and 'FooBaz.php' are modified, the cache item is purged.\n$fileBoundCache-\u003eset('cache_key', $myDataToCache, \n[\n    'FooBar.php',\n    'FooBaz.php'\n]);\n\n// Fetching data\n$myDataToCache = $fileBoundCache-\u003eget('cache_key');\n```\n\nYou can also use the `FileBoundMemoryAdapter` to store the cache in memory for even faster access in the same query.\n\n```php\nuse TheCodingMachine\\CacheUtils\\FileBoundCache;\nuse TheCodingMachine\\CacheUtils\\FileBoundMemoryAdapter;\n\n$fileBoundCache = new FileBoundMemoryAdapter(new FileBoundCache($psr6Cache));\n```\n\n### Class bound cache\n\nYou can also bind a cache item to a class / trait / interface using the `ClassBoundCache` class.\nThe cache will expire if the class / trait / interface is modified.\n\n```php\nuse TheCodingMachine\\CacheUtils\\FileBoundCache;\nuse TheCodingMachine\\CacheUtils\\ClassBoundCache;\n\n$fileBoundCache = new FileBoundCache($psr6Cache);\n$classBoundCache = new ClassBoundCache($fileBoundCache);\n\n// Put the $myDataToCache object in cache.\n// If the FooBar class is modified, the cache item is purged.\n$classBoundCache-\u003eset('cache_key', $myDataToCache, new ReflectionClass(FooBar::class));\n\n// Fetching data\n$myDataToCache = $classBoundCache-\u003eget('cache_key');\n```\n\nThe `ClassBoundCache` constructor accepts 3 additional parameters:\n\n```php\n\nclass ClassBoundCache implements ClassBoundCacheInterface\n{\n    public function __construct(FileBoundCacheInterface $fileBoundCache, bool $analyzeParentClasses = true, bool $analyzeTraits = true, bool $analyzeInterfaces = false)\n}\n```\n\n- `$analyzeParentClasses`: if set to true, the cache will be invalidated if one of the parent classes is modified\n- `$analyzeTraits`: if set to true, the cache will be invalidated if one of the traits is modified\n- `$analyzeInterfaces`: if set to true, the cache will be invalidated if one of the interfaces implemented is modified\n\nYou can also use the `ClassBoundMemoryAdapter` to store the cache in memory for even faster access in the same query.\n\n```php\nuse TheCodingMachine\\CacheUtils\\ClassBoundCache;\nuse TheCodingMachine\\CacheUtils\\ClassBoundMemoryAdapter;\n\n$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));\n```\n\n### Easier interface with cache contracts\n\nYou can even get an easier to use class bound cache using the `ClassBoundCacheContract`.\n\n```php\nuse TheCodingMachine\\CacheUtils\\FileBoundCache;\nuse TheCodingMachine\\CacheUtils\\ClassBoundCache;\nuse TheCodingMachine\\CacheUtils\\ClassBoundMemoryAdapter;\nuse TheCodingMachine\\CacheUtils\\ClassBoundCacheContract;\n\n$fileBoundCache = new FileBoundCache($psr6Cache);\n$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));\n$classBoundCacheContract = new ClassBoundCacheContract(new ClassBoundCache($fileBoundCache));\n\n// If the FooBar class is modified, the cache item is purged.\n$item = $classBoundCache-\u003eget(new ReflectionClass(FooBar::class), function() {\n    // ...\n    // let's return the item to be cached.\n    // this function is called only if the item is not in cache yet.\n    return $item;\n});\n```\n\nWith cache contracts, there is not setters. Only a getter that takes in parameter a callable that will resolve the \ncache item if the item is not available in the cache.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fcache-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fcache-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fcache-utils/lists"}