Beispiel #1
0
        public object GetOrAdd(ICacheKey key, CacheItemValueGetter getter, ICacheDependency dependency, DateTime expirationDate, TimeSpan slidingSpan, RuntimeCommon.Caching.CacheItemPriority priority)
        {
            var value = Get(key);

            if (value != null)
            {
                return(value);
            }

            value = getter();
            Add(key, value, dependency, expirationDate, slidingSpan, priority);

            return(value);
        }
Beispiel #2
0
        public void Add(ICacheKey cacheKey, object value, ICacheDependency dependency, DateTime expirationDate, TimeSpan slidingSpan, RuntimeCommon.Caching.CacheItemPriority priority)
        {
            var policy = new CacheItemPolicy();

            { //configurations
                var depKeys = TransformDependency(dependency)?.ToList();
                if (!depKeys.IsNullOrEmpty())
                {
                    foreach (var key in depKeys)
                    {
                        subscription.SubscribeTopic(key);
                    }

                    var monitor = new PubSubMonitor(cacheKey.GetKeyAsString(), depKeys, filter);

                    policy.ChangeMonitors.Add(monitor);
                }

                policy.SlidingExpiration = slidingSpan == CacheUtils.NoSliding ?
                                           ObjectCache.NoSlidingExpiration :
                                           slidingSpan;

                policy.AbsoluteExpiration = expirationDate == CacheUtils.NoExpiration ?
                                            ObjectCache.InfiniteAbsoluteExpiration :
                                            expirationDate;

                policy.Priority = priority == RuntimeCommon.Caching.CacheItemPriority.NotRemovable ?
                                  System.Runtime.Caching.CacheItemPriority.NotRemovable :
                                  System.Runtime.Caching.CacheItemPriority.Default;
            }
            memoryCacheInstance.Set(cacheKey.GetKeyAsString(), value, policy);
        }