简体   繁体   English

保持内存中的更改与后端同步的好方法是什么

[英]What is a good way to keep an in-memory change in sync with a backend

What is a good way to keep changes to a resource in sync with both in-memory and persistent storage in a multi-user situation?在多用户情况下,保持资源更改与内存和持久存储同步的好方法是什么? Here's two ideas, but they're both clumsy这是两个想法,但它们都很笨拙

Candidate A候选人A

Here, a copy of r must be retrieved so that in-memory state and persistent state is updated at the same time on put .在这里,必须检索r的副本,以便在put时同时更新内存中状态和持久状态。

Resource r = resources.getDeepCopyOfResourceById("foo");
r.removeValue("bar");
resources.put(r);

Candidate B候选人乙

Here, update operations are located on the service level.在这里,更新操作位于服务级别。 This appear to me as an object-oriented anti-pattern.这在我看来是一种面向对象的反模式。

Resource r = resources.getResourceById("foo");
resources.removeValue("foo", r);

Is there a better way to design this?有没有更好的方法来设计这个?

What is a good way to keep changes to a resource in sync with both in-memory and persistent storage in a multi-user situation?在多用户情况下,保持资源更改与内存和持久存储同步的好方法是什么?

Generally speaking you have two problems here: Caching and concurrency.一般来说,这里有两个问题:缓存和并发。

For caching you need to decide what strategy to use.对于缓存,您需要决定使用什么策略。 There are plenty of options.有很多选择。 For example:例如:

  • read through / write through读/写
  • write around到处写
  • write behind写在后面
  • cache aside缓存放在一边

When you've decided the caching pattern the next step is to make the cache and the persistent data access thread safe.确定缓存模式后,下一步是使缓存和持久数据访问线程安全。 This is usually achieved with transactions and locks.这通常通过事务和锁来实现。 The exact implementation depends on which libraries you use.确切的实现取决于您使用的库。

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

相关问题 Android上ParseObject的ArrayList的良好内存大小 - Good in-memory size of ArrayList of ParseObject on android 什么是支持范围查询的良好的Java友好内存映射? - What's a good java-friendly in-memory map supporting range queries? 有没有办法将巨大的Java变量保留在内存中以节省加载时间? - Is there a way to keep huge Java variables in-memory to save up loading time? 将序列化和压缩的对象保留在内存中 - Keep serialized and compressed Objects in-memory 使用 Java(Spring 框架)和内存数据库测试在 DAO 类中找到的 CRUD 方法的正确方法是什么? - What is the correct way of testing CRUD methods that are found in DAO classes using Java(Spring framework) and In-memory DB? 有没有办法在内存中公开 ActiveMQ 端口 - Is there a way to expose ActiveMQ port when it is in-memory 什么是 Java 中的内存持久性 - What's in-memory persistence in Java 什么是可视化内存中变量的好方法? (可视调试器) - What would be a good way to visualize variables in memory? (A visual debugger) 有没有办法使两个功能保持同步? - Is there a way to keep two functions in sync? 在哪里保持用户之间共享的内存数据 - Where to keep in-memory data shared between users
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM