小编use*_*627的帖子

如何在c#中实现通用缓存管理器

我正在尝试实现通用缓存管理器,但是我不确定如何进行锁定.

到目前为止我有以下内容,但是如果我有两个具有相同返回类型的缓存条目,那么我猜测将使用相同的锁对象!

public class CacheManager : ICacheManager
{
    static class TypeLock<T>
    {
        public static readonly object SyncLock = new object();
    }
    private readonly ICache _cache;
    public CacheManager(ICache cache)
    {
        if (cache == null)
            throw new ArgumentNullException("cache");

        _cache = cache;
    }

    public TResult AddCache<TResult>(string cacheKey, Func<TResult> acquire, int cacheDurationInMinutes) where TResult : class
    {
        return AddCache(cacheKey, null, acquire, cacheDurationInMinutes);
    }

    public TResult AddCache<TResult>(string cacheKey, CacheDependency dependency, Func<TResult> acquire, int cacheDurationInMinutes) where TResult : class
    {
        var entry = acquire.Invoke();
        if (entry …
Run Code Online (Sandbox Code Playgroud)

c# generics locking

11
推荐指数
1
解决办法
9670
查看次数

标签 统计

c# ×1

generics ×1

locking ×1