簡體   English   中英

該緩存管理器如何存儲緩存的數據?

[英]How is this cache manager storing the cached data?

我正在使用這個緩存管理器,取自github上的bbarry

我不了解實際在哪里或如何存儲緩存的數據。 我見過的大多數緩存都使用MemoryCache ,但是我在任何地方都沒有提到它。

我只能認為ConcurrentDictionary正在存儲緩存的數據,但是如果是這樣,我不明白為什么將其設置為readonly

public static class Cache<T> {
    static readonly ConcurrentDictionary<string, T> Dictionary = new ConcurrentDictionary<string, T>();

    //by making this a tuple with the generic constraint, there will be one per cache type; the first parameter will always be the default value
    static readonly ConcurrentDictionary<string, Tuple<T, DateTime, TimeSpan>> Removals = new ConcurrentDictionary<string, Tuple<T, DateTime, TimeSpan>>();


    public static T GetOrAdd(string key, Func<string, T> creator) { return GetOrAdd(key, creator, null, null); }


    public static T GetOrAdd(string key, Func<string, T> creator, DateTime absoluteExpiration) { return GetOrAdd(key, creator, absoluteExpiration, null); }


    public static T GetOrAdd(string key, Func<string, T> creator, TimeSpan slidingExpiration) { return GetOrAdd(key, creator, null, slidingExpiration); }


    public static bool TryGetValue(string key, out T value) {
        Tuple<T, DateTime, TimeSpan> when;
        if (Removals.TryGetValue(key, out when) && when.Item3 != TimeSpan.Zero) {
            Remove(key, Tuple.Create(default(T), DateTime.Now.Add(when.Item3), when.Item3));
        }
        return Dictionary.TryGetValue(key, out value); 
    }


    public static bool Expire(string key, out T value) { return Dictionary.TryRemove(key, out value); }


    public static void Expire(string key) {
        T value;
        Dictionary.TryRemove(key, out value);
    }

    static T GetOrAdd(string key, Func<string, T> creator, DateTime? absoluteExpiration, TimeSpan? slidingExpiration) {
        if (key == null) {
            throw new ArgumentNullException("key");
        }
        Tuple<T, DateTime, TimeSpan> when;
        var updateRemoval = Removals.TryGetValue(key, out when) && when.Item3 != TimeSpan.Zero;
        var v = Dictionary.GetOrAdd(key, creator);
        if (absoluteExpiration == null && slidingExpiration == null && !updateRemoval) {
            return v;
        }
        if (absoluteExpiration != null || slidingExpiration != null) {
            var expiration = (TimeSpan)(slidingExpiration ?? (absoluteExpiration - DateTime.Now));
            when = Tuple.Create(default(T), DateTime.Now.Add(expiration), expiration);
        } else {
            when = Tuple.Create(default(T), DateTime.Now.Add(when.Item3), when.Item3);
        }
        if (absoluteExpiration != null) {
            Removals.TryAdd(key, Tuple.Create(default(T), (DateTime)absoluteExpiration, TimeSpan.Zero));
        } else {
            Removals.AddOrUpdate(key, when, (a, b) => when);
        }

        Remove(key, when);
        return v;
    }

    static void Remove(string key, Tuple<T, DateTime, TimeSpan> then) {
        System.Threading.Tasks.Task.Delay(then.Item3).ContinueWith(task => {
            Tuple<T, DateTime, TimeSpan> when;
            if (!Removals.TryGetValue(key, out when) || when.Item2 >= DateTime.Now)
                return;
            T v;
            Dictionary.TryRemove(key, out v);
            Removals.TryRemove(key, out when);
        });
    }
}

readonly修飾符並不意味着您不能修改標有它的集合(在這種情況下為ConcurrentDictionary<string, T> Dictionary )。

這僅意味着您無法重新分配其價值。 所以在這個例子中你不能

Dictionary = new ConcurrentDictionary<string, T>();

再次。 readonly文檔(上面有鏈接)列出了可以分配標記為readonly的字段的可能位置。 在構造函數或靜態構造函數或字段聲明中。

問題的答案是肯定的,在這種情況下,是ConcurrentDictionary存儲數據的。 GetOrAdd方法)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM