Laravel Cache ::最佳实践

Ray*_*eur 7 php caching laravel

PHP同事:

此问题与使用Laravel Cache的最佳实践有关.

中心目标是减少对数据库的访问次数,这是出于与性能相关的所有常见原因.该应用程序是一个读取密集型新闻站点,最多可能有十几个控制器,主要是资源类型.

是否有任何记录的应用程序设计最佳实践?对我来说,似乎很明显,因为Cache ::是一行语句,所以很容易将其放入控制器 - 返回缓存数据或调用模型并缓存结果.并且在请求更新模型时使缓存无效(可能需要重新加载).但这是一个好习惯吗?

这是在控制器中执行此操作的第一个视图

/**
 * Retrieve listing of the gallery resource.
 *
 * @uses GET /gallery to return all image_collections.
 *
 * @param int $id The gallery id
 *
 * @return Response - Contains a HTTP code and a list of articles.
 */
public function index()
{
    $response_data = array();
    $response_code = 200;

    // TRY TO RETURN A CACHED RESPONSE
    $cache_key = "gallery_index";
    $response_data = Cache::get($cache_key, null);

    // IF NO CACHED RESPONSE, QUERY THE DATABASE
    if (!$response_data) {
        try {
            $response_data['items'] = $this->gallery->all();
            Cache::put($cache_key, $response_data, Config::get('app.gallery_cache_minutes'));
        } catch (PDOException $ex) {
            $response_code = 500;
            $response_data['error'] = ErrorReporter::raiseError($ex->getCode());
        }
    }

    return Response::json($response_data, $response_code);
}
Run Code Online (Sandbox Code Playgroud)

我听说过你可以使用Laravel路由过滤器缓存响应的建议,但我无法理解这个想法.

思考?参考文献?例子?

谢谢大家,雷

Eme*_*bah 19

许多人这样做的方式不同,但是当最佳实践成为一个问题时,我认为进行缓存的最佳位置是在您的存储库中.

您的控制器不应该对数据源了解太多我的意思是它来自缓存还是来自数据库.

控制器中的缓存不支持DRY(不要重复自己)方法,因为您发现自己在多个控制器和方法中复制代码,从而使脚本难以维护.

所以对我而言,如果使用laravel 4,我在Laravel 5中的滚动方式并没有那么不同:

// 1.在App/Repositories中创建GalleryEloquentRepository.php

<?php namespace App\Repositories;

use App\Models\Gallery;
use \Cache;

/**
 * Class GalleryEloquentRepository
 * @package App\Repositories
 */
class GalleryEloquentRepository implements GalleryRepositoryInterface
{

    public function all()
    {
        return Cache::remember('gallerys', $minutes='60', function()
        {
            return Gallery::all();
        });
    }


    public function find($id)
    {
        return Cache::remember("gallerys.{$id}", $minutes='60', function() use($id)
        {
            if(Cache::has('gallerys')) return Cache::has('gallerys')->find($id); //here am simply trying Laravel Collection method -find

            return Gallery::find($id);
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

// 2.在App/Repositories中创建GalleryRepositoryInterface.php

<?php namespace App\Repositories;

/**
 * Interface GalleryRepositoryInterface
 * @package App\Repositories
 */
interface GalleryRepositoryInterface
{

    public function find($id);

    public function all();
}
Run Code Online (Sandbox Code Playgroud)

// 3.在App/Providers中创建RepositoryServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

/**
 * Class RepositoryServiceProvider
 * @package App\Providers
 */
class RepositoryServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    //protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {

        $this->app->bind(
            'App\Repositories\GalleryRepositoryInterface',
            'App\Repositories\GalleryEloquentRepository'
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

// 4.在控制器上你可以做到这一点

<?php namespace App\Http\Controllers;

use \View;
use App\Repositories\GalleryRepositoryInterface;

class GalleryController extends Controller {

    public function __construct(GalleryRepositoryInterface $galleryInterface)
    {
        $this->galleryInterface = $galleryInterface;

    }


    public function index()
    {
        $gallery = $this->galleryInterface->all();

        return $gallery ? Response::json($gallery->toArray()) : Response::json($gallery,500);
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 好的答案,@ Digilimit。 (3认同)