[英]Where would you implement caching in a .NET Core web/api application?
I have the following architecture:我有以下架构:
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.如果出于某种原因需要清除缓存的数据,您可以在一个地方执行此操作,而不是在每个客户端上执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.