为这个问题的轻微投机性质道歉。
我正在使用内存中的静态对象编写 Cms 来保存系统的当前状态,并使用 Event Sourcing 创建一个事件日志,我可以使用它来重建对象状态,并提供回滚等(请参阅http://martinfowler.com/articles/lmax.html如果你不知道我在说什么)
public class Cms
{
private static object WriteLock = new object();
public static Cms Read { get; set; }
static Cms Write { get; set; }
static Cms()
{
Read = RebuildFromActionLog();
Write = RebuildFromActionLog();
}
public static void Update(Action action)
{
lock (WriteLock)
{
try
{
action.Apply(Write);
}
catch(Exception ex)
{
Write = RebuildFromActionLog(); //ditch the potentially messed up Write model
throw;
}
LogAction(action); //the action was a keeper, so keep it
Read = Write; //ditch the current read only model - it will continue to be used by any requests that have grabbed it
Write = RebuildFromActionLog(); //get a new model ready for the next write
}
}
...
}
除了在执行或重建需要很长时间时可能会堆积大量写入以及可能使用大量内存之外,这是否存在任何错误,尤其是与并发相关的错误?