Laravel缓存大量查询

时间:2021-08-01 23:49:33

I got an ecommerce site that has up to 1million products and the site is in many regions. Each product page has around 10-15 queries to DB, stuff like get product by url, get product category and get product format etc. Currenly im using laravel file-based caching and im not caching anything on product page.

我有一个电子商务网站,拥有多达100万种产品,该网站位于许多地区。每个产品页面都有大约10-15个DB查询,比如通过url获取产品,获取产品类别和获取产品格式等等。使用laravel基于文件的缓存并且我不在产品页面上缓存任何内容。

The problem is product pages are also targets for various crawlers/bots meaning a large number of queries made to DB. Also if I cache a product page say for 1hour its unlikely that will be hit in that time period since those bots target all of the pages. Also since I have different regions (different domains) meaning the number of pages is timed by count of regions (5-10).

问题是产品页面也是各种爬虫/机器人的目标,这意味着对DB进行了大量查询。此外,如果我缓存一个产品页面说1小时不太可能会在那段时间内被击中,因为这些机器人的目标是所有页面。此外,因为我有不同的区域(不同的域)意味着页面的数量由区域的数量(5-10)计时。

So I'm out of ideas how to cache this. I thought about caching all those queries forever (without time limit) and changing the cache when something about product changes. Is this the way to go? Does laravel create a cache file for each query that is being cached? If yes then the number of files could become a problem.

所以我没有想法如何缓存这个。我想到永远缓存所有这些查询(没有时间限制)并在产品更改时更改缓存。这是要走的路吗? laravel是否为每个正在缓存的查询创建缓存文件?如果是,则文件数可能会成为问题。

Also I thought about using redis. But I think i would run out of memory when caching that many keys? Any idea how should I go about caching this? Thanks in advance.

我还想过使用redis。但是我认为在缓存那么多密钥时我会耗尽内存?知道我应该如何缓存这个?提前致谢。

Edit1:

Thought about html caching with redis. So that I would cache for example product price and its html. Any ides how much memory do I need to cache lets say 10-20million keys with some html as their value?

考虑用redis进行html缓存。所以我会缓存例如产品价格及其html。任何想法我需要缓存多少内存让我们说一些html作为其价值的1000万至20亿个密钥?

2 个解决方案

#1


0  

There is a laravel package that caches queries.

有一个缓存查询的laravel包。

https://github.com/dwightwatson/rememberable

https://github.com/dwightwatson/rememberable

User::first()->remember(1440)->posts()->get();

#2


0  

Some caching ideas:

一些缓存的想法:

nginx cache the entire HTML page if people are not logged in.

如果人们没有登录,nginx会缓存整个HTML页面。

get 4 more VPS and put nothing but memcache on each one and memcache all your queries. Keep adding more servers until you have enough memory to cache all the querires.

再获得4个VPS,并且每个都只提供memcache,并记忆所有查询。继续添加更多服务器,直到有足够的内存来缓存所有查询。

get 4 more VPS and put mysql on each and use all available memory for query caching. Use sharding (not master slave) to route requests for certain products to certain machines. keep adding machines until all data is held in query caches

获得4个VPS并在每个上放置mysql并使用所有可用内存进行查询缓存。使用分片(非主从)将某些产品的请求路由到某些机器。继续添加计算机,直到所有数据都保存在查询缓存中

rewrite the system so that it doesn't require caching to function at acceptable speeds. Caching works best when it is used for greater throughput, not for increasing speed.

重写系统,使其不需要缓存以可接受的速度运行。当缓存用于更大的吞吐量而不是用于提高速度时,缓存效果最佳。

#1


0  

There is a laravel package that caches queries.

有一个缓存查询的laravel包。

https://github.com/dwightwatson/rememberable

https://github.com/dwightwatson/rememberable

User::first()->remember(1440)->posts()->get();

#2


0  

Some caching ideas:

一些缓存的想法:

nginx cache the entire HTML page if people are not logged in.

如果人们没有登录,nginx会缓存整个HTML页面。

get 4 more VPS and put nothing but memcache on each one and memcache all your queries. Keep adding more servers until you have enough memory to cache all the querires.

再获得4个VPS,并且每个都只提供memcache,并记忆所有查询。继续添加更多服务器,直到有足够的内存来缓存所有查询。

get 4 more VPS and put mysql on each and use all available memory for query caching. Use sharding (not master slave) to route requests for certain products to certain machines. keep adding machines until all data is held in query caches

获得4个VPS并在每个上放置mysql并使用所有可用内存进行查询缓存。使用分片(非主从)将某些产品的请求路由到某些机器。继续添加计算机,直到所有数据都保存在查询缓存中

rewrite the system so that it doesn't require caching to function at acceptable speeds. Caching works best when it is used for greater throughput, not for increasing speed.

重写系统,使其不需要缓存以可接受的速度运行。当缓存用于更大的吞吐量而不是用于提高速度时,缓存效果最佳。