rapidwebltd / caching-object-wrapper
将任何PHP对象包装起来,使其所有方法都被缓存。
v1.0.2
2018-03-21 14:14 UTC
Requires
- php: >=5.6
- psr/cache: ^1.0
Requires (Dev)
- cache/integration-tests: ^0.16.0
- phpunit/phpunit: ^5.7
- satooshi/php-coveralls: ^2.0
This package is auto-updated.
Last update: 2024-09-06 08:58:15 UTC
README
将任何PHP对象包装起来,使其所有方法都被缓存。
安装
您可以通过Composer轻松安装此软件包。只需从您项目的根目录运行以下命令。
composer require rapidwebltd/caching-object-wrapper
用法
以下是一个非常简单的用法示例,展示我们如何轻松地缓存一个简单的对象。
use RapidWeb\CachingObjectWrapper\CachingObjectWrapper; use rapidweb\RWFileCachePSR6\CacheItemPool; // This is an example class that we are going to use. It just generates random numbers. class RandomNumberGenerator { public function generate() { return rand(); } } // First, we need to install and setup a PSR6 cache item pool. // As an example, we'll use the PSR-6 adapter for RW File Cache. // $ composer require rapidwebltd/rw-file-cache-psr-6 $cache = new CacheItemPool(); $cache->changeConfig( [ 'cacheDirectory' => '/tmp/cow-tests/', 'gzipCompression' => true, ] ); // Next, we can wrap up a new RandomNumberGenerator. // We also pass in the $cache object we just created, and the desired cache expiry time in seconds. $randomNumberGenerator = new CachingObjectWrapper(new RandomNumberGenerator(), $cache, 60 * 60); // That's it! // To test, we can tell the wrapped object to generate us two random numbers. $randomNumber1 = $randomNumberGenerator->generate(); $randomNumber2 = $randomNumberGenerator->generate(); // Due to our caching, $randomNumber1 and $randomNumber2 should be identical.