예제 #1
0
        /// <summary>
        /// Get an existing value based on the return type, inserting it if it does not exist, and updating the cache if it does.
        /// Use this method for a singleton cache instance where there will only exist a single cached item at a time of this type
        /// </summary>
        /// <typeparam name="TValue">The return type of the stored value</typeparam>
        /// <param name="valueFactory">The zero-parameter function to retrieve the value fresh if it does not currently exist</param>
        /// <param name="evictionLengthHours">The length of time (in hours) until this item will be considered stale.  If null, will never expire</param>
        /// <returns>The existing or newly created value that exists for this type</returns>
        public static TValue AddOrUpdate <TValue>(Func <TValue> valueFactory, double?evictionLengthHours) where TValue : class
        {
            var    policy = BuildCacheItemPolicy(evictionLengthHours);
            object value  = _store.AddOrUpdateSafe(valueFactory, oldValue => valueFactory(), policy);

            return((TValue)value);
        }