
There is no doubt that one of the ways to improve the performance of any website or web application is to use the cache as much as possible.
Cache is a very good technique to improve the performance of websites and web applications, but what are the types of cache? and can we cache all website data?
Caching Tips
- There are two types of cache (browser cache) and (server cache).
- Browser caching means storing resources such as images, stylesheets, javascript files, html files in browser local storage.
- Browser caching works by utilizing particular HTTP headers that tell the browser how to cache data. These HTTP headers can be defined in .htaccess or nginx config files.
- From these headers ETag, Cache-Control, Expires, Last Modified.
Example .htaccess to all files with the below extensions:
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|php|txt|xsl)$"> Header set Cache-Control "max-age=31536050" </FilesMatch>
- Server caching on the other hand is the most effective over browser caching. It means caching content that comes from db, caching files and views, and we can cache source code.
- Server caching we have more control over the size the data to be cached and how long the data is cached.
- In server caching data is stored and managed in server. For example in laravel framework data is cached in files by default located in storage/framework/cache/ directory. There are other places to store cache like database, memcached, redis, dynamodb.
- Server caching is more secure than browser caching as the browser caching can be compromised.
- In laravel caching made easy using the Cache facade which provides many methods like Cache::remember(), Cache::set(), Cache::get(), Cache::increment(), Cache::decrement(), etc.
Example:
$value = Cache::remember('latest_products', Carbon::now()->addWeek(), function () { return DB::table('products')->orderBy('id', 'DESC')->limit(9)->get(); });
Now let’s talk about which data to cache.
When developing a website and when thinking about using cache, there are one important point you need to consider is that not all data you have to cache, or in other means there are specific content need to be cached and the other not.
I will pick an example of a periodically changing website data such as an e-commerce website. What is the scenario to follow.
- Typically you should cache the homepage content say for example the homepage displays the 9 latest products, so you can cache those products for about one hour.
- When the cache expires the products will automatically refreshed. In laravel you can use Cache::remember() function for this.
- If the website have a search page. In this page there is no need to use cache because the user should see every added product at this moment.
- Also you should not cache the display product details page.
- You can cache specific Api responses such as a login Api to speed up the response login time. But be careful to clear the cache on other places like when the user updates his profile.
public function login(Request $request) { if (! $user = auth()->attempt($request->all())) { $response = Cache::remember('user_' . $user->id, Carbon::now()->addWeek(), function () { // fetch any user details }); return response()->json(['user' => $response], 200); } return response()->json(['error' => 'Unauthorized'], 401); }
Cache Removal
In typical web applications there are scenarios when you have to clear cache when specific actions occur so that the user can see updated data, some of these:
- When a user updates a specific item like in an e-commerce app after updating a product.
- Removing user cache when updating user profile.
- Also you can clear app cache after some time automatically using a background process like cron jobs.