Exemple #1
0
    public T GetOrAdd(string key, Func <T> loadFunction, Func <CacheItemPolicy> getCacheItemPolicyFunction)
    {
        LazyLock <T> lazy;
        bool         success;

        synclock.EnterReadLock();
        try
        {
            success = this.TryGetValue(key, out lazy);
        }
        finally
        {
            synclock.ExitReadLock();
        }
        if (!success)
        {
            synclock.EnterWriteLock();
            try
            {
                if (!this.TryGetValue(key, out lazy))
                {
                    lazy = new LazyLock <T>();
                    var policy = getCacheItemPolicyFunction();
                    this.cache.Add(key, lazy, policy);
                }
            }
            finally
            {
                synclock.ExitWriteLock();
            }
        }
        return(lazy.Get(loadFunction));
    }
Exemple #2
0
 private bool TryGetValue(string key, out LazyLock <T> value)
 {
     value = (LazyLock <T>) this.cache.Get(key);
     if (value != null)
     {
         return(true);
     }
     return(false);
 }
 public void Add(string key, LazyLock item, ICacheDetails cacheDetails)
 {
     // NOTE: cacheDetails is normally used to set the timeout - you might
     // need to roll your own method for doing that.
     Context.Session[key] = item;
 }