snicholson/slimfilecache
Composer 安装命令:
composer require snicholson/slimfilecache
包简介
A file based cache middleware for the slim framework (3.0)
README 文档
README
A file based cache middleware for the slim framework (3.0). The cache is a simple file based cache, I needed something for a very specific situation where I had many external API requests (essentially an API aggregator) to make and within a given time period they always made my routes give the same output. I didn't need a database or anything fancy, and wasn't too fussed about performance. So a file based cache made a lot of sense.
Installation
Installation is done via composer
composer require snicholson/slimfilecache "~1.0"
Requirements
This middleware works with slim 3.0 Release Candidate.
Setup
The middleware conforms to slims normal injection requirements. The example below is the basic setup of the cache. Once this has been implemented, it will automatically load any active cache records for the route currently being called and halt the execution of the application.
//Start up a new slim app $slim = new App($container); // Add file cache middleware $cache = new \SNicholson\SlimFileCache\Cache($slim); $slim->add($cache);
Usage
To use the cache you simple need to cache the output of your route before returning it. The add method takes up to 6 arguments, the first one is the route name. The second one is the content, and the third argument is the response code status (e.g. 200) and fourthly is an array of headers to send, the fifth is the duration of the cache in seconds. The default length is 1 hour. The second argument for the cache is the directory to store the cache files in. The directory must be writeable to the web user.
//Start up a new slim app $slim = new App($container); // Add file cache middleware $cache = new \SNicholson\SlimFileCache\Cache($slim, 'relate/path/to/cache/directory'); $slim->add($cache); //Configure the "foo" route and cache the output $slim->get( '/foo', function(\Slim\Http\Request $request, \Slim\Http\Response $response, $args) use ($cache) { $response = 'foo response string'; $cache->add('/foo', $response); return $response; } );
Alternatively you can tell the cache to store all routes called by simple placing the following line after you have run the slim app
//Run the slim app (like normal) $slim->run(); //Place the global cache afterwards, the next request at this route will be cached $cache->add($slim->request->getUri()->getPath(), $slim->response->getBody()->__toString());
The cache has a few other simple methods. They are flush, get, remove. They essentially do what they say on the tin:
$cache = new \SNicholson\SlimFileCache\Cache($slim) //Flush removes all entries $cache->flush(); //Remove a single cache entry $cache->remove('/foo'); //Get returns what is stored in the cache $response = $cache->get('/foo');
snicholson/slimfilecache 适用场景与选型建议
snicholson/slimfilecache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.02k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 snicholson/slimfilecache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 snicholson/slimfilecache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: apache
- 更新时间: 2015-10-04