Friday 27 March 2015

Re: [dcphp-dev] Laravel Cache

Shameless references:

Using caches - http://www.phparch.com/magazine/2014-2/june/
Using Laravel route filters - http://www.phparch.com/magazine/2015-2/january/


On 03/26/2015 07:37 PM, Ray wrote:
PHP Colleagues:

This question relates to best practices for using Laravel Cache.

The central objective is to reduce the number of accesses to the database for all the usual
performance-related reasons.

Are there any documented best practices for the application design?  It seems obvious to me
that since Cache:: is a one-line statement, it's easy to drop this into the controllers --
either return the cached data or call the model and cache the results. And invalidate the
cache (maybe with an eager reload) when requests update the model.

Here's a first look at doing 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've heard the suggestion that you could use Laravel Route Filters to cache the responses,
but I can't quite get my head around the idea.

Thoughts?  References?  Examples?

Thanks to all,
Ray


--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

0 comments:

Post a Comment