繁体   English   中英

Laravel 缓存::最佳实践

[英]Laravel Cache:: Best Practices

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);
}

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

想法? 参考? 例子?

谢谢大家,雷

许多人以不同的方式执行此操作,但是当考虑最佳实践时,我认为进行缓存的最佳位置是在您的存储库中。

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

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

所以对我来说,这就是我在 Laravel 5 中滚动的方式,如果使用 Laravel 4,则没有太大区别:

//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);
        });
    }

}

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

<?php namespace App\Repositories;

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

    public function find($id);

    public function all();
}

//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'
        );
    }
}

//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);
    }

}

当然,有些技术可以避免将缓存逻辑放入控制器中,并将其交给一些第三方包来为您管理缓存。

我建议你看看这篇文章

https://github.com/imanghafoori1/laravel-widgetize

通过这种方式,您可以将页面部分组织成定义明确且自缓存的widgets类。 所以你将对缓存配置有精细的控制,你只需要设置“配置”(而不是缓存逻辑)。 像“缓存标签”、“到期时间”等。缓存逻辑是由其他人为您编写的,并提取到一个经过良好单元测试的包中。 所以它不会污染你的代码。

另一个好处是不仅可以通过这种方式保存数据库查询,而且还可以节省大量的 php 代码,以免一遍又一遍地运行,例如不需要运行 laravel Blade 后面的 php 代码,也不需要运行小部件在缓存中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM