gpt4 book ai didi

php - Laravel 缓存::最佳实践

转载 作者:可可西里 更新时间:2023-11-01 12:45:26 24 4
gpt4 key购买 nike

PHP 同事:

这个问题与使用 Laravel 缓存的最佳实践有关。

中心目标是减少所有通常访问数据库的次数性能相关的原因。该应用程序是一个读取密集型新闻站点,最多可能有十几个 Controller ,主要是资源类型。

应用程序设计是否有任何记录在案的最佳实践?这对我来说似乎很明显因为 Cache::是一个单行语句,所以很容易将其放入 Controller 中——返回缓存数据或调用模型并缓存结果。并使当请求更新模型时缓存(可能有急切的重新加载)。但这是一个好的做法吗?

首先看一下在 Controller 中执行此操作

/**
* 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 路由过滤器来缓存响应的建议,但我不太明白这个想法。

想法?引用?例子?

感谢大家,雷

最佳答案

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

你的 Controller 不应该知道太多关于数据源的信息,我的意思是它是来自缓存还是来自数据库。

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

所以对我来说,这就是我在 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.在 Controller 上你可以这样做

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

}

关于php - Laravel 缓存::最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29290898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com