{"id":18750643,"url":"https://github.com/webiny/cache","last_synced_at":"2026-02-03T18:34:51.944Z","repository":{"id":20219506,"uuid":"23491199","full_name":"webiny/Cache","owner":"webiny","description":"[READ-ONLY] `Cache` is a PHP component give you ability to store different information into memory for a limited time. Supports memcache, APC, Couchbase and Redis. (master at Webiny/Framework)","archived":false,"fork":false,"pushed_at":"2017-11-26T21:24:19.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-01-06T13:25:24.355Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.webiny.com/","language":"PHP","has_issues":false,"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/webiny.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2014-08-30T12:22:25.000Z","updated_at":"2024-12-17T14:13:37.000Z","dependencies_parsed_at":"2022-08-28T10:23:10.286Z","dependency_job_id":null,"html_url":"https://github.com/webiny/Cache","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/Cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240449893,"owners_count":19803125,"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-07T17:12:40.807Z","updated_at":"2026-02-03T18:34:50.427Z","avatar_url":"https://github.com/webiny.png","language":"PHP","readme":"Cache Component\n===============\n`Cache` component give you ability to store different information into memory for a limited time.\n\nInstall the component\n---------------------\nThe best way to install the component is using Composer.\n\n```bash\ncomposer require webiny/cache\n```\nFor additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/cache).\n\n## Supported drivers\n\nThe cache component supports following cache drivers:\n* `APC` (http://php.net/manual/en/book.apc.php)\n    - only available in PHP 5.4, from PHP 5.5 APC is not supported\n* `Couchbase` (http://www.couchbase.com/develop/php/current)\n* `Memcache` (http://php.net/manual/en/book.memcache.php)\n* `Redis` (http://redis.io/clients)\n\nIf you are not sure which driver to use, we suggest `Memcache`.\n\nBased on the selected driver, you'll have to pass different options to the constructor.\n\n## Requirements\n\nThe default bridged library is `Memory` by `Jamm` (https://github.com/jamm/Memory).\nIt is required that you add this library to the `ClassLoader` :\n\n```php\n    \\Webiny\\Components\\ClassLoader::getInstance()-\u003eregisterMap(['Jamm\\Memory' =\u003e 'path to memory lib']);\n```\n\nFor example:\n\n```php\n    // APC\n    $cache = \\Webiny\\Component\\Cache\\Cache::APC('cache-id');\n\n    // Couchbase\n    $cache = \\Webiny\\Component\\Cache\\Cache::Couchbase('CacheService', 'username', 'password', 'bucket', '127.0.0.1:8091');\n\n    // Memcache\n    $cache = \\Webiny\\Component\\Cache\\Cache::Memcache('CacheService', 'localhost', 11211);\n\n    // Redis\n    $cache = \\Webiny\\Component\\Cache\\Cache::Redis('CacheService', 'localhost', 6379);\n```\n\n## Common operations\n\nOnce you have created your `Cache` instance, you can start using your cache.\nThe cache methods are the same, no matter which driver you use:\n\n```php\n    // write to cache\n    $cache-\u003esave('myKey', 'some value', 600, ['tag1', 'tag2']);\n\n    // read from cache\n    $cache-\u003eread('myKey');\n\n    // delete from cache\n    $cache-\u003edelete('myKey');\n\n    // delete by tag\n    $cache-\u003edeleteByTag(['tag1']);\n```\n\n## Cache config\n\nThe preferred way of defining cache drivers is creating them inside your the config file of your application.\n\n```yaml\n    Cache:\n      TestCache:\n        Factory: \"\\Webiny\\Component\\Cache\\Cache\"\n        Method: \"Apc\"\n      SomeOtherCache:\n          Factory: \"\\Webiny\\Component\\Cache\\Cache\"\n          Method: \"Memcache\"\n          Arguments: ['127.0.0.1', '11211']\n```\n\nSee the `ExampleConfig.yaml` for additional details.\n\nUnder `Cache` you define the cache drivers by giving each of them a `cache id` and underneath you nest its config.\nThe driver configuration depends on which driver you are using.\n\nIf you wish to turn off the cache, use the `BlackHole` driver.\n\nThe `Method` parameter must be a valid callback function that will return an instance of `CacheStorage`.\n\nThe benefit of defining cache drivers in this way is that the drivers are initialized the second Webiny Framework is loaded.\nThis enables you to access the cache by 'CacheTrait'.\n\n```php\n    class MyClass\n    {\n        use \\Webiny\\Component\\Cache\\CacheTrait;\n\n        public function myMethod(){\n            $this-\u003ecache('Frontend')-\u003eread('cache_key');\n        }\n    }\n```\n\n## Custom `Cache` driver\n\nTo implement you own custom cache driver you must first create a class that will implement `\\Webiny\\Component\\Cache\\Bridge\\CacheInterface`.\nOnce you have that class, create another class with a static function that will return an instance of `CacheDriver`.\n\n```php\n    class CustomCacheDriver implements \\Webiny\\Component\\Cache\\Bridge\\CacheInterface\n    {\n        // implement the interface methods\n\n        // static method that will return the CacheDriver\n        function getDriver($cacheId, $param1, $param2, array $options){\n            $driver = new static($cacheId, $param1, $param2);\n\n            return \\Webiny\\Component\\Cache\\CacheDriver($driver, $options);\n        }\n    }\n```\n\nNow just set your class and the static method as the `Method` inside your config and you can access the cache\nover the `CacheTrait`.\n\nResources\n---------\n\nTo run unit tests, you need to use the following command:\n\n    $ cd path/to/Webiny/Component/Cache/\n    $ composer.phar install\n    $ phpunit\n\nMake sure that you also set your cache driver details in `Tests/ExampleConfig.yaml`.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fcache/lists"}