我计划有一个函数,它将使用remember()上给定的第二个参数作为键将sql语句存储在缓存中,每当sql语句更改时,它将再次针对数据库运行,并覆盖存储的sql,也覆盖缓存的结果,如果没有,它将通过remember()函数获取默认的缓存结果。
所以我计划在like \Database\Query\Builder上有类似这样的东西
/**
* Execute the query based on the cached query
*
* @param array $columns
* @return array|static[]
*/
public function getCacheByQuery($columns = array('*'))
{
if ( ! is_null($this->cacheMinutes))
{
list($key, $minutes) = $this->getCacheInfo();
// if the stored sql is the same with the new one then get the cached
// if not, remove the cached query before calling the getCached
$oldSql = self::flag($key);
$newSql = $this->toSql().implode(',', $this->bindings);
if ($newSql!==$oldSql)
{
// remove the cache
\Cache::forget($key);
// update the stored sql
self::updateFlag($key, $newSql);
}
return $this->getCached($columns);
}
return $this->getFresh($columns);
}
public static function updateFlag($flag, $value)
{
$flags = \Cache::get(t().'databaseFlags', []);
$flags[$flag] = $value;
\Cache::put(t().'databaseFlags', $flags, USER_SESSION_EXPIRATION);
}
public static function flag($flag)
{
$flags = \Cache::get(t().'databaseFlags', []);
return @$flags[$flag] ?: false;
}但问题是,我不想把它直接放在Database\Database\Query\Builder上,因为这只是我对我正在工作的当前应用程序的需要。我正在尝试扩展Illuminate\Database\Query\Builder,但问题是它没有检测到my extension类。
Call to undefined method Illuminate\Database\Query\Builder::getCachedByQuery()我的扩展类
<?php namespace Lukaserat\Traits;
class QueryBuilder extends \Illuminate\Database\Query\Builder {
/**
* Execute the query based on the caced query
*
* @param array $columns
* @return array|static[]
*/
public function getCachedByQuery($columns = array('*'))
{
if ( ! is_null($this->cacheMinutes))
{
list($key, $minutes) = $this->getCacheInfo();
// if the stored sql is the same with the new one then get the cached
// if not, remove the cached query before calling the getCached
$oldSql = self::flag($key);
$newSql = $this->toSql().implode(',', $this->bindings);
if ($newSql!==$oldSql)
{
// remove the cache
\Cache::forget($key);
// update the stored sql
self::updateFlag($key, $newSql);
}
return $this->getCached($columns);
}
return $this->getFresh($columns);
}
public static function updateFlag($flag, $value)
{
$flags = \Cache::get(t().'databaseFlags', []);
$flags[$flag] = $value;
\Cache::put(t().'databaseFlags', $flags, USER_SESSION_EXPIRATION);
}
public static function flag($flag)
{
$flags = \Cache::get(t().'databaseFlags', []);
return @$flags[$flag] ?: false;
}
}在..上实现
<?php
use LaravelBook\Ardent\Ardent;
use Lukaserat\Traits\DataTable;
use Lukaserat\Traits\QueryBuilder as QueryBuilder;
use Illuminate\Support\MessageBag as MessageBag;
class ArdentBase extends Ardent implements InterfaceArdentBase{
use DataTable;我是不是遗漏了什么?
发布于 2013-11-27 17:00:49
我通过将我在扩展类中创建的函数从getCachedByQuery重命名为get来覆盖Illuminate\Database\Query\Builder上的get()方法,因为我只是扩展了get的例程。
我变了
public function getCachedByQuery($columns = array('*'))至
public function get()在我的Lukaserat\Traits\QueryBuilder
现在它的运行情况和我预期的一样。
https://stackoverflow.com/questions/20237465
复制相似问题