Cache and conditionals in Blade, the WORST Laravel combination

I show you an anecdote about a development in which I use cache and conditionals in blade.

I wanted to tell you here something very curious that happened to me between the use of the cache and the conditionals, that when you say it like that, the problem seems quite obvious but even so you can take it as a small challenge that was happening to me before in the application here of free development. Let me explain a little bit of the context here. I have a controller to show the details of a page, using a cache system that I activate taking care that it is in production:

 if (config('app')['env'] == 'production') {
           //*** flujo normal, busca en cache
           $keyCache = 'post_show_' .  $category_url_clean . '_' . $post_url_clean;
           if (Cache::has($keyCache)) {
               return Cache::get($keyCache);
           }
       }

From the master template, I show and hide certain content depending on whether we are in PC or mobile mode:

@if (!isMobile())
 Contenido no movil
@endif

When you do these conditionals and when you work with the cache is where we have the problem, since the cache that will be registered is the one that depends on the client’s device, if the client enters through a mobile device and there is no cache, then a cache will be generated for the mobile mode and that one will remain established forever, if ANOTHER client enters the application from a PC to consume the same page that the user consumed through the mobile device, it will return the cache, but the cache will be the one generated by the mobile device… Which is clearly a huge problem because this is not the correct format.

One cache per mode

To solve this, it is as easy as also asking for the mode and creating a specific cache for each mode:

$type = isMobile() ? 'm' : 'd';
 if (config('app')['env'] == 'production') {
           //*** flujo normal, busca en cache
           $keyCache = 'post_show_' .  $category_url_clean . '_' . $post_url_clean . '_' . $type;
           if (Cache::has($keyCache)) {
               return Cache::get($keyCache);
           }
       }

This is how it works correctly, which is how I have it here in production, so nothing. That was a little anecdote since it took me a while to realize this and well, if this happens to someone else, you have to be very careful with the cache because it can play these bad tricks on you like it happened to me, so nothing. See you in another video.

Video Transcript

I wanted to tell you something and you can also take this as a small challenge that was happening to me before in the free development application here. I'll explain the context a little bit here. I have the show one that is to show all this that is over here, all the content that is obviously several boxes, several components, view fragments and so on. I have a conditional here to ask what mode it is in, for example, if here I look for the Master mode, for example when it is in mobile mode as it is a smaller screen here for example I don't put the one to search for posts and another advertising message and other things here with an important script is to notice that there are fewer things between mobile mode and PC mode, in PC mode there is more HTML content specified by some conditionals. If you have followed the course you already know how to do all this, the translation, which is what we saw here, and also the part of The cache, so I don't know if you have already realized and I am already giving you enough detail of a possible problem that can occur precisely when you do these conditionals and when you work with the cache, I am going to tell you a little about the situation or more than tell you in case you have not seen it yet, we are going to continue pressing This is what I do here in the controller:


 if (config('app')['env'] == 'production') {
           //*** flujo normal, busca en cache
           $keyCache = 'post_show_' .  $category_url_clean . '_' . $post_url_clean . '_';
           if (Cache::has($keyCache)) {
               return Cache::get($keyCache);
           }
       }

Here first check the environment so as I am here in development it does not matter what I do we are not expanding the cache and obviously that problem would not happen but I am going to put all this in production for a moment here I am going to put Production Sorry this one here I put Production come back here This is the local one there you can see the URL here now I am in PC version perfect and here I am in mobile as you saw it recognizes it as mobile and reload And notice that it still says PC version why do you think it is happening, This is something like an anecdote more than anything and it is precisely because here we are always returning the same view notice, that the cache is generated only once it does not care about the conditionals that we have here since this is internal to us and therefore the first time that view is consulted either from PC mode or from a mobile device it will generate that cache that cached view again depending on what was requested and that is the one that I am going to use in my case until the end of time because I only delete it manually and it does not have one expiration date as you can see here that again it is a cache as who says without a time limit and that was precisely the problem of why this happens that again, notice that here so that you inform yourself a little better Here it appears PC version therefore it is in PC mode which is not exactly this notice that for example this according to the names of the components that I gave you should not be there and you can see it here in the well in production here you can see that it does not appear but here it does appear and it is because we are in PC mode which I do not want because in production I wanted to Disable some components with which the user is not going to interact because the screen is very small remove the advertising so that it loads a little faster and is not bothering him Because Google Adsense seems to be giving strong to advertising and those aspects to improve here a little the performance in mobile mode.

The problem is precisely this blessed condition that we have here in which we always have a cache for everyone. How do we solve this? Well, it's as easy as asking here for the mode and thus we vary the cache:

$type = isMobile() ? 'm' : 'd';
 if (config('app')['env'] == 'production') {
           //*** flujo normal, busca en cache
           $keyCache = 'post_show_' .  $category_url_clean . '_' . $post_url_clean . '_' . $type;
           if (Cache::has($keyCache)) {
               return Cache::get($keyCache);
           }
       }

Remember that this is here, that is to say here is the name of the cache which is then the one we establish, then Well now as who says I have the work in duplicate since two caches are saved for the same page in different formats, one mobile and the other as It would be good, I don't even know why I put d because it was mobile and PC there would now be two modes this way it works correctly, which is how I have it here in production, so nothing That was a little anecdote since it took me a while to realize this in case this happens to someone else, you have to be very careful with the cache because it can play these bad tricks on you like it happened to me, so nothing See you in another video.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.