简体   繁体   中英

Laravel cart cache very slow

I am using cache to store our carts. As more carts are being added the system is becoming extremely slow when fetching a single cart.

I use the below code to access a single cart and feel it may be that there is lots of information stored under one key in the cache.

$carts = Cache::get("carts");

$cart = $carts[$this->reference];

I understand we could use unique keys such as carts:uniqueId but I need to be able to access all of the carts and loop through them so I feel this solution doesn't work.

I'd appreciate any advice on how I can solve this issue.

Edit: As per ka_lin comment, I need to loop through the carts and bookings so that I can check if the booking date is still available. If it is no longer available I need to clear the date selected and also emit an event to alert the user.

I would prefer to keep the data on the server side as this is built behind an api in order to make it easier for integration for partners.

If you need to save the carts on the server for some reason you could always use the database for that.

If you are storing them because the users should not lose items when leaving the page i would recommend using javascript and storing it with localStorage.

EDIT: It sounds like you are using the cache for something that should be handled by a database.

What happen behind the scene is: Cart (if you have used as Session based cart) in Laravel gets stored in file system and Cache without memcache/apc cache installed. Laravel use default cache as a filesystem too. So ultimately you doing same thing.

Personally i don't think that only using cart in many places of website slow down the whole website. There must be an other factor of website which slow down your website.

为了解决该问题,我创建了一个名为CacheKeys的表,在其中保存了唯一的购物车ID,然后可以使用它们循环浏览并获取所有购物车/预订。

The mindset with which you approached the problem is correct. The implementation, however, brings lots of pain.

Enter: Lada cache package

In such systems, what's truly slow is the mountain of sql queries which hit the database. Lada cache helps by automatically saving your query results in redis and invalidating them when these have been updated.

We've seen queries drop from 2s to 0.5s in production environments, an effective 4x gain.

RTFM: Read the manual CAREFULLY especially the Finally, all your models must include the Spiritix\\LadaCache\\Database\\LadaCacheTrait trait part and enjoy a hassle-free cache system.

NOTE: Your backend should be the only system handling your queries against your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM