104corp/cache
最新稳定版本:1.0.0
Composer 安装命令:
composer require 104corp/cache
包简介
PSR-16 simple cache implements written by PHP.
README 文档
README
PSR-16 simple cache implements written by PHP.
系統需求
- PHP >= 5.5
安裝
使用 Composer 安裝
$ composer require 104corp/cache
說明
Cache 實作遵守 PSR-16 Simple Cache,並提供 CacheAwareTrait ,可以方便地掛載在需要 Cache 的元件上;提供幾種常見的實作,可以直接套用。
因實作遵守 PSR-16 規定,如果覺得不夠用的話,也可以直接無痛改用第三方套件。
基本
如果需要做 Cache 的話,首先先掛上 CacheAwareTrait ,並實作必要的方法 getDefaultTtl() ,接著就能開始使用了。下面是一個簡單的範例:
use Corp104\Cache\Util\CacheAwareTrait; class Resource { use CacheAwareTrait; public function getDefaultTtl() { // 60 second return 60; } public function getData() { $data = null; if (null !== $this->cache) { $data = $this->cache->get('some-resource-key', null); } if (null === $data) { $data = $this->getRealData(); if (null !== $this->cache) { $this->cache->set('some-resource-key', $data, $this->getTtl()); } } return $data; } private function getRealData() { return 'RealData'; } }
實際要使用 Resource 物件時,只要傳入適當的 cache driver 即可運作,如 Symfony Cache 。
$cacheInstance = new \Symfony\Component\Cache\Simple\PhpFilesCache(); $resource = new Resource(); $resource->setCache($cacheInstance); $data = $resource->getData();
實作
Corp104\Cache\FileCache- 使用 PHP 檔案當作 cache 。
測試替身
Cache 元件實作 Psr\SimpleCache\CacheInterface ,因此可以直接使用此介面來產生 Stub / Spy / Mock 。
測試階段可使用 Corp104\Cache\ArrayCache 來測元件與 Cache 的互動和結果是否正常。
Contributing
開發相關資訊可以參考 CONTRIBUTING ,有任何問題或建議,歡迎發 issue ;如果覺得程式碼可以修更好的話,也歡迎發 PR 修正。
PR 如何使用可以參考 Git 官方文件。
统计信息
- 总下载量: 48.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-08-03