Ejemplo n.º 1
0
        /// <summary>
        /// 檢查快取位址是否有值,無則使用create查詢2
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="create"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>
        /// <returns></returns>
        public static TValue GetOrStore <TValue>(this System.Runtime.Caching.ObjectCache cache,
                                                 string key,
                                                 Func <string, TValue> create,
                                                 DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration)
        {
            var instance = cache.GetOrStoreContainer <TValue>(key, absoluteExpiration, slidingExpiration);

            if (instance.Value == null)
            {
                lock (instance)
                    if (instance.Value == null)
                    {
                        instance.Value = create(key);
                    }
            }

            return(instance.Value);
        }