[英]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:
例如:
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.