stroker/cache
Composer 安装命令:
composer require stroker/cache
包简介
Provides a full page cache solution for Laminas
README 文档
README
This module provides a full page cache solution for Laminas.
Installation
Installation of StrokerCache uses composer. For composer documentation, please refer to getcomposer.org.
-
cd my/project/directory -
create or modify the
composer.jsonfile within your ZF2 application file with following contents:{ "require": { "stroker/cache": "*" } } -
install composer via
curl -s https://getcomposer.org/installer | php(on windows, download https://getcomposer.org/installer and execute it with PHP). Then runphp composer.phar install -
open
my/project/directory/configs/application.config.phpand add the following key to yourmodules:'StrokerCache',
Setup cache rules
Copy the file strokercache.local.php.dist to your config/autoload directory and rename to strokercache.local.php. Edit this file to reflect your needs.
The module provides several strategies to determine if a page should be cached.
- By routename
- By controller classname
- By regex on the URI
- Disable caching for authenticated users
Examples
Caching the home route:
<?php return [ 'strokercache' => [ 'strategies' => [ 'enabled' => [ 'StrokerCache\Strategy\RouteName' => [ 'routes' => [ 'home' ], ], ], ], ], ];
Caching the foo/bar route, but only for a GET request and only when the param id equals 60
<?php return [ 'strokercache' => [ 'strategies' => [ 'enabled' => [ 'StrokerCache\Strategy\RouteName' => [ 'routes' => [ 'foo/bar' => [ 'http_methods' => ['GET'], 'params' => ['id' => 60] ] ], ], ], ], ], ];
Change storage adapter
Storage adapter can be changed by configuration. Configuration structure is the same a StorageFactory consumes. See the Laminas reference guide. By default filesystem storage is used.
Example using APC:
<?php return array( 'strokercache' => [ 'storage_adapter' => [ 'name' => 'Laminas\Cache\Storage\Adapter\Apc', ], ], ];
TTL
You 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.
<?php return [ 'strokercache' => [ 'storage_adapter' => [ 'name' => 'filesystem', 'options' => [ 'cache_dir' => __DIR__ . '/../../data/cache' ], ], ], ];
Clearing the cache
You can invalidate cache items using the provided console route.
Alternatively you could pull strokercache_service from the servicelocator and call clearByTags directly from your application (i.e. from an event listener).
Run the following command from your project root:
php public/index.php strokercache clear <tags>
Multiple tags can be seperated by a ,.
Every page which is cached by StrokerCache is identified using the following tags:
route_<routename>: Contains the matched routename of the pagecontroller_<controllername>: Contains the controllernameparam_<paramname>_<paramvalue>: One tag for every route param
To clear every page renderered by the someAction in MyNamespace\MyController do the following:
php public/index.php strokercache clear controller_MyNamespace\MyController,param_action:some
To clear the route with alias player but only for the player with id 60.
php public/index.php strokercache clear route_player,param_id_60
Custom id generators
You can create your own id generator by implementing the StrokerCache\IdGenerator\IdGeneratorInterface. Now register your generator to the PluginManager:
<?php return [ 'strokercache' => [ 'id_generators' => [ 'plugin_manager' => [ 'invokables' => [ 'myGenerator' => 'MyNamespace\MyGenerator' ], ], ], 'id_generator' => 'myGenerator' ], ];
Custom strategies
You can create your own strategies by implementing the StrokerCache\Strategy\StrategyInterface. Now register your strategy to the pluginManager:
<?php return [ 'strokercache' => [ 'strategies' => [ 'plugin_manager' => [ 'invokables' => [ 'MyNamespace\MyCustomStrategy' ], ], ], ], ];
Next you need to enable the strategy
<?php return [ 'strokercache' => [ 'strategies' => [ 'enabled' => [ 'MyNamespace\MyCustomStrategy' ], ], ], ];
Disable FPC
You 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.
<?php return [ 'strokercache' => [ 'enabled' => false ] ];
Events
The cache service triggers several events you can utilize to add some custom logic whenever saving/loading the cache happens. The events are listed as constants in the CacheEvent class:
EVENT_LOAD: triggered when the requested page is found in the cache and ready to be served to the clientEVENT_SAVE: triggered when your page is stored in the cache storageEVENT_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.
Examples
Setting custom tags example
public function onBootstrap(MvcEvent $e) { $serviceManager = $e->getApplication()->getServiceManager(); $cacheService = $serviceManager->get('strokercache_service'); $cacheService->getEventManager()->attach(CacheEvent::EVENT_SAVE, function (CacheEvent $e) { $e->setTags(['custom_tag']); }); }
Log to file whenever a page is written to the cache storage
public function onBootstrap(MvcEvent $e) { $serviceManager = $e->getApplication()->getServiceManager(); $logger = new \Laminas\Log\Logger(); $logger->addWriter(new \Laminas\Log\Writer\Stream('/log/strokercache.log')); $cacheService = $serviceManager->get('strokercache_service'); $cacheService->getEventManager()->attach(CacheEvent::EVENT_SAVE, function (CacheEvent $e) use ($logger) { $logger->debug('Saving page to cache with ID: ' . $e->getCacheKey()); }); }
Say we want to disable caching for all requests on port 8080, we can simply listen to the SHOULDCACHE event and return false.
Keep 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.
Also you need to attach the listener at a higher priority (1000 in this example) than the buildin strategies (they are registered at priority 100).
public function onBootstrap(MvcEvent $e) { $serviceManager = $e->getApplication()->getServiceManager(); $cacheService = $serviceManager->get('strokercache_service'); $cacheService->getEventManager()->attach(CacheEvent::EVENT_SHOULDCACHE, function (CacheEvent $e) { if ($e->getMvcEvent()->getRequest()->getUri()->getPort() == 8080) { $e->stopPropagation(true); return false; } return true; }, 1000); }
If you want to avoide caching because, for instance, the user is authenticated, do the same as above, but listen on LOAD instead of SHOULDCACHE:
public function onBootstrap(MvcEvent $e) { $serviceManager = $e->getApplication()->getServiceManager(); $cacheService = $serviceManager->get('strokercache_service'); $cacheService->getEventManager()->attach(CacheEvent::EVENT_LOAD, function (CacheEvent $e) { $loggedIn = /* your logic here */; if ($loggedIn) { $e->stopPropagation(true); return false; } }, 1000); }
Attention: Be aware, that you should probably disable storing for authenticated users as well:
public function onBootstrap(MvcEvent $e) { $serviceManager = $e->getApplication()->getServiceManager(); $cacheService = $serviceManager->get('strokercache_service'); $cacheService->getEventManager()->attach(CacheEvent::EVENT_SHOULDCACHE, function (CacheEvent $e) { $loggedIn = /* your logic here */; if ($loggedIn) { $e->stopPropagation(true); return false; } }, 1000); }
Store directly to HTML files for max performance
This is still a bit expirimental. Please see this issue for some pointers how to get this working.
stroker/cache 适用场景与选型建议
stroker/cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44.88k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2012 年 12 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「zf2」 「fpc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stroker/cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stroker/cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stroker/cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
LosLog provides some log utility
EdpModuleLayouts is very simple Laminas module for making module-specific layouts insanely easy.
A framework agnostic Litespeed cache library for PHP. Can be used in any PHP application.
Zend Framework module to leverage the symfony dependency injection container
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 44.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 64
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-12-25