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(Don't Repeat Yourself)方法,因为您发现自己在多个控制器和方法中复制代码,从而使脚本难以维护。

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

1.在应用程序/存储库中创建GalleryEloquentRepository.php

namespace App'Repositories;
use App'Models'Gallery;
use Cache;
    
/**
 * Class GalleryEloquentRepository
 * 
 * @package App'Repositories
 */
class GalleryEloquentRepository implements GalleryRepositoryInterface
{
    /**
     * Fetch all the galleries
     * 
     * @return 'Illuminate'Database'Eloquent'Collection
     */
    public function all()
    {
        return Cache::remember('gallerys', $minutes='60', function()
        {
            return Gallery::all();
        });
    }
    /**
     * Fetch a gallery
     * 
     * @return 'App'Models'Gallery
     */
    public function find($id)
    {
        return Cache::remember("gallerys.{$id}", $minutes='60', function() use($id)
        {
            return Gallery::find($id);
        });
    }
}

2.在应用程序/存储库中创建GalleryRepositoryInterface.php

namespace App'Repositories;
    
/**
 * Interface GalleryRepositoryInterface
 * 
 * @package App'Repositories
 */
interface GalleryRepositoryInterface
{
    public function find($id);
    public function all();
}

3.在App/Providers中创建RepositoryServiceProvider.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.在控制器上,您可以执行此操作

namespace App'Http'Controllers;
    
use View;
use use Illuminate'Support'Facades'Response;
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代码不需要运行,当小部件在缓存中时也不需要运行。