gpt4 book ai didi

c# - 使用昂贵的构建缓存对象并允许更新

转载 作者:行者123 更新时间:2023-11-30 17:57:21 25 4
gpt4 key购买 nike

我正在为 MVC 网络应用程序开发缓存管理器。对于这个应用程序,我有一些构建成本很高的非常大的对象。在应用程序生命周期中,我可能需要根据用户请求创建多个此类对象。构建时,用户将使用对象中的数据,从而导致许多读取操作。有时,我需要更新缓存对象中的一些次要数据点(创建和替换会花费太多时间)。

下面是我创建的一个缓存管理器类来帮助我解决这个问题。除了基本的线程安全之外,我的目标是:

  1. 允许对一个对象进行多次读取,但将所有对该对象的读取锁定在更新请求
  2. 确保对象只被创建 1 次,如果它尚不存在(请记住,它的构建时间很长行动)。
  3. 允许缓存存储很多对象,并保持一个锁每个对象(而不是所有对象的一把锁)。

    public class CacheManager 
    {
    private static readonly ObjectCache Cache = MemoryCache.Default;
    private static readonly ConcurrentDictionary<string, ReaderWriterLockSlim>
    Locks = new ConcurrentDictionary<string, ReaderWriterLockSlim>();
    private const int CacheLengthInHours = 1;

    public object AddOrGetExisting(string key, Func<object> factoryMethod)
    {
    Locks.GetOrAdd(key, new ReaderWriterLockSlim());

    var policy = new CacheItemPolicy
    {
    AbsoluteExpiration = DateTimeOffset.Now.AddHours(CacheLengthInHours)
    };
    return Cache.AddOrGetExisting
    (key, new Lazy<object>(factoryMethod), policy);
    }

    public object Get(string key)
    {
    var targetLock = AcquireLockObject(key);
    if (targetLock != null)
    {
    targetLock.EnterReadLock();

    try
    {
    var cacheItem = Cache.GetCacheItem(key);
    if(cacheItem!= null)
    return cacheItem.Value;
    }
    finally
    {
    targetLock.ExitReadLock();
    }
    }

    return null;
    }

    public void Update<T>(string key, Func<T, object> updateMethod)
    {
    var targetLock = AcquireLockObject(key);
    var targetItem = (Lazy<object>) Get(key);

    if (targetLock == null || key == null) return;
    targetLock.EnterWriteLock();

    try
    {
    updateMethod((T)targetItem.Value);
    }
    finally
    {
    targetLock.ExitWriteLock();
    }
    }

    private ReaderWriterLockSlim AcquireLockObject(string key)
    {
    return Locks.ContainsKey(key) ? Locks[key] : null;
    }
    }

我是否在保持线程安全的同时实现了我的目标?你们都看到了实现我的目标的更好方法吗?

谢谢!

更新:所以这里的底线是我真的试图在 1 个区域做太多。出于某种原因,我确信在管理缓存的同一个类中管理 Get/Update 操作是个好主意。在查看 Groo 的解决方案并重新思考该问题后,我能够进行大量重构,从而消除了我面临的这个问题。

最佳答案

好吧,我不认为这个类可以满足您的需求。

Allow multiple reads against the object, but lock all reads upon an update request

您可以将所有读取锁定到缓存管理器,但您不会将读取(或更新)锁定到实际的缓存实例。

Ensure that the object is only ever created 1 time if it does not already exist (keep in mind that its a long build action).

我不认为你能确保这一点。在将对象添加到字典时您没有锁定任何东西(此外,您添加了一个惰性构造函数,因此您甚至不知道对象何时被实例化)。

编辑: 这部分成立,我唯一要更改的是制作 Get返回 Lazy<object> .在编写我的程序时,我忘记转换它并调用 ToString在返回值上返回`“未创建值”。

Allow the cache to store many objects, and maintain a lock per object (rather than one lock for all objects).

这与第 1 点相同:您锁定的是字典,而不是对对象的访问。还有你的update delegate 有一个奇怪的签名(它接受类型化的通用参数,并返回一个从未使用过的 object)。这意味着您实际上是在修改对象的属性,并且这些更改对于程序中持有该对象引用的任何部分都是立即可见的。

如何解决这个问题

如果您的对象是可变的(我假设它是),则无法确保事务的一致性,除非您的每个属性在每次读取访问时也都获得了锁。简化这一点的一种方法是使其不可变(这就是为什么这些在多线程中如此受欢迎)。

或者,您可以考虑将这个大对象分解成更小的部分并分别缓存每个部分,并在需要时使它们不可变。

[编辑] 添加了竞争条件示例:

class Program
{
static void Main(string[] args)
{
CacheManager cache = new CacheManager();
cache.AddOrGetExisting("item", () => new Test());

// let one thread modify the item
ThreadPool.QueueUserWorkItem(s =>
{
Thread.Sleep(250);
cache.Update<Test>("item", i =>
{
i.First = "CHANGED";
Thread.Sleep(500);
i.Second = "CHANGED";

return i;
});
});

// let one thread just read the item and print it
ThreadPool.QueueUserWorkItem(s =>
{
var item = ((Lazy<object>)cache.Get("item")).Value;
Log(item.ToString());
Thread.Sleep(500);
Log(item.ToString());
});

Console.Read();
}

class Test
{
private string _first = "Initial value";
public string First
{
get { return _first; }
set { _first = value; Log("First", value); }
}

private string _second = "Initial value";
public string Second
{
get { return _second; }
set { _second = value; Log("Second", value); }
}

public override string ToString()
{
return string.Format("--> PRINTING: First: [{0}], Second: [{1}]", First, Second);
}
}

private static void Log(string message)
{
Console.WriteLine("Thread {0}: {1}", Thread.CurrentThread.ManagedThreadId, message);
}

private static void Log(string property, string value)
{
Console.WriteLine("Thread {0}: {1} property was changed to [{2}]", Thread.CurrentThread.ManagedThreadId, property, value);
}
}

应该会发生这样的事情:

t = 0ms  : thread A gets the item and prints the initial value
t = 250ms: thread B modifies the first property
t = 500ms: thread A prints the INCONSISTENT value (only the first prop. changed)
t = 750ms: thread B modifies the second property

关于c# - 使用昂贵的构建缓存对象并允许更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com