{"id":18407524,"url":"https://github.com/simplecomplex/php-cache","last_synced_at":"2025-08-16T11:07:52.392Z","repository":{"id":57051245,"uuid":"92948319","full_name":"simplecomplex/php-cache","owner":"simplecomplex","description":"PSR-16 Simple Cache file-based cache and cache management","archived":false,"fork":false,"pushed_at":"2020-05-02T07:03:48.000Z","size":109,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-19T18:27:31.726Z","etag":null,"topics":["cache","php","php7","psr","psr-16"],"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/simplecomplex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","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":"2017-05-31T13:27:22.000Z","updated_at":"2019-08-16T10:46:20.000Z","dependencies_parsed_at":"2022-08-24T03:40:59.147Z","dependency_job_id":null,"html_url":"https://github.com/simplecomplex/php-cache","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/simplecomplex/php-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplecomplex","download_url":"https://codeload.github.com/simplecomplex/php-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270702562,"owners_count":24630877,"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","status":"online","status_checked_at":"2025-08-16T02:00:11.002Z","response_time":91,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","php7","psr","psr-16"],"created_at":"2024-11-06T03:14:21.798Z","updated_at":"2025-08-16T11:07:52.368Z","avatar_url":"https://github.com/simplecomplex.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Cache ##\n\n- [Installation](#installation)\n- [Requirements](#requirements)\n\n### Scope ###\n\nCaching of complex variables, and variables which are expensive to generate.  \nLike configuration, localization and service responses.\n\nNot page caching, no [stampede protection](https://en.wikipedia.org/wiki/Cache_stampede).\n\n### Cache abstraction ###\n\n**``` CacheBroker ```** decouples code using cache from the actual PSR-16 cache implementation.\n\nDefines three cache class aliases:\n\n- _variable time-to-live_ (default ttl and set() arg ttl)\n- _fixed time-to-live_ (default ttl, set() arg ttl ignored)\n- _persistent_ (default ttl 'forever' and set() arg ttl ignored)\n\nPlus three like the above which allow long keys; length 128 instead of the PSR-16 compliant 64.\n\n#### How to use ####\n\nAsk ``` CacheBroker ``` for an aliased type of cache instance - do _not_ instantiate a particular cache class.\n\nExtend ``` CacheBroker ```, if you later want to switch from say file-based to database-based caching.\n\n#### Dependency injection container ID: cache-broker ####\n\nRecommendation: access (and thus instantiate) the cache broker via DI container ID 'cache-broker'.  \nSee [SimpleComplex Utils](https://github.com/simplecomplex/php-utils) ``` Dependency ```.\n\n### File-based caching ###\n\n**``` FileCache ```** is a thorough and cautious PSR-16 Simple Cache implementation; file-based.  \nCoded defensively - key (and other argument) validation. \n\nAddresses:\n\n- default time-to-live; ignore set() arg ttl option; no time-to-live\n- garbage collection\n- clearing all items or expired items only\n- exporting all items, to JSON\n- building/replacing a cache store during production (using a 'candidate' store)\n- CLI interface for clearing items, e.g. via cron\n- concurrency issues (storage-wise only)\n\n\n### Cache management, replacement and backup ###\n\nDefines two extensions to the PSR-16 CacheInterface, implemented by ``` FileCache ```.\n\n**``` ManageableCacheInterface ```**  \n\n- is the cache store new or empty?\n- setting default time-to-live; setting 'ignore' set() arg ttl\n- clearing and exporting\n- listing all cache stores\n\n**``` BackupCacheInterface ```**\n\n- backup/restore\n- replacing a store, by building a 'candidate' and switching to that when it's complete\n\n### Example ###\n\n```php\n// Bootstrap.\nDependency::genericSet('cache-broker', function () {\n    return new \\SimpleComplex\\Cache\\CacheBroker();\n});\n// ...\n// Use.\n/** @var \\Psr\\Container\\ContainerInterface $container */\n$container = Dependency::container();\n/** @var \\SimpleComplex\\Cache\\CacheBroker $cache_broker */\n$cache_broker = $container-\u003eget('cache-broker');\n/**\n * Create or re-initialize a cache store.\n *\n * @var \\SimpleComplex\\Cache\\FileCache $cache_store\n */\n$cache_store = $cache_broker-\u003egetStore(\n    'some-cache-store',\n    CacheBroker::CACHE_VARIABLE_TTL\n);\nunset($cache_broker);\n/** @var mixed $whatever */\n$whatever = $cache_store-\u003eget('some-key', 'the default value');\n```\n\n### CLI commands ###\n\n```bash\n# List all cache commands and their help.\nphp cli.php cache -h\n# One command's help.\nphp cli.php cache-xxx -h\n\n# List existing cache stores.\nphp cli.php cache-list-stores\n\n# Display/get value of a cache item.\nphp cli.php cache-get store key\n\n# Delete a cache item.\nphp cli.php cache-delete store key\n\n# Delete all expired items of one or all cache stores.\nphp cli.php cache-clear-expired\n\n# Delete all items of one or all cache stores.\nphp cli.php cache-clear\n\n# Backup a cache store.\nphp cli.php cache-backup store\n\n# Restore a cache store from backup.\nphp cli.php cache-restore store\n\n# Destroy one or all cache stores.\nphp cli.php cache-destroy\n```\n\n### Installation ###\n\nCreate a 'private' files directory alongside the document root dir  \nand make it writable for the webserver user (www-data or apache).\n\nLike:  \n```/var/www/my-host/```**```http```**  \n```/var/www/my-host/```**```private```**\n\nOn first cache store instantiation, **```FileCache```** will create directory  \n```private/```**```lib/simplecomplex/file-cache```**\n\nIf that directory structure isn't suitable, do either:\n- supply **```CacheBroker```** (or **```FileCache```** constructor directly) with a 'path' argument\n- extend **```FileCache```** and override it's class constant **```PATH_DEFAULT```**\n\n### Requirements ###\n\n- PHP \u003e=7.0\n- 64-bit PHP\n- [PSR-16 Simple Cache](https://github.com/php-fig/simple-cache)\n- [SimpleComplex Utils](https://github.com/simplecomplex/php-utils)\n\n##### Suggestions #####\n\n- [SimpleComplex Inspect](https://github.com/simplecomplex/inspect)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplecomplex%2Fphp-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplecomplex%2Fphp-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplecomplex%2Fphp-cache/lists"}