{"id":15643038,"url":"https://github.com/bramstroker/zf2-fullpage-cache","last_synced_at":"2025-04-13T08:18:36.643Z","repository":{"id":6054726,"uuid":"7279885","full_name":"bramstroker/zf2-fullpage-cache","owner":"bramstroker","description":"Full page cache module for ZF2 and ZF3","archived":false,"fork":false,"pushed_at":"2020-03-20T11:13:05.000Z","size":274,"stargazers_count":60,"open_issues_count":2,"forks_count":24,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-13T08:18:21.448Z","etag":null,"topics":[],"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/bramstroker.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":"2012-12-21T22:18:19.000Z","updated_at":"2023-07-17T10:13:08.000Z","dependencies_parsed_at":"2022-08-24T13:39:38.490Z","dependency_job_id":null,"html_url":"https://github.com/bramstroker/zf2-fullpage-cache","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bramstroker%2Fzf2-fullpage-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bramstroker%2Fzf2-fullpage-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bramstroker%2Fzf2-fullpage-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bramstroker%2Fzf2-fullpage-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bramstroker","download_url":"https://codeload.github.com/bramstroker/zf2-fullpage-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681522,"owners_count":21144703,"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-10-03T11:58:42.497Z","updated_at":"2025-04-13T08:18:36.618Z","avatar_url":"https://github.com/bramstroker.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StrokerCache\n\n[![Build Status](https://travis-ci.org/bramstroker/zf2-fullpage-cache.png?branch=master)](https://travis-ci.org/bramstroker/zf2-fullpage-cache)\n[![Code Coverage](https://scrutinizer-ci.com/g/bramstroker/zf2-fullpage-cache/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/bramstroker/zf2-fullpage-cache/?branch=master)\n[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/bramstroker/zf2-fullpage-cache/badges/quality-score.png?s=82cfa6f87dbe10c8c9d9e74ca62027a80a8c9cfb)](https://scrutinizer-ci.com/g/bramstroker/zf2-fullpage-cache/)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/b99c2f08-32c2-4c57-923e-a0be3af98227/mini.png)](https://insight.sensiolabs.com/projects/b99c2f08-32c2-4c57-923e-a0be3af98227)\n[![Total Downloads](https://poser.pugx.org/stroker/cache/downloads.svg)](https://packagist.org/packages/stroker/cache)\n[![Latest Stable Version](https://poser.pugx.org/stroker/cache/v/stable.svg)](https://packagist.org/packages/stroker/cache)\n\nThis module provides a full page cache solution for Laminas.\n\n## Installation\n\nInstallation of StrokerCache uses composer. For composer documentation, please refer to\n[getcomposer.org](http://getcomposer.org/).\n\n  1. `cd my/project/directory`\n  2. create or modify the `composer.json` file within your ZF2 application file with\n     following contents:\n\n     ```json\n     {\n         \"require\": {\n             \"stroker/cache\": \"*\"\n         }\n     }\n     ```\n  3. install composer via `curl -s https://getcomposer.org/installer | php` (on windows, download\n     https://getcomposer.org/installer and execute it with PHP). Then run `php composer.phar install`\n  4. open `my/project/directory/configs/application.config.php` and add the following key to your `modules`:\n\n     ```php\n     'StrokerCache',\n     ```\n\n## Setup cache rules\n\nCopy the file `strokercache.local.php.dist` to your config/autoload directory and rename to `strokercache.local.php`. Edit this file to reflect your needs.\n\nThe module provides several strategies to determine if a page should be cached.\n\n- By routename\n- By controller classname\n- By regex on the URI\n- Disable caching for authenticated users\n\n### Examples\n\nCaching the home route:\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'strategies' =\u003e [\n            'enabled' =\u003e [\n                'StrokerCache\\Strategy\\RouteName' =\u003e [\n                    'routes' =\u003e [\n                        'home'\n                    ],\n                ],\n            ],\n        ],\n    ],\n];\n```\n\nCaching the `foo/bar` route, but only for a GET request and only when the param `id` equals 60\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'strategies' =\u003e [\n            'enabled' =\u003e [\n                'StrokerCache\\Strategy\\RouteName' =\u003e [\n                    'routes' =\u003e [\n                        'foo/bar' =\u003e [\n                            'http_methods' =\u003e ['GET'],\n                            'params' =\u003e ['id' =\u003e 60]\n                        ]\n                    ],\n                ],\n            ],\n        ],\n    ],\n];\n```\n\n## Change storage adapter\n\nStorage adapter can be changed by configuration. Configuration structure is the same a StorageFactory consumes. See the [Laminas reference guide](https://docs.laminas.dev/laminas-cache/storage/adapter/).\nBy default filesystem storage is used.\n\nExample using APC:\n```php\n\u003c?php\nreturn array(\n    'strokercache' =\u003e [\n        'storage_adapter' =\u003e [\n            'name' =\u003e 'Laminas\\Cache\\Storage\\Adapter\\Apc',\n        ],\n    ],\n];\n```\n\n## TTL\n\nYou can set the TTL (Time to live) for the cache items by specifying the option on the storage adapter configuration. Not all Laminas storage adapters support TTL, which also is the reason why StrokerCache doesn't support per item TTL at the moment.\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'storage_adapter' =\u003e [\n            'name' =\u003e 'filesystem',\n            'options' =\u003e [\n              'cache_dir' =\u003e __DIR__ . '/../../data/cache'\n            ],\n        ],\n    ],\n];\n```\n\n## Clearing the cache\n\nYou can invalidate cache items using the provided console route. \nAlternatively you could pull `strokercache_service` from the servicelocator and call `clearByTags` directly from your application (i.e. from an event listener).\n\nRun the following command from your project root:\n`php public/index.php strokercache clear \u003ctags\u003e`\n\nMultiple tags can be seperated by a `,`.\nEvery page which is cached by StrokerCache is identified using the following tags:\n- `route_\u003croutename\u003e`: Contains the matched routename of the page\n- `controller_\u003ccontrollername\u003e`: Contains the controllername\n- `param_\u003cparamname\u003e_\u003cparamvalue\u003e`: One tag for every route param\n\nTo clear every page renderered by the `someAction` in `MyNamespace\\MyController` do the following:\n`php public/index.php strokercache clear controller_MyNamespace\\MyController,param_action:some`\n\nTo clear the route with alias `player` but only for the player with id 60. \n`php public/index.php strokercache clear route_player,param_id_60`\n\n## Custom id generators\n\nYou can create your own id generator by implementing the StrokerCache\\IdGenerator\\IdGeneratorInterface.\nNow register your generator to the PluginManager:\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'id_generators' =\u003e [\n            'plugin_manager' =\u003e [\n                'invokables' =\u003e [\n                    'myGenerator' =\u003e 'MyNamespace\\MyGenerator'\n                ],\n            ],\n        ],\n        'id_generator' =\u003e 'myGenerator'\n    ],\n];\n```\n\n## Custom strategies\n\nYou can create your own strategies by implementing the StrokerCache\\Strategy\\StrategyInterface. Now register your strategy to the pluginManager:\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'strategies' =\u003e [\n            'plugin_manager' =\u003e [\n                'invokables' =\u003e [\n                    'MyNamespace\\MyCustomStrategy'\n                ],\n            ],\n        ],\n    ],\n];\n```\n\nNext you need to enable the strategy\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'strategies' =\u003e [\n            'enabled' =\u003e [\n                'MyNamespace\\MyCustomStrategy'\n            ],\n        ],\n    ],\n];\n```\n\n## Disable FPC\n\nYou can disable the Caching solution all together by using the following configuration. This comes in handy on your development environment where you obviously don't want any caching to happen.\n\n```php\n\u003c?php\nreturn [\n    'strokercache' =\u003e [\n        'enabled' =\u003e false        \n    ]\n];\n```\n\n## Events\n\nThe cache service triggers several events you can utilize to add some custom logic whenever saving/loading the cache happens.\nThe events are listed as constants in the [CacheEvent](https://github.com/bramstroker/zf2-fullpage-cache/blob/master/src/StrokerCache/Event/CacheEvent.php) class:\n\n- `EVENT_LOAD`: triggered when the requested page is found in the cache and ready to be served to the client\n- `EVENT_SAVE`: triggered when your page is stored in the cache storage\n- `EVENT_SHOULDCACHE`: this event is used to determine if a page should be stored into the cache. You can listen to this event if you don't want the page to be cached. All the strategies are attached to this event as well.\n\n### Examples\n\nSetting custom tags example\n\n```php\npublic function onBootstrap(MvcEvent $e)\n{\n    $serviceManager = $e-\u003egetApplication()-\u003egetServiceManager();\n    $cacheService = $serviceManager-\u003eget('strokercache_service');\n    $cacheService-\u003egetEventManager()-\u003eattach(CacheEvent::EVENT_SAVE, function (CacheEvent $e) {\n        $e-\u003esetTags(['custom_tag']);\n    });\n}\n```\n\nLog to file whenever a page is written to the cache storage\n\n```php\npublic function onBootstrap(MvcEvent $e)\n{\n    $serviceManager = $e-\u003egetApplication()-\u003egetServiceManager();\n    $logger = new \\Laminas\\Log\\Logger();\n    $logger-\u003eaddWriter(new \\Laminas\\Log\\Writer\\Stream('/log/strokercache.log'));\n    $cacheService = $serviceManager-\u003eget('strokercache_service');\n    $cacheService-\u003egetEventManager()-\u003eattach(CacheEvent::EVENT_SAVE, function (CacheEvent $e) use ($logger) {\n        $logger-\u003edebug('Saving page to cache with ID: ' . $e-\u003egetCacheKey());\n    });\n}\n```\n\nSay we want to disable caching for all requests on port 8080, we can simply listen to the `SHOULDCACHE` event and return `false`.\nKeep in mind you want to prevent other listeners from executing using `stopPropagation()`. If you don't do this other listeners will be executed and whenever one of them returns `true` the page will be cached.\nAlso you need to attach the listener at a higher priority (`1000` in this example) than the buildin strategies (they are registered at priority `100`).\n\n```php\npublic function onBootstrap(MvcEvent $e)\n{\n    $serviceManager = $e-\u003egetApplication()-\u003egetServiceManager();\n    $cacheService = $serviceManager-\u003eget('strokercache_service');\n    $cacheService-\u003egetEventManager()-\u003eattach(CacheEvent::EVENT_SHOULDCACHE, function (CacheEvent $e) {\n        if ($e-\u003egetMvcEvent()-\u003egetRequest()-\u003egetUri()-\u003egetPort() == 8080) {\n            $e-\u003estopPropagation(true);\n            return false;\n        }\n        return true;\n    }, 1000);\n}\n```\n\nIf you want to avoide caching because, for instance, the user is authenticated, do the same as above, but listen on `LOAD` instead of `SHOULDCACHE`:\n\n\n```php\npublic function onBootstrap(MvcEvent $e)\n{\n    $serviceManager = $e-\u003egetApplication()-\u003egetServiceManager();\n    $cacheService = $serviceManager-\u003eget('strokercache_service');\n    $cacheService-\u003egetEventManager()-\u003eattach(CacheEvent::EVENT_LOAD, function (CacheEvent $e) {\n        $loggedIn = /* your logic here */;\n        if ($loggedIn) {\n            $e-\u003estopPropagation(true);\n            return false;\n        }\n    }, 1000);\n}\n```\n\n**Attention:** Be aware, that you should probably disable storing for authenticated users as well:\n\n```php\npublic function onBootstrap(MvcEvent $e)\n{\n    $serviceManager = $e-\u003egetApplication()-\u003egetServiceManager();\n    $cacheService = $serviceManager-\u003eget('strokercache_service');\n    $cacheService-\u003egetEventManager()-\u003eattach(CacheEvent::EVENT_SHOULDCACHE, function (CacheEvent $e) {\n        $loggedIn = /* your logic here */;\n        if ($loggedIn) {\n            $e-\u003estopPropagation(true);\n            return false;\n        }\n    }, 1000);\n}\n```\n\n## Store directly to HTML files for max performance\n\nThis is still a bit expirimental. Please see [this issue](https://github.com/bramstroker/zf2-fullpage-cache/issues/33) for some pointers how to get this working.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbramstroker%2Fzf2-fullpage-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbramstroker%2Fzf2-fullpage-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbramstroker%2Fzf2-fullpage-cache/lists"}