A typical request on an dynamic PHPÂ site can doÂ a lot of things.Â It's highly likely that a bunch database queries are performed. Â On complex pages executing thoseÂ queries and hydrating them can slow a site down.
The response time can be improved by caching the entire response. The idea is that whenÂ a user visits a certain page theÂ app stores the rendered page. When a second request to the page is made, the app shouldn't bother with rendering the page from scratch but just serve the saved response.
I've made a Laravel package named "laravel-responsecache" that does just that. Installing it is very easy: just add the service provider and facade to the app's configuration. And step two is... there is no step two. In most cases you're done. Â All successful responses (that is a response with a statuscode in the 200 or 300 range)Â to a GET-requests will now beÂ cached for a week. If the response of a specificÂ route or controller should never be cached middleware can be addedÂ that prevents caching. FurthermoreÂ each logged in user will have have it's own separate cache.Â Cached responses can be stored in any configured repositoryÂ in Laravel. You could easily share a cache between servers Â by usingÂ memcached.
I think that behaviour will suit a lot of use cases. If you need some other caching behaviour (eg. cacheÂ error responses, exempting redirects, using a common cache for users with the same role, Â changing the expiration time of the cache) you can easily write a custom caching profile.
The package isn't supposed to sweep performance troubles under the rug. All appsÂ should be optimized so that they'll respond in an acceptable timeframe without using response caching. My rule of thumb is that typical pages in a cms should be able to render within a secondÂ (and preferably much less). Anything above thatÂ is unacceptable. ThatÂ number isÂ by no means scientific. Make up your own mind what an acceptable responsetimeÂ should be. Of course all of this depends on the type of site and the amount of visitors it has to handle. Also keep in mind that that there are a lot of other aspects that need to be consideredÂ when trying to deliver a speedy experience.
There are some great alternativesÂ to cache responses. Two well known solutions areÂ VarnishÂ andÂ Nginx caching. They take response caching one step further by not even invoking php when serving a cached request. Both optionsÂ are very robust and can work on any scale. Â The benefits the Laravel packageÂ has over Varnish-like solutions is that itÂ is easier to set up and thatÂ application logic can be used to determine what needs to be cached.
If you're interested in speeding up your Laravel app using the package, go take a look at it on GitHub: