{"id":13617499,"url":"https://github.com/asm89/twig-cache-extension","last_synced_at":"2025-04-14T06:34:00.297Z","repository":{"id":6944750,"uuid":"8196473","full_name":"asm89/twig-cache-extension","owner":"asm89","description":"The missing cache extension for Twig. Caching template fragments with Twig.","archived":true,"fork":false,"pushed_at":"2020-10-05T13:12:48.000Z","size":51,"stargazers_count":388,"open_issues_count":9,"forks_count":27,"subscribers_count":26,"default_branch":"master","last_synced_at":"2024-11-08T02:33:13.889Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asm89.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":"2013-02-14T09:07:38.000Z","updated_at":"2024-05-21T14:40:04.000Z","dependencies_parsed_at":"2022-07-28T23:48:45.201Z","dependency_job_id":null,"html_url":"https://github.com/asm89/twig-cache-extension","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asm89%2Ftwig-cache-extension","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asm89%2Ftwig-cache-extension/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asm89%2Ftwig-cache-extension/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asm89%2Ftwig-cache-extension/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asm89","download_url":"https://codeload.github.com/asm89/twig-cache-extension/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248835164,"owners_count":21169173,"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-08-01T20:01:42.750Z","updated_at":"2025-04-14T06:33:59.939Z","avatar_url":"https://github.com/asm89.png","language":"PHP","funding_links":[],"categories":["Templating","PHP","模板","目录","模板引擎( Templating )"],"sub_categories":["模板 Templating"],"readme":"Twig cache extension\n====================\n\n**This extension moved to the Twig organization. Check out the repository over\nthere instead: https://github.com/twigphp/twig-cache-extension.**\n\nThe missing cache extension for Twig. The extension allows for caching rendered parts of\ntemplates using several cache strategies.\n\n[![Build Status](https://secure.travis-ci.org/asm89/twig-cache-extension.png?branch=master)](http://travis-ci.org/asm89/twig-cache-extension)\n\n## Installation\n\nThe extension is installable via composer:\n\n```json\n{\n    \"require\": {\n        \"asm89/twig-cache-extension\": \"~1.0\"\n    }\n}\n```\n\n## Quick start\n\n### Setup\n\nA minimal setup for adding the extension with the `LifeTimeCacheStrategy` and\ndoctrine array cache is as following:\n\n```php\n\u003c?php\n\nuse Doctrine\\Common\\Cache\\ArrayCache;\nuse Asm89\\Twig\\CacheExtension\\CacheProvider\\DoctrineCacheAdapter;\nuse Asm89\\Twig\\CacheExtension\\CacheStrategy\\LifetimeCacheStrategy;\nuse Asm89\\Twig\\CacheExtension\\Extension as CacheExtension;\n\n$cacheProvider  = new DoctrineCacheAdapter(new ArrayCache());\n$cacheStrategy  = new LifetimeCacheStrategy($cacheProvider);\n$cacheExtension = new CacheExtension($cacheStrategy);\n\n$twig-\u003eaddExtension($cacheExtension);\n```\n\n### Want to use a PSR-6 cache pool?\n\nInstead of using the default `DoctrineCacheAdapter` the extension also has \na `PSR-6` compatible adapter. You need to instantiate one of the cache pool\nimplementations as can be found on: http://php-cache.readthedocs.io/en/latest/\n\nExample: Making use of the `ApcuCachePool` via the `PsrCacheAdapter`:\n\n```bash\ncomposer require cache/apcu-adapter\n```\n\n```php\n\u003c?php\n\nuse Asm89\\Twig\\CacheExtension\\CacheProvider\\PsrCacheAdapter;\nuse Asm89\\Twig\\CacheExtension\\CacheStrategy\\LifetimeCacheStrategy;\nuse Asm89\\Twig\\CacheExtension\\Extension as CacheExtension;\nuse Cache\\Adapter\\Apcu\\ApcuCachePool;\n\n$cacheProvider  = new PsrCacheAdapter(new ApcuCachePool());\n$cacheStrategy  = new LifetimeCacheStrategy($cacheProvider);\n$cacheExtension = new CacheExtension($cacheStrategy);\n\n$twig-\u003eaddExtension($cacheExtension);\n```\n\n### Usage\n\nTo cache a part of a template in Twig surround the code with a `cache` block.\nThe cache block takes two parameters, first an \"annotation\" part, second the\n\"value\" the cache strategy can work with. Example:\n\n```jinja\n{% cache 'v1/summary' 900 %}\n    {# heavy lifting template stuff here, include/render other partials etc #}\n{% endcache %}\n```\n\nCache blocks can be nested:\n\n```jinja\n{% cache 'v1' 900 %}\n    {% for item in items %}\n        {% cache 'v1' item %}\n            {# ... #}\n        {% endcache %}\n    {% endfor %}\n{% endcache %}\n```\n\nThe annotation can also be an expression:\n\n```jinja\n{% set version = 42 %}\n{% cache 'hello_v' ~ version 900 %}\n    Hello {{ name }}!\n{% endcache %}\n```\n\n## Cache strategies\n\nThe extension ships with a few cache strategies out of the box. Setup and usage\nof all of them is described below.\n\n### Lifetime\n\nSee the [\"Quick start\"](#quick-start) for usage information of the `LifetimeCacheStrategy`.\n\n### Generational\n\nStrategy for generational caching.\n\nIn theory the strategy only saves fragments to the cache with infinite\nlifetime. The key of the strategy lies in the fact that the keys for blocks\nwill change as the value for which the key is generated changes.\n\nFor example: entities containing a last update time, would include a timestamp\nin the key. For an interesting blog post about this type of caching see:\nhttp://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works\n\n### Blackhole\n\nStrategy for development mode.\n\nIn development mode it often not very useful to cache fragments. The blackhole\nstrategy provides an easy way to not cache anything it all. It always generates\na new key and does not fetch or save any fragments.\n\n#### Setup\n\nIn order to use the strategy you need to setup a `KeyGenerator` class that is\nable to generate a cache key for a given value.\n\nThe following naive example always assumes the value is an object with the methods\n`getId()` and `getUpdatedAt()` method. The key then composed from the class\nname, the id and the updated time of the object:\n\n```php\n\u003c?php\n\nuse Asm89\\Twig\\CacheExtension\\CacheStrategy\\KeyGeneratorInterface;\n\nclass MyKeyGenerator implements KeyGeneratorInterface\n{\n    public function generateKey($value)\n    {\n        return get_class($value) . '_' . $value-\u003egetId() . '_' . $value-\u003egetUpdatedAt();\n    }\n\n}\n```\n\nNext the `GenerationalCacheStrategy` needs to be setup with the keygenerator.\n\n```php\n\u003c?php\n\nuse Asm89\\Twig\\CacheExtension\\CacheStrategy\\GenerationalCacheStrategy;\nuse Asm89\\Twig\\CacheExtension\\Extension as CacheExtension;\n\n$keyGenerator   = new MyKeyGenerator();\n$cacheProvider  = /* see Quick start */;\n$cacheStrategy  = new GenerationalCacheStrategy($cacheProvider, $keyGenerator, 0 /* = infinite lifetime */);\n$cacheExtension = new CacheExtension($cacheStrategy);\n\n$twig-\u003eaddExtension($cacheExtension);\n```\n\n#### Usage\n\nThe strategy expects an object as value for determining the cache key of the\nblock:\n\n```jinja\n{% cache 'v1/summary' item %}\n    {# heavy lifting template stuff here, include/render other partials etc #}\n{% endcache %}\n```\n\n### Using multiple strategies\n\nDifferent cache strategies are useful for different usecases. It is possible to\nmix multiple strategies in an application with the\n`IndexedChainingCacheStrategy`. The strategy takes an array of `'name' =\u003e\n$strategy` and delegates the caching to the appropriate strategy.\n\n#### Setup\n\n```php\n\u003c?php\n\nuse Asm89\\Twig\\CacheExtension\\CacheStrategy\\IndexedChainingCacheStrategy;\nuse Asm89\\Twig\\CacheExtension\\Extension as CacheExtension;\n\n$cacheStrategy  = new IndexedChainingCacheStrategy(array(\n    'time' =\u003e $lifetimeCacheStrategy,\n    'gen'  =\u003e $generationalCacheStrategy,\n));\n$cacheExtension = new CacheExtension($cacheStrategy);\n\n$twig-\u003eaddExtension($cacheExtension);\n```\n\n#### Usage\n\nThe strategy expects an array with as key the name of the strategy which it\nneeds to delegate to and as value the appropriate value for the delegated\nstrategy.\n\n```jinja\n{# delegate to lifetime strategy #}\n{% cache 'v1/summary' {time: 300} %}\n    {# heavy lifting template stuff here, include/render other partials etc #}\n{% endcache %}\n\n{# delegate to generational strategy #}\n{% cache 'v1/summary' {gen: item} %}\n    {# heavy lifting template stuff here, include/render other partials etc #}\n{% endcache %}\n```\n\n## Implementing a cache strategy\n\nCreating separate caches for different access levels, languages or other\nusecases can be done by implementing a custom cache strategy. In order to do so\nimplement the `CacheProviderInterface`. It is recommended to use composition\nand wrap a custom strategy around an existing one.\n\n## Authors\n\nAlexander \u003ciam.asm89@gmail.com\u003e\n\n## License\n\ntwig-cache-extension is licensed under the MIT License - see the LICENSE file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasm89%2Ftwig-cache-extension","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasm89%2Ftwig-cache-extension","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasm89%2Ftwig-cache-extension/lists"}