How to configure automatic clearCache in Strapi Cache Middlware with Strapi v3? - caching

I have a problem related with the cache middleware in strapi v3. I have two models that are not related each other but when loading the first one I am calling the second one. So my problem is:
I have the clearRelatedCache attribute enable so when something change from one of each model, it refresh the cache. Everything works fine if we call each route individually. The problem is with the first model (settings) retrieving an array from the second-one (events). If I call settings and something is changed from events it does not refresh the cache of settings
My question is, is there any magic attribute that related settings with events only in the configuration cache so every time I change something from the events the cache from settings refresh itself?
Things that I tried: I know it can work relating each other on Strapi but I cannot do that. I know it also works configuring its afterXXX method on the model but I want do find a elegant solution.

Related

Skip SSR in NextJS when I already have the data cached and not stale in the client

I'm using NextJS with TanStack Query (formerly ReactQuery). TanStack Query acts as a cache between my NextJS app and the data stored in the backend.
I was previously doing SSR only, but I'm complementing it with TanStack Query for optimistic updates. I previously needed to fetch data on getServerSideProps for every "detail" page, but now I'm thinking that I could skip some of those fetches since I already have the data in the cache and it's still fresh.
For example. Let's say we have a TODO app. When I visit /todo/id_1 for the first time, it's nice to have SSR to send the page already rendered to the client. If I go somewhere else, and come back to /todo/id_1, I know for a fact that the contents of that TODO hasn't changed, but I still need to go through SSR.
Would there be a way to skip SSR in that case?
I was hoping I could to something like the following:
<Link href={`/todo/${id}`} skipSsr={cachedTodo[id].notStale} />
NextJs will always call getServerSideProps if the url changes. As mentioned in the comments, shallow routing does not work if the url actually changes.
I think there are a couple of ways around this:
set a cache-control: max-age on the response with the time of caching that you want. That way, the request will be made, but it will not hit your server, but come from the browser cache instead. As an advantage, those fetches will also succeed while you're offline.
instruct next to not make the query if the request comes from a client transition. There is an open discussion about this:
Add option to disable getServerSideProps on client-side navigation
shallow routing doesn't really solve it, so it seems you have to make the request and then check if it comes from SSR or not. This comment has the best workaround I guess ?
use incremental static site regeneration. It basically makes your page static, but it revalidates after a certain time if a request comes in.

How to implement dashboard settings in Laravel project

I want to implement admin panel settings(coloring, theme, font, logo...) in Laravel project.
I have used limitless bootstrap admin panel for my project, laravel blade, and no front end framework such as vue.js or anything else.
The solution that I know, is that make a settings table which save colors and other settings.
But the problem is putting those data into css file, frequently fetching those data ....
My Question
Is there any better way to implement this?
If it is correct, how to handle the problems I mentioned?
To persist the data across devices or multiple sessions you would need to store them as database values. I will suggest you keep these values in a JSON data column in your SQL database mapped against a particular user id or so.
When a user logs in to the application check if he has a record for custom settings in the DB. If so, pull in that record (JSON data) and store it in the current session. Further in your HTML files use values from your session store to alter styling or logo or things you wish to change. This way you will limit DB query to the very first login request only.
An additional thing to note is you should update the session store values if in case the user updates these settings anything during his active session.
Note: Haven't implemented the same, just a wild guess that sounds ideal to me.

Symfony 1.4 Cache Dashboard for specific user

Not sure if anyone is still using the legacy 1.4 - but I love it!
Background:
I have a user dashboard available at app.com/home/dashboard
To optimize DB hits, I cached the template Since the URL doesn't have a user parameter, a user ended up seeing another user's data
To beat this, I wrote a filter to include the user-id in the URL, giving each user their own URL like app.com/home/18/dashboard
While this prevents the data leak and provides the benefit of caching, it is messing reporting in GA, since I cannot track the total visits to the Dashboard (directly)
Has anyone worked around this?
I've had the same problem. To solve it I've decided to split the whole page into partials and components and cache those instead.
To make partials/component cached per-user just pass 'user_id' => $sf_user->getId() along with parameters. That will make user_id value used as part of cache entry key.

Loading a lot of model data into client on page load

I have a question about proper or recommended way of loading a lot of data from database. I have Laravel 5.4 project with Vue.js 2.0. I need display employees table from database on a page. On client Vue component is used for requesting this data with promise and build grid with vue-tables-2.
The problem in case that i can't find proper way for logic. There is already 50 000+ records and will be more. So using Employees::all() is really bad idea. Data is requested with axios from api url. And there is no possibility of using reddis or memcached. Looks like i need some kind of lazy loading post request from client and maybe with Laravel pagination. I will request first part of data and make next request to paginator with next post... and will have spam of requests.
If i will use default caching mechanism i don't understand how to build logic of caching, how to detect that model was changed and cache require rebuilding.
Maybe there is a way to organize lazy loading of data with dynamically add it into table and if user start search or filter before loading finished make request to server for direct database search. But again in this case possibly i will have a lot of database requests.
So question is - maybe there is a recommended way for organization stuff like this ?

Cakephp Session error on live site

I am having issues with using the session component in cakephp.
They worked fine throughout the site, when on a dev site on a different server.
After uploading the final version on the new server, it seems that the session will not start in certain controllers. Only on two controllers is this happening, but nothing seems to be done differently on these controllers - except possibly the fact that only these two controllers are using a custom file uploader component. Have tried disabling this and nothing changed.
The website is an ecommerce system, with the problems mainly occuring when trying to add an item to the basket, which is stored in a session. this session is remaining blank, whereas on the dev site worked a charm.
I am also using the auth component to login/logout, and although these 2 areas are not required to be logged in to function, they cause it to switch to logged out whenever they are loaded - and this then switches back by itself when any other areas of the site are accessed. The auth and session components are loaded in the appcontroller.
Thanks!
I've had a similar problem before now, and for me I was under the same impression as #starlocke that something needed kick starting. Instead of using Cake's Session component I used the session_start() command, after checking that $_SESSION was not defined. For example:
if(!isset($_SESSION)) session_start();
This was placed inside the beforeFilter() method of the AppController class.
I then also had a problem where it was saying it couldn't start the session because of there already being output to the server, this was solved for me by deleting any closing ?> tags inside controllers, as these aren't required by php and stop any unneeded output being sent to the browser like in this case.
Further reading on php closing tags:
Why I don’t use closing PHP tags
PHP closing tag
My personal experiences have shown that CakePHP's sessions are independent of Apache+PHP sessions. With that in mind, it helps to take some action to always guarantee that a CakePHP Session (instead of the usual Apache+PHP session) is instantiated early in your website's workflow , for example, adding something like $this->Session->read('foo'); inside your site's AppController's beforeFilter(). It shouldn't matter what key you try to read, and 'foo' should be enough to start your CakePHP sessions.

Resources