andydune/object-cache-proxy

允许缓存执行对象的查询结果。对工作代码透明。

v1.1.1 2018-06-14 10:30 UTC

This package is auto-updated.

Last update: 2024-09-21 20:59:11 UTC


README

Build Status Software License Packagist Version Total Downloads

允许缓存执行对象的查询结果。对工作代码透明。

安装

使用composer安装

composer require andydune/object-cache-proxy

如果composer未全局安装

php composer.phar require andydune/object-cache-proxy

或者编辑你的 composer.json

"require" : {
     "andydune/object-cache-proxy": "^1"
}

并执行命令

php composer.phar update

问题

这是任何类的实例。我们使用setter注入数据,并最终执行最后的方法来检索数据。我们经常在有限的数据范围内这样做。我们如何简单地改变它以提高速度?

$instance = new Adventures(); // any class

// Use some setters 
$instance->setDirection('up');
$instance->setSpeed('low');
$instance->setLimit(13);

// Next is results
$ways = $instance->get(); 

我们可以使用 ObjectCacheProxy 在最少的工作代码更改下进行缓存。

use AndyDune\ObjectCacheProxy\ObjectCacheProxy;
use Symfony\Component\Cache\Simple\FilesystemCache;

$instanceOrigin = new \Adventures(); // any class

$instance = new ObjectCacheProxy(new FilesystemCache());

// inject object and method name witch results will be cached. 
$instance->setObject($instanceOrigin, 'get');

// Use some setters 
$instance->setDirection('up');
$instance->setSpeed('low');
$instance->setLimit(13);

// Next is results
$ways = $instance->get(); 

ObjectCacheProxy 获取工作类实例,拦截对其调用的方法。它将setter方法和参数累积为缓存键,并在执行方法 get 之前一次性执行。