简体   繁体   English

当一个元素受特定用户情况影响时,我如何从缓存中受益

[英]How can I benefit from cache when one element is effected by specific user situation

We have a page that lists products for an e-commerce platform, which updates every 30 minutes and we cache it for 30 minutes as it gets a lot of views.我们有一个列出电子商务平台产品的页面,该页面每 30 分钟更新一次,我们将其缓存 30 分钟,因为它获得了很多浏览量。

However there is a "buy" button on it, and if a user has already bought it needs to change from "buy" to "bought" and therefore suddenly makes the caching complex.但是上面有一个“购买”按钮,如果用户已经购买,则需要从“购买”更改为“已购买”,因此突然使缓存变得复杂。

What is the right approach to this, where we benefit from cache but also make sure the buy/bought button per product per user is accurate?什么是正确的方法,我们从缓存中受益,但也确保每个用户每个产品的购买/购买按钮是准确的?

Anyone have experience?任何人有经验吗?

If I understand your posting correctly, what you want to do is to cache as much of the page's data as possible without caching specifically the "user has purchased this item or not" part.如果我正确理解您的帖子,您要做的是尽可能多地缓存页面数据,而不是专门缓存“用户是否购买了此商品”部分。

In short:简而言之:

The solution here is to separate the cacheable data from the not-cacheable data.这里的解决方案是将可缓存数据与不可缓存数据分开。 Then, on page load, do the following:然后,在页面加载时,执行以下操作:

  1. Read the cacheable data from cache从缓存中读取可缓存的数据
  2. Read the non-cacheable data from wherever it is stored.从存储的任何地方读取不可缓存的数据。
  3. Combine them together into the web page to display to the end user将它们组合成 web 页面显示给最终用户

I think this is a very common way of optimizing performance for high traffic web pages or mobile app screens (at least we do it quite a bit at my company).我认为这是优化高流量 web 页面或移动应用程序屏幕性能的一种非常常见的方法(至少我们在我的公司做了很多)。 The benefit here is that you are improving page load times by caching as much as possible - while still making sure the business logic functions as required (in your case, making the "buy/bought" button work properly).这里的好处是您可以通过尽可能多地缓存来缩短页面加载时间 - 同时仍确保业务逻辑按要求运行(在您的情况下,使“购买/购买”按钮正常工作)。

Now, about how you could actually do this in practice:现在,关于如何在实践中实际做到这一点:

The first step is for you to understand your data and how it needs to be cached.第一步是了解您的数据以及需要如何缓存数据。 Some data may not be cacheable at all, and some may have specific caching requirements (for example, your logic may require that the cache for certain data will be invalidated for different reasons than other data) which means it must be cached separately from other data.有些数据可能根本无法缓存,有些可能有特定的缓存要求(例如,您的逻辑可能要求某些数据的缓存因与其他数据不同的原因而失效)这意味着它必须与其他数据分开缓存. It sounds like you have already done this.听起来你已经这样做了。 As you've described, your data falls into the following two categories:正如您所描述的,您的数据分为以下两类:

  • Not-cachable: User item purchase status for each item on the page不可缓存:页面上每个项目的用户项目购买状态
  • Cacheable (30 minutes): everything else on the page缓存(30 分钟):页面上的所有其他内容

Now that you understand your data, what you need to do is set up your application to cache (or not cache) it according to the rules you've decided.现在您了解了您的数据,您需要做的是设置您的应用程序以根据您决定的规则缓存(或不缓存)它。 And then to load everything and combine it together on page loads.然后加载所有内容并在页面加载时将它们组合在一起。

There are a couple ways to do caching & then page loading depending on where you want to cache the cacheable data.有几种方法可以进行缓存然后加载页面,具体取决于您要缓存可缓存数据的位置。

If you want to cache on client side - You can cache the cacheable part of the page in a browser (or native app) cache.如果您想在客户端缓存- 您可以在浏览器(或本机应用程序)缓存中缓存页面的可缓存部分。 Then, on page load, you can fetch the user-purchase data by having the page's Javascript (or native code) make a request(s) to the backend server.然后,在页面加载时,您可以通过让页面的 Javascript(或本机代码)向后端服务器发出请求来获取用户购买数据。 The request (or multiple requests depending on how you decide to do this) can fetch the user item purchase status for each item found in the cached part of the page data.该请求(或多个请求,具体取决于您决定如何执行此操作)可以获取在页面数据的缓存部分中找到的每个项目的用户项目购买状态。

If you want to cache on server side - In this case, you can cache the cacheable part of the page data in Redis/Memcached/your application's memory/etc.如果您想在服务器端缓存- 在这种情况下,您可以将页面数据的可缓存部分缓存在 Redis/Memcached/您的应用程序内存等中。 - you have a lot of options for specific technologies. - 你有很多特定技术的选择。 Then, when a page is requested, your backend application can load the cached page data from wherever, then fetch the user item purchase status for each item found in the cached page data, and finally combine everything into the data sent to the client to display the page.然后,当一个页面被请求时,你的后端应用程序可以从任何地方加载缓存的页面数据,然后获取缓存页面数据中找到的每个项目的用户项目购买状态,最后将所有内容组合成数据发送给客户端显示这页纸。

In our company's case, we do a mixture of both of these (and by the way, for us the client-side is generally native mobile rather than web - so we have a bit more flexibility on client side caching).在我们公司的案例中,我们混合使用了这两种方法(顺便说一下,对于我们来说,客户端通常是本地移动设备而不是 web - 因此我们在客户端缓存方面有更多的灵活性)。

Finally, as a side note:最后,作为旁注:

I'm guessing in your e-commerce platform a user can never "un-buy" something?我猜在您的电子商务平台中,用户永远无法“取消购买”某些东西? In this case you may be able to cache that a user "did buy" something even if you can't cache that a user "did not buy" something.在这种情况下,即使您无法缓存用户“未购买”某物,您也可以缓存用户“确实购买”了某物。 This is a good example of where using both client-side and server-side caches can be useful.这是同时使用客户端和服务器端缓存的一个很好的例子。 You can cache most of the page on client-side, but cache this specific information on server-side to speed up the response of the "check if user bought these items" requests made by the client-side code at page load time.您可以在客户端缓存大部分页面,但在服务器端缓存此特定信息,以加快客户端代码在页面加载时发出的“检查用户是否购买了这些项目”请求的响应。

However, since this is getting to be a lot of caching, for this I would personally suggest to consider the complexity/maintenance drawbacks here vs the performance benefit of this caching (which you would want to performance test to see how much it really helps).但是,由于缓存越来越多,为此我个人建议考虑这里的复杂性/维护缺点与此缓存的性能优势(您希望进行性能测试以查看它真正有多大帮助) .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种情况下如何创建缓存映射? - How can I create cache map in this situation? Django + Redis:如何仅使一个特定元素的缓存无效 - Django + Redis: How to invalidate cache for just one specific element 如何清除特定的流式缓存? - how can I clear specific streamlit cache? Scons-如何检测何时从缓存中拉出文件? - Scons - How Can I Detect When a File is Pulled from Cache? 如何缓存所有查看用户的请求,因此,如果再次发出相同的请求,则会从缓存中获取该请求 - How can I cache all view user requests so If the same request is made again it is fetched from cache 启用页面缓存后,如何停止特定用户控件的缓存? - How can I stop a specific user control from caching when page caching is enabled? Android-ImageLoader =>当我从服务器调用具有相同名称的图像时,imageLoader从缓存中获取较旧的图像。 我该如何解决? - Android-ImageLoader => When I call image with the same name from server, imageLoader takes older one from Cache. How can I fix it? 如何从CQ5调度程序缓存中排除从特定模板创建的页面? - How can I exclude pages created from a specific template from the CQ5 dispatcher cache? Android-如何使用okhttp-retrofit从已保存的缓存中使特定的URL无效/删除。 - Android - How can I invalidate/remove specific URL s from saved cache using okhttp-retrofit.? 如何仅从一个特定查询重置缓存 - How to reset cache from only one specific query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM