Laravel Cache :: Best Practices

PHP Colleagues:

This question is about best practices for using Laravel Cache.

The main goal is to reduce the number of database accesses for all common performance-related ones. The application is a news site with heavy reading, which probably uses no more than a dozen controllers, mostly a resource one.

Are there any documented guidelines for application design? It seems obvious to me that since Cache :: is a one-line statement, it is easy to drop it into the controllers - either return the cached data, or call the model and cache the results. And invalidate the cache (possibly with a lively reboot) when requests update the model. But is this a good practice?

Here first look at this in the 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); } 

I heard that you can use Laravel Route Filters to cache responses, but I cannot understand this idea.

Thoughts? Recommendations? Examples?

Thanks everyone Ray

+6
source share
1 answer

Many people do it differently, but when best practice is troubling, I find the best place to cache is in your repository.

Your controller should not know too much about the data source, I mean, whether it comes from the cache or comes from the database.

Caching in the controller does not support the DRY (Do not Repeat Yourself) approach, because you duplicate your code in several controllers and methods, thereby complicating their execution.

So, for me, this is the way I download in Laravel 5, not as much if using laravel 4:

//1. Create GalleryEloquentRepository.php in App / Repositories

 <?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. Create GalleryRepositoryInterface.php in App / Repositories

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

// 3. Create RepositoryServiceProvider.php in application / providers

 <?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. On the controller you can do it

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

Source: https://habr.com/ru/post/984291/


All Articles