sci/cacheable
最新稳定版本:1.0.1
Composer 安装命令:
composer require sci/cacheable
包简介
Transparent caching library
README 文档
README
Installation
Using composer:
composer require sci/cacheable
Usage
Lets say, you have a class Foo, implementing a method Foo::bar() with quite high time/resource consumption:
class Foo { public function bar($a, $b) { // make some hard things with $a and $b ... return ...; // some result } } $foo = new Foo(); $bar = $foo->bar(1, 2); // takes some amount of time // and later, again... $bar = $foo->bar(1, 2); // takes the same amount of time, again
If there are no side-effects, the result of Foo::bar() is determined only by its arguments $a and $b.
So you could use some cache, if eventuelly the method is called again.
To avoid messing around with cache keys, you can use sci\cacheable:
use Sci\Cacheable; use Sci\CacheTrait; class Foo implements Cacheable { use CacheTrait; public function bar($a, $b) { // make some hard things with $a and $b ... return ...; // some result } } $foo = new Foo(); $foo->setCache(/* any PSR-6 cache pool interface */) $bar = $foo->cache()->bar(1, 2); // 1st call takes some time, but now, the result is stored into cache // and later, again... $bar = $foo->cache()->bar(1, 2); // 2nd call's result comes directly from cache
en détail
- A class to be cached (
Fooin example above) implements the interfaceSci\Cacheable(which demands a methodcache()). - This can be done using
Sci\CacheTrait(which additially provides a methodsetCache()). - The method
setCachegets as first argument a service, implementingPsr\Cache\CacheItemPoolInterface.- This cache pool is the actual cache backend.
- An optional second argument to
setCacheis the default TTL (time to live) for all cached values.
- To use the cache, method calls are proxied through the method
cache(), e.g. instead of$foo->bar(1, 2), now we use$foo->cache()->bar(1, 2).- Method
cache()returns$this, i.e. it acts in a transparent way (fluent interface). - An optional argument for
cache()allows to specify a TTL, different from the default one.
- Method
Implementation
License
All contents of this package are licensed under the MIT license.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-26