简体   繁体   English

你会在 .NET Core web/api 应用程序中的什么地方实现缓存?

[英]Where would you implement caching in a .NET Core web/api application?

I have the following architecture:我有以下架构:

  • .NET Core 3.0 web application using razor pages for the UI .NET Core 3.0 Web 应用程序使用 razor 页面作为 UI
  • .NET Core 3.0 web api application serving all the data to the web app. .NET Core 3.0 web api 应用程序为 web 应用程序提供所有数据。 It talks to the database via Entity Framework它通过实体框架与数据库对话

I am not doing a lot of complicated operations on the data.我没有对数据做很多复杂的操作。 Most of the APIS are basic CRUD operations.大多数 APIS 都是基本的 CRUD 操作。 That said, I want to eliminate unnecessary trips to the database and for long term scalability I want to implement a caching layer in my application.也就是说,我想消除不必要的数据库访问,并且为了长期可扩展性,我想在我的应用程序中实现一个缓存层。

What I am debating is in which layer to implement the caching layer?我在争论的是在哪一层实现缓存层? Let's use an example of the app displaying a list of your upcoming appointments.让我们使用显示您即将到来的约会列表的应用程序示例。

Option 1 -> Do it in the web app.选项 1 -> 在 Web 应用程序中执行。 I could implement a factory or similar pattern where the web app no longer talks directly to the api.我可以实现一个工厂或类似的模式,其中 Web 应用程序不再直接与 api 对话。 It could ask the appointment factory for my appointments, and the factory would check cache first.它可以向预约工厂询问我的预约,工厂会先检查缓存。 If its not there, it would hit the API, and then cache the result.如果它不存在,它将命中 API,然后缓存结果。 However, unless I also made sure that any creations/updates also happened through the factory, the cache would not get updated if the data changed.但是,除非我还确保任何创建/更新也通过工厂发生,否则如果数据更改,缓存将不会更新。

Option 2 - Do it in the web api app.选项 2 - 在 web api 应用程序中执行此操作。 I could essentially treat the API as a factory.我基本上可以将 API 视为工厂。 Since I already know that all data manipulation will happen through the API, I can be assured that any time the data changes, I would know about it and could update the cache appropriately.由于我已经知道所有数据操作都将通过 API 进行,因此我可以放心,每当数据发生更改时,我都会知道它并可以适当地更新缓存。

I don't know if I love the idea of doing it at the web api layer.我不知道我是否喜欢在 web api 层做它的想法。 Part of me feels like the API should just do one job - put data into the database and get data out of the database.我觉得 API 应该只做一项工作 - 将数据放入数据库并从数据库中取出数据。 Maybe it shouldn't be making decisions on whether or not the calling app/user wants to use a cached value or not.也许它不应该决定调用应用程序/用户是否想要使用缓存值。

I would love to hear any insight into your real world experiences with one or the other - and if you had to do it over again, would you do anything different?我很想听听您对现实世界中的经历的任何见解——如果你不得不重来一次,你会做任何不同的事情吗? Are there long term benefits to one solution?一种解决方案是否有长期利益?

I would (and do) do this in the API for a number of reasons:出于多种原因,我会(并且会)在 API 中执行此操作:

  • It makes the API the central place where you ask for data.它使 API 成为您请求数据的中心位置。
    How it decides where that data comes from (cache, DB, another API, etc) is up to the API, and the UI doesn't have to care.它如何决定数据来自何处(缓存、数据库、另一个 API 等)取决于 API,而 UI 不必关心。

  • By making the API the "master", you can create other applications which use it, and the data is always consistent.通过使 API 成为“主”,您可以创建其他使用它的应用程序,并且数据始终保持一致。

  • At some point you have to send the data to the API anyway so that it is persisted.在某些时候,您无论如何都必须将数据发送到 API,以便它被持久化。 One or more equivalent caches of data at the front end are going to be wrong at some point or out of alignment.前端的一个或多个等效数据缓存在某些时候会出错或不对齐。 Send it to the API, and use what the API returns.将其发送到 API,并使用 API 返回的内容。 No thinking about it necessary.没有必要考虑。

  • If you need to purge the cached data for some reason, you have one place to do it, rather than on every client.如果出于某种原因需要清除缓存的数据,您可以在一个地方执行此操作,而不是在每个客户端上执行。

  • It keeps the presentation layers lighter.它使表示层更轻。

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

相关问题 在 .NET Core Web API 中缓存大数据 - Caching big data in .NET Core Web API 使用 Vue JS 和 .Net Core Web API 之类的堆栈 - 我在哪里实施身份验证(Azure AD)? - With a stack like Vue JS and .Net Core Web API - where do I implement auth (Azure AD)? 如何在ASP.NET Web API Core应用程序上使用QUARTZ实现调度程序? - How to implement a scheduler using QUARTZ on an ASP.NET Web API Core application? 尝试在 .net 核心 web api 中实现分页时出现 SqlException 错误 - SqlException error when trying to implement pagination in .net core web api 在 ASP.NET Core 5.0 Web API 中实现 DelegatingHandler? - Implement DelegatingHandler in ASP.NET Core 5.0 Web API? 如何在ASP.NET Core Web API中实现通过id获取? - How to implement get by id in ASP.NET Core Web API? ASP.NET中如何实现搜索过滤 Core Web API - How to implement search filter in ASP.NET Core Web API 如何在ASP Net Core中授权Web API控制器 - How do you Authorize a Web API Controller in ASP Net Core C# ASP.NET Core Web API 包含与 where - C# ASP.NET Core Web API include with where .Net Core Web Api 这个随机端口分配来自哪里? - .Net Core Web Api where is this random port assignment coming from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM