Beispiel #1
0
        internal static object InternalCallback(string key)
        {
            CacheEntry cacheEntry = CacheLockbox.GetCacheEntry(key);

            if (cacheEntry == null)
            {
                return(null);
            }
            CacheLoaderDelegate cacheLoader = cacheEntry.CacheLoader;

            if (cacheLoader == null)
            {
                return(null);
            }
            object obj2 = null;

            try
            {
                obj2 = cacheLoader();
            }
            catch (Exception exception)
            {
                if (_cacheLoaderErrorDelegate != null)
                {
                    try
                    {
                        _cacheLoaderErrorDelegate(key, exception);
                    }
                    catch
                    {
                    }
                }
            }
            if (obj2 != null)
            {
                Insert(key, obj2, null, (int)cacheEntry.RefreshInterval.TotalSeconds, CacheItemPriority.Normal, new CacheItemRemovedCallback(TCache.ItemRemovedCallback));
                CacheLockbox.UpdateCacheEntry(key, DateTime.Now);
            }
            return(obj2);
        }
Beispiel #2
0
        /// <summary>
        /// 获取缓存值重载
        /// </summary>
        /// <param name="key"></param>
        /// <param name="loader"></param>
        /// <returns></returns>
        public static object Get(string key, CacheLoaderDelegate loader)
        {
            CacheEntry entry = null;

            try
            {
                if (Contains(key))
                {
                    _rwLock.AcquireReaderLock(1000);
                    entry = Instance[key];
                }
                else
                {
                    entry = new CacheEntry();
                    if (loader != null)
                    {
                        _rwLock.AcquireWriterLock(500);
                        entry.Content = loader();
                        DateTime dtNow = DateTime.Now;
                        entry.CreateTime  = dtNow;
                        entry.LastUpdate  = dtNow;
                        entry.CacheLoader = loader;
                        _dicCache.Add(key, entry);
                    }
                }
                return(entry.Content);
            }
            finally
            {
                if (_rwLock.IsReaderLockHeld)
                {
                    _rwLock.ReleaseReaderLock();
                }
                if (_rwLock.IsWriterLockHeld)
                {
                    _rwLock.ReleaseWriterLock();
                }
            }
        }
Beispiel #3
0
        public static object Get(string key, CacheDependency dep, CacheLoaderDelegate cacheLoader)
        {
            object obj = _cache[key];

            if (obj == null)
            {
                if (cacheLoader != null)
                {
                    try
                    {
                        obj = cacheLoader();
                        Insert(key, obj, dep);
                    }
                    catch (Exception ex)
                    {
                        if (_cacheLoaderErrorDelegate != null)
                        {
                            _cacheLoaderErrorDelegate(key, ex);
                        }
                    }
                }
            }
            return(obj);
        }
Beispiel #4
0
        public static object Get(string key, TimeSpan absoluteExprition, CacheLoaderDelegate cacheLoader)
        {
            object obj = _cache[key];

            if (obj == null)
            {
                if (cacheLoader != null)
                {
                    try
                    {
                        obj = cacheLoader();
                        Insert(key, obj, absoluteExprition);
                    }
                    catch (Exception ex)
                    {
                        if (_cacheLoaderErrorDelegate != null)
                        {
                            _cacheLoaderErrorDelegate(key, ex);
                        }
                    }
                }
            }
            return(obj);
        }
Beispiel #5
0
 public static object GetCacheEntryLock(string key, TimeSpan refreshInterval, TimeSpan slidingExpiration, CacheLoaderDelegate loaderDelegate)
 {
     return(CacheLockbox.GetLock(key, refreshInterval, slidingExpiration, loaderDelegate));
 }
Beispiel #6
0
 public static object GetCacheEntryLock(string key, int refreshIntervalSeconds, int slidingExpirationSeconds, CacheLoaderDelegate loaderDelegate)
 {
     return(CacheLockbox.GetLock(key, TimeSpan.FromSeconds((double)(refreshIntervalSeconds * Factor)), TimeSpan.FromSeconds((double)(slidingExpirationSeconds * Factor)), loaderDelegate));
 }
Beispiel #7
0
 /// <summary>
 /// Returns a <see cref="CacheLoader{V} "/> which creates values by
 /// applying a <see cref="CacheLoaderDelegate{V} "/> using a key.
 /// </summary>
 /// <param name="loader">A <see cref="CacheLoaderDelegate{V} "/> that
 /// is used to compute the value from the key.</param>
 /// <returns>A <see cref="CacheLoader{V} "/> wich creates values by
 /// applying the <paramref name="loader"/> to the key.</returns>
 public static CacheLoader <T> From(CacheLoaderDelegate <T> loader)
 {
     return(new CacheLoaderDelegateToCacheLoader <T>(loader));
 }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="CacheLoader{T}.CacheLoaderDelegateToCacheLoader{T}"/>
 /// class by using the specified <see cref="CacheLoaderDelegate{T}"/>
 /// </summary>
 /// <param name="loader">The loader that is used to load instances of
 /// type <typeparamref name="T"/></param>
 public CacheLoaderDelegateToCacheLoader(
     CacheLoaderDelegate <T> loader)
 {
     loader_ = loader;
 }
Beispiel #9
0
 public static object GetCacheEntryLock(string key, TimeSpan refreshInterval, TimeSpan slidingExpiration, CacheLoaderDelegate loaderDelegate)
 {
     return CacheLockbox.GetLock(key, refreshInterval, slidingExpiration, loaderDelegate);
 }
Beispiel #10
0
 public static object GetCacheEntryLock(string key, int refreshIntervalSeconds, int slidingExpirationSeconds, CacheLoaderDelegate loaderDelegate)
 {
     return CacheLockbox.GetLock(key, TimeSpan.FromSeconds((double)(refreshIntervalSeconds * Factor)), TimeSpan.FromSeconds((double)(slidingExpirationSeconds * Factor)), loaderDelegate);
 }