/// <summary>
 /// Adds an object into cache based on the parameters provided.
 /// </summary>
 /// <param name="tag">The tag.</param>
 /// <param name="name">The name.</param>
 /// <param name="itemPolicy">The itemPolicy object.</param>
 /// <param name="value">The value to store in cache.</param>
 /// <param name="dispatch">The dispatch.</param>
 /// <returns></returns>
 public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
 {
     if (itemPolicy == null)
         throw new ArgumentNullException("itemPolicy");
     var updateCallback = itemPolicy.UpdateCallback;
     if (updateCallback != null)
         updateCallback(name, value);
     //
     if (itemPolicy.SlidingExpiration != ServiceCache.NoSlidingExpiration)
         throw new ArgumentOutOfRangeException("itemPolicy.SlidingExpiration", "not supported.");
     if (itemPolicy.RemovedCallback != null)
         throw new ArgumentOutOfRangeException("itemPolicy.RemovedCallback", "not supported.");
     //
     var oldVersion = (tag as DataCacheItemVersion);
     var lockHandle = (tag as DataCacheLockHandle);
     var dataCacheTags = GetCacheDependency(tag, itemPolicy.Dependency, dispatch);
     var timeout = GetTimeout(itemPolicy.AbsoluteExpiration);
     string regionName;
     if (timeout == TimeSpan.Zero && dataCacheTags == null)
         if (oldVersion == null && lockHandle == null)
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value);
             else Cache.Put(name, value, regionName);
         }
         else if (oldVersion != null)
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, oldVersion);
             else Cache.Put(name, value, oldVersion, regionName);
         }
         else
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.PutAndUnlock(name, value, lockHandle);
             else Cache.PutAndUnlock(name, value, lockHandle, regionName);
         }
     else if (timeout != TimeSpan.Zero && dataCacheTags == null)
         if ((oldVersion == null) && (lockHandle == null))
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, timeout);
             else Cache.Put(name, value, timeout, regionName);
         }
         else if (oldVersion != null)
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, oldVersion, timeout);
             else Cache.Put(name, value, oldVersion, timeout, regionName);
         }
         else
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.PutAndUnlock(name, value, lockHandle, timeout);
             else Cache.PutAndUnlock(name, value, lockHandle, timeout, regionName);
         }
     else if (timeout == TimeSpan.Zero && dataCacheTags != null)
         if ((oldVersion == null) && (lockHandle == null))
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, dataCacheTags);
             else Cache.Put(name, value, dataCacheTags, regionName);
         }
         else if (oldVersion != null)
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, oldVersion, dataCacheTags);
             else Cache.Put(name, value, oldVersion, dataCacheTags, regionName);
         }
         else
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.PutAndUnlock(name, value, lockHandle, dataCacheTags);
             else Cache.PutAndUnlock(name, value, lockHandle, dataCacheTags, regionName);
         }
     else
         if (oldVersion == null && lockHandle == null)
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, timeout, dataCacheTags);
             else Cache.Put(name, value, timeout, dataCacheTags, regionName);
         }
         else if (oldVersion != null)
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.Put(name, value, oldVersion, timeout, dataCacheTags);
             else Cache.Put(name, value, oldVersion, timeout, dataCacheTags, regionName);
         }
         else
         {
             if (!Settings.TryGetRegion(ref name, out regionName)) Cache.PutAndUnlock(name, value, lockHandle, timeout, dataCacheTags);
             else Cache.PutAndUnlock(name, value, lockHandle, timeout, dataCacheTags, regionName);
         }
     //
     var registration = dispatch.Registration;
     if (registration != null && registration.UseHeaders)
     {
         var header = dispatch.Header;
         header.Item = name;
         Add(tag, name + "#", CacheItemPolicy.Default, header, ServiceCacheByDispatcher.Empty);
     }
     return value;
 }
 private IEnumerable<DataCacheTag> GetCacheDependency(object tag, CacheItemDependency dependency, ServiceCacheByDispatcher dispatch)
 {
     object value;
     if (dependency == null || (value = dependency(this, dispatch.Registration, tag, dispatch.Values)) == null)
         return null;
     //
     var names = (value as string[]);
     var touchable = Settings.Touchable;
     return ((touchable != null && names != null ? touchable.MakeDependency(tag, names) : value) as IEnumerable<DataCacheTag>);
 }
        /// <summary>
        /// Adds an object into cache based on the parameters provided.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="name">The name.</param>
        /// <param name="itemPolicy">The itemPolicy object.</param>
        /// <param name="value">The value to store in cache.</param>
        /// <param name="dispatch">The dispatch.</param>
        /// <returns></returns>
        public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
        {
            if (itemPolicy == null)
            {
                throw new ArgumentNullException("itemPolicy");
            }
            var updateCallback = itemPolicy.UpdateCallback;

            if (updateCallback != null)
            {
                updateCallback(name, value);
            }
            //
            var          absoluteExpiration = itemPolicy.AbsoluteExpiration;
            var          slidingExpiration  = itemPolicy.SlidingExpiration;
            ulong        cas;
            object       opvalue;
            StoreMode    storeMode;
            IncrementTag increment;
            DecrementTag decrement;

            if (absoluteExpiration == DateTime.MaxValue && slidingExpiration == TimeSpan.Zero)
            {
                switch (_tagMapper.ToSetOpcode(tag, ref name, out cas, out opvalue, out storeMode))
                {
                case TagMapper.SetOpcode.Prepend: value = Cache.Prepend(name, OpValueToArraySegment <byte>(opvalue)); break;

                case TagMapper.SetOpcode.PrependCas: value = Cache.Prepend(name, cas, OpValueToArraySegment <byte>(opvalue)); break;

                //
                case TagMapper.SetOpcode.Store: value = Cache.Store(storeMode, name, value); break;

                case TagMapper.SetOpcode.Cas: value = Cache.Cas(storeMode, name, value, cas); break;

                case TagMapper.SetOpcode.Decrement: decrement = OpValueToDecrementTag(opvalue); value = Cache.Decrement(name, decrement.DefaultValue, decrement.Delta); break;

                case TagMapper.SetOpcode.DecrementCas: decrement = OpValueToDecrementTag(opvalue); value = Cache.Decrement(name, decrement.DefaultValue, decrement.Delta, cas); break;

                case TagMapper.SetOpcode.Increment: increment = OpValueToIncrementTag(opvalue); value = Cache.Increment(name, increment.DefaultValue, increment.Delta); break;

                case TagMapper.SetOpcode.IncrementCas: increment = OpValueToIncrementTag(opvalue); value = Cache.Increment(name, increment.DefaultValue, increment.Delta, cas); break;

                default: throw new InvalidOperationException();
                }
            }
            else if (absoluteExpiration != DateTime.MinValue && slidingExpiration == TimeSpan.Zero)
            {
                switch (_tagMapper.ToSetOpcode(tag, ref name, out cas, out opvalue, out storeMode))
                {
                case TagMapper.SetOpcode.Prepend:
                case TagMapper.SetOpcode.PrependCas:
                    throw new NotSupportedException("Operation not supported with absoluteExpiration && slidingExpiration");

                case TagMapper.SetOpcode.Store: value = Cache.Store(storeMode, name, value, absoluteExpiration); break;

                case TagMapper.SetOpcode.Cas: value = Cache.Cas(storeMode, name, value, absoluteExpiration, cas); break;

                case TagMapper.SetOpcode.Decrement: decrement = OpValueToDecrementTag(opvalue); value = Cache.Decrement(name, decrement.DefaultValue, decrement.Delta, absoluteExpiration); break;

                case TagMapper.SetOpcode.DecrementCas: decrement = OpValueToDecrementTag(opvalue); value = Cache.Decrement(name, decrement.DefaultValue, decrement.Delta, absoluteExpiration, cas); break;

                case TagMapper.SetOpcode.Increment: increment = OpValueToIncrementTag(opvalue); value = Cache.Increment(name, increment.DefaultValue, increment.Delta, absoluteExpiration); break;

                case TagMapper.SetOpcode.IncrementCas: increment = OpValueToIncrementTag(opvalue); value = Cache.Increment(name, increment.DefaultValue, increment.Delta, absoluteExpiration, cas); break;

                default: throw new InvalidOperationException();
                }
            }
            else if (absoluteExpiration == DateTime.MinValue && slidingExpiration != TimeSpan.Zero)
            {
                switch (_tagMapper.ToSetOpcode(tag, ref name, out cas, out opvalue, out storeMode))
                {
                case TagMapper.SetOpcode.Prepend:
                case TagMapper.SetOpcode.PrependCas:
                    throw new NotSupportedException("Operation not supported with absoluteExpiration && slidingExpiration");

                case TagMapper.SetOpcode.Store: value = Cache.Store(storeMode, name, value, slidingExpiration); break;

                case TagMapper.SetOpcode.Cas: value = Cache.Cas(storeMode, name, value, slidingExpiration, cas); break;

                case TagMapper.SetOpcode.Decrement: decrement = OpValueToDecrementTag(opvalue); value = Cache.Decrement(name, decrement.DefaultValue, decrement.Delta, slidingExpiration); break;

                case TagMapper.SetOpcode.DecrementCas: decrement = OpValueToDecrementTag(opvalue); value = Cache.Decrement(name, decrement.DefaultValue, decrement.Delta, slidingExpiration, cas); break;

                case TagMapper.SetOpcode.Increment: increment = OpValueToIncrementTag(opvalue); value = Cache.Increment(name, increment.DefaultValue, increment.Delta, slidingExpiration); break;

                case TagMapper.SetOpcode.IncrementCas: increment = OpValueToIncrementTag(opvalue); value = Cache.Increment(name, increment.DefaultValue, increment.Delta, slidingExpiration, cas); break;

                default: throw new InvalidOperationException();
                }
            }
            else
            {
                throw new InvalidOperationException("absoluteExpiration && slidingExpiration");
            }
            //
            var registration = dispatch.Registration;

            if (registration != null && registration.UseHeaders)
            {
                var header = dispatch.Header;
                header.Item = name;
                Add(tag, name + "#", CacheItemPolicy.Default, header, ServiceCacheByDispatcher.Empty);
            }
            return(value);
        }
Example #4
0
 /// <summary>
 /// Adds an object into cache based on the parameters provided.
 /// </summary>
 /// <param name="tag">The tag.</param>
 /// <param name="name">The name.</param>
 /// <param name="itemPolicy">The itemPolicy object.</param>
 /// <param name="value">The value to store in cache.</param>
 /// <param name="dispatch">The dispatch.</param>
 /// <returns></returns>
 public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
 {
     if (itemPolicy == null)
         throw new ArgumentNullException("itemPolicy");
     var updateCallback = itemPolicy.UpdateCallback;
     if (updateCallback != null)
         updateCallback(name, value);
     // item priority
     WebCacheItemPriority cacheItemPriority;
     switch (itemPolicy.Priority)
     {
         case CacheItemPriority.AboveNormal: cacheItemPriority = WebCacheItemPriority.AboveNormal; break;
         case CacheItemPriority.BelowNormal: cacheItemPriority = WebCacheItemPriority.BelowNormal; break;
         case CacheItemPriority.High: cacheItemPriority = WebCacheItemPriority.High; break;
         case CacheItemPriority.Low: cacheItemPriority = WebCacheItemPriority.Low; break;
         case CacheItemPriority.Normal: cacheItemPriority = WebCacheItemPriority.Normal; break;
         case CacheItemPriority.NotRemovable: cacheItemPriority = WebCacheItemPriority.NotRemovable; break;
         default: cacheItemPriority = WebCacheItemPriority.Default; break;
     }
     //
     var removedCallback = (itemPolicy.RemovedCallback == null ? null : new WebCacheItemRemovedCallback((n, v, c) => { itemPolicy.RemovedCallback(n, v); }));
     Cache.Insert(name, value, GetCacheDependency(tag, itemPolicy.Dependency, dispatch), itemPolicy.AbsoluteExpiration, itemPolicy.SlidingExpiration, cacheItemPriority, removedCallback);
     var registration = dispatch.Registration;
     if (registration != null && registration.UseHeaders)
     {
         var headerPolicy = new WebCacheDependency(null, new[] { name });
         var header = dispatch.Header;
         header.Item = name;
         Cache.Insert(name + "#", header, headerPolicy, itemPolicy.AbsoluteExpiration, itemPolicy.SlidingExpiration, cacheItemPriority, null);
     }
     return value;
 }
Example #5
0
        /// <summary>
        /// Adds an object into cache based on the parameters provided.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="name">The name.</param>
        /// <param name="itemPolicy">The itemPolicy object.</param>
        /// <param name="value">The value to store in cache.</param>
        /// <param name="dispatch">The dispatch.</param>
        /// <returns></returns>
        public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
        {
            if (itemPolicy == null)
            {
                throw new ArgumentNullException("itemPolicy");
            }
            var updateCallback = itemPolicy.UpdateCallback;

            if (updateCallback != null)
            {
                updateCallback(name, value);
            }
            //
            if (itemPolicy.SlidingExpiration != ServiceCache.NoSlidingExpiration)
            {
                throw new ArgumentOutOfRangeException("itemPolicy.SlidingExpiration", "not supported.");
            }
            if (itemPolicy.RemovedCallback != null)
            {
                throw new ArgumentOutOfRangeException("itemPolicy.RemovedCallback", "not supported.");
            }
            //
            var    oldVersion    = (tag as DataCacheItemVersion);
            var    lockHandle    = (tag as DataCacheLockHandle);
            var    dataCacheTags = GetCacheDependency(tag, itemPolicy.Dependency, dispatch);
            var    timeout       = GetTimeout(itemPolicy.AbsoluteExpiration);
            string regionName;

            if (timeout == TimeSpan.Zero && dataCacheTags == null)
            {
                if (oldVersion == null && lockHandle == null)
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.Put(name, value);
                    }
                    else
                    {
                        Cache.Put(name, value, regionName);
                    }
                }
                else if (oldVersion != null)
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.Put(name, value, oldVersion);
                    }
                    else
                    {
                        Cache.Put(name, value, oldVersion, regionName);
                    }
                }
                else
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.PutAndUnlock(name, value, lockHandle);
                    }
                    else
                    {
                        Cache.PutAndUnlock(name, value, lockHandle, regionName);
                    }
                }
            }
            else if (timeout != TimeSpan.Zero && dataCacheTags == null)
            {
                if ((oldVersion == null) && (lockHandle == null))
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.Put(name, value, timeout);
                    }
                    else
                    {
                        Cache.Put(name, value, timeout, regionName);
                    }
                }
                else if (oldVersion != null)
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.Put(name, value, oldVersion, timeout);
                    }
                    else
                    {
                        Cache.Put(name, value, oldVersion, timeout, regionName);
                    }
                }
                else
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.PutAndUnlock(name, value, lockHandle, timeout);
                    }
                    else
                    {
                        Cache.PutAndUnlock(name, value, lockHandle, timeout, regionName);
                    }
                }
            }
            else if (timeout == TimeSpan.Zero && dataCacheTags != null)
            {
                if ((oldVersion == null) && (lockHandle == null))
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.Put(name, value, dataCacheTags);
                    }
                    else
                    {
                        Cache.Put(name, value, dataCacheTags, regionName);
                    }
                }
                else if (oldVersion != null)
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.Put(name, value, oldVersion, dataCacheTags);
                    }
                    else
                    {
                        Cache.Put(name, value, oldVersion, dataCacheTags, regionName);
                    }
                }
                else
                {
                    if (!Settings.TryGetRegion(ref name, out regionName))
                    {
                        Cache.PutAndUnlock(name, value, lockHandle, dataCacheTags);
                    }
                    else
                    {
                        Cache.PutAndUnlock(name, value, lockHandle, dataCacheTags, regionName);
                    }
                }
            }
            else
            if (oldVersion == null && lockHandle == null)
            {
                if (!Settings.TryGetRegion(ref name, out regionName))
                {
                    Cache.Put(name, value, timeout, dataCacheTags);
                }
                else
                {
                    Cache.Put(name, value, timeout, dataCacheTags, regionName);
                }
            }
            else if (oldVersion != null)
            {
                if (!Settings.TryGetRegion(ref name, out regionName))
                {
                    Cache.Put(name, value, oldVersion, timeout, dataCacheTags);
                }
                else
                {
                    Cache.Put(name, value, oldVersion, timeout, dataCacheTags, regionName);
                }
            }
            else
            {
                if (!Settings.TryGetRegion(ref name, out regionName))
                {
                    Cache.PutAndUnlock(name, value, lockHandle, timeout, dataCacheTags);
                }
                else
                {
                    Cache.PutAndUnlock(name, value, lockHandle, timeout, dataCacheTags, regionName);
                }
            }
            //
            var registration = dispatch.Registration;

            if (registration != null && registration.UseHeaders)
            {
                var header = dispatch.Header;
                header.Item = name;
                Add(tag, name + "#", CacheItemPolicy.Default, header, ServiceCacheByDispatcher.Empty);
            }
            return(value);
        }
Example #6
0
        private IEnumerable <DataCacheTag> GetCacheDependency(object tag, CacheItemDependency dependency, ServiceCacheByDispatcher dispatch)
        {
            object value;

            if (dependency == null || (value = dependency(this, dispatch.Registration, tag, dispatch.Values)) == null)
            {
                return(null);
            }
            //
            var names     = (value as string[]);
            var touchable = Settings.Touchable;

            return((touchable != null && names != null ? touchable.MakeDependency(tag, names) : value) as IEnumerable <DataCacheTag>);
        }
 /// <summary>
 /// Adds an object into cache based on the parameters provided.
 /// </summary>
 /// <param name="tag">The tag.</param>
 /// <param name="name">The name.</param>
 /// <param name="itemPolicy">The itemPolicy object.</param>
 /// <param name="value">The value to store in cache.</param>
 /// <param name="dispatch">The dispatch.</param>
 /// <returns></returns>
 public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
 {
     if (itemPolicy == null)
         throw new ArgumentNullException("itemPolicy");
     var updateCallback = itemPolicy.UpdateCallback;
     if (updateCallback != null)
         updateCallback(name, value);
     //
     throw new NotImplementedException();
 }
Example #8
0
        /// <summary>
        /// Adds an object into cache based on the parameters provided.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="name">The name.</param>
        /// <param name="itemPolicy">The itemPolicy object.</param>
        /// <param name="value">The value to store in cache.</param>
        /// <param name="dispatch">The dispatch.</param>
        /// <returns></returns>
        public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
        {
            if (itemPolicy == null)
            {
                throw new ArgumentNullException("itemPolicy");
            }
            var updateCallback = itemPolicy.UpdateCallback;

            if (updateCallback != null)
            {
                updateCallback(name, value);
            }
            // item priority
            WebCacheItemPriority cacheItemPriority;

            switch (itemPolicy.Priority)
            {
            case CacheItemPriority.AboveNormal: cacheItemPriority = WebCacheItemPriority.AboveNormal; break;

            case CacheItemPriority.BelowNormal: cacheItemPriority = WebCacheItemPriority.BelowNormal; break;

            case CacheItemPriority.High: cacheItemPriority = WebCacheItemPriority.High; break;

            case CacheItemPriority.Low: cacheItemPriority = WebCacheItemPriority.Low; break;

            case CacheItemPriority.Normal: cacheItemPriority = WebCacheItemPriority.Normal; break;

            case CacheItemPriority.NotRemovable: cacheItemPriority = WebCacheItemPriority.NotRemovable; break;

            default: cacheItemPriority = WebCacheItemPriority.Default; break;
            }
            //
            var removedCallback = (itemPolicy.RemovedCallback == null ? null : new WebCacheItemRemovedCallback((n, v, c) => { itemPolicy.RemovedCallback(n, v); }));

            Cache.Insert(name, value, GetCacheDependency(tag, itemPolicy.Dependency, dispatch), itemPolicy.AbsoluteExpiration, itemPolicy.SlidingExpiration, cacheItemPriority, removedCallback);
            var registration = dispatch.Registration;

            if (registration != null && registration.UseHeaders)
            {
                var headerPolicy = new WebCacheDependency(null, new[] { name });
                var header       = dispatch.Header;
                header.Item = name;
                Cache.Insert(name + "#", header, headerPolicy, itemPolicy.AbsoluteExpiration, itemPolicy.SlidingExpiration, cacheItemPriority, null);
            }
            return(value);
        }
 /// <summary>
 /// Adds the specified tag.
 /// </summary>
 /// <param name="tag">The tag.</param>
 /// <param name="name">The name.</param>
 /// <param name="itemPolicy">The item policy.</param>
 /// <param name="value">The value.</param>
 /// <param name="dispatch">The dispatch.</param>
 /// <returns></returns>
 public object Add(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
 {
     if (itemPolicy == null)
         throw new ArgumentNullException("itemPolicy");
     var updateCallback = itemPolicy.UpdateCallback;
     if (updateCallback != null)
         updateCallback(name, value);
     //
     if (itemPolicy.SlidingExpiration != ServiceCache.NoSlidingExpiration)
         throw new ArgumentOutOfRangeException("itemPolicy.SlidingExpiration", "not supported.");
     if (itemPolicy.RemovedCallback != null)
         throw new ArgumentOutOfRangeException("itemPolicy.RemovedCallback", "not supported.");
     //
     throw new NotImplementedException();
 }
Example #10
0
        /// <summary>
        /// Adds an object into cache based on the parameters provided.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="name">The name used to identify the item in cache.</param>
        /// <param name="itemPolicy">The itemPolicy defining caching policies.</param>
        /// <param name="value">The value to store in cache.</param>
        /// <param name="dispatch"></param>
        /// <returns></returns>
        public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
        {
            _cache[name] = value;
            var registration = dispatch.Registration;

            if (registration != null && registration.UseHeaders)
            {
                var header = dispatch.Header;
                header.Item        = name;
                _cache[name + "#"] = header;
            }
            return(value);
        }
Example #11
0
        /// <summary>
        /// Adds an object into cache based on the parameters provided.
        /// </summary>
        /// <param name="tag">Not used</param>
        /// <param name="name">The key used to identify the item in cache.</param>
        /// <param name="itemPolicy">Not used</param>
        /// <param name="value">The value to store in cache.</param>
        /// <param name="dispatch">Not used</param>
        /// <returns>Last value that what in cache.</returns>
        public object Add(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
        {
            // TODO: Throw on dependency or other stuff not supported by this simple system
            object lastValue;

            if (!_cache.TryGetValue(name, out lastValue))
            {
                _cache[name] = value;
                var registration = dispatch.Registration;
                if (registration != null && registration.UseHeaders)
                {
                    var header = dispatch.Header;
                    header.Item        = name;
                    _cache[name + "#"] = header;
                }
                return(null);
            }
            return(lastValue);
        }
        private SystemCaching.CacheItemPolicy GetCacheDependency(object tag, CacheItemPolicy itemPolicy, ServiceCacheByDispatcher dispatch)
        {
            // item priority
            SystemCaching.CacheItemPriority itemPriority;
            switch (itemPolicy.Priority)
            {
            case CacheItemPriority.NotRemovable:
                itemPriority = SystemCaching.CacheItemPriority.NotRemovable;
                break;

            default:
                itemPriority = SystemCaching.CacheItemPriority.Default;
                break;
            }
            var removedCallback = (itemPolicy.RemovedCallback != null ? new SystemCaching.CacheEntryRemovedCallback(x => { itemPolicy.RemovedCallback(x.CacheItem.Key, x.CacheItem.Value); }) : null);
            var updateCallback  = (itemPolicy.UpdateCallback != null ? new SystemCaching.CacheEntryUpdateCallback(x => { itemPolicy.UpdateCallback(x.UpdatedCacheItem.Key, x.UpdatedCacheItem.Value); }) : null);
            var newItemPolicy   = new SystemCaching.CacheItemPolicy
            {
                AbsoluteExpiration = itemPolicy.AbsoluteExpiration,
                Priority           = itemPriority,
                RemovedCallback    = removedCallback,
                SlidingExpiration  = itemPolicy.SlidingExpiration,
                UpdateCallback     = updateCallback,
            };
            var changeMonitors = GetCacheDependency(tag, itemPolicy.Dependency, dispatch);

            if (changeMonitors != null)
            {
                var list = newItemPolicy.ChangeMonitors;
                foreach (var changeMonitor in changeMonitors)
                {
                    list.Add(changeMonitor);
                }
            }
            return(newItemPolicy);
        }
        /// <summary>
        /// Adds an object into cache based on the parameters provided.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="name">The name.</param>
        /// <param name="itemPolicy">The itemPolicy object.</param>
        /// <param name="value">The value to store in cache.</param>
        /// <param name="dispatch">The dispatch.</param>
        /// <returns></returns>
        public object Set(object tag, string name, CacheItemPolicy itemPolicy, object value, ServiceCacheByDispatcher dispatch)
        {
            if (itemPolicy == null)
            {
                throw new ArgumentNullException("itemPolicy");
            }
            var updateCallback = itemPolicy.UpdateCallback;

            if (updateCallback != null)
            {
                updateCallback(name, value);
            }
            string regionName;

            Settings.TryGetRegion(ref name, out regionName);
            //
            Cache.Set(name, value, GetCacheDependency(tag, itemPolicy, dispatch), regionName);
            var registration = dispatch.Registration;

            if (registration != null && registration.UseHeaders)
            {
                var headerPolicy = new SystemCaching.CacheItemPolicy();
                headerPolicy.ChangeMonitors.Add(Cache.CreateCacheEntryChangeMonitor(new[] { name }, regionName));
                var header = dispatch.Header;
                header.Item = name;
                Cache.Set(name + "#", header, headerPolicy, regionName);
            }
            return(value);
        }