1

指定された2 番目のパラメーターをremember()キーとしてキャッシュに SQL ステートメントを格納する関数を計画しています。SQL ステートメントが変更されるたびに、データベースに対して再度実行され、格納された SQL とキャッシュされた結果が上書きされます。そうでない場合は、関数によってデフォルトでキャッシュされた結果が取得されremember()ます。

だから私は Illuminate\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;
}

しかし、問題は、これを Illuminate\Database\Query\Builder に直接配置したくないということです。これは、私が作業している現在のアプリケーションに必要なだけだからです。Illuminate\Database\Query\Builder を拡張しようとしていますが、問題は拡張クラスが検出されないことです。

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;

何か不足していますか?

4

1 に答える 1

0

のルーチンを拡張しただけなので、拡張クラスで作成した関数の名前をget()から に変更して、Illuminate\Database\Query\Builderのメソッドを上書きするのは正しいですか。getCachedByQuerygetget

私が変更され

public function getCachedByQuery($columns = array('*'))

public function get()

私の〜の上にLukaserat\Traits\QueryBuilder

そして、それは今、私が期待したように機能しています..

于 2013-11-27T09:00:48.563 に答える