EntlibCacheProvider.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » Providers » Caching » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » Providers » Caching » EntlibCacheProvider.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EverestCachingEverest.Library.Providers.Caching;

using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
namespace Everest.Providers.Caching{
    public class EntlibCacheProvider : EverestCaching.ICacheProvider
    {
        private ICacheManager cacheManager = null;
        public EntlibCacheProvider(ICacheManager cacheManager)
        {
            this.cacheManager = cacheManager;
        }
        #region ICacheProvider Members

        public void Add(string key, object value)
        {
            this.cacheManager.Add(key, value);
        }

        public void Add(string key, object value, EverestCaching.CacheItemPriority scavengingPriority, EverestCaching.ICacheItemRefreshAction refreshAction, params EverestCaching.ICacheItemExpiration[] expirations)
        {
            cacheManager.Add(key, value, ConvertCacheItemPriority(scavengingPriority), ConvertRefreshAction(refreshAction), ConvertExpirations(expirations));
        }

        public bool Contains(string key)
        {
            return cacheManager.Contains(key);
        }

        public int Count
        {
            get { return cacheManager.Count; }
        }

        public void Flush()
        {
            cacheManager.Flush();
        }

        public object Get(string key)
        {
            return cacheManager.GetData(key);
        }

        public void Remove(string key)
        {
            cacheManager.Remove(key);
        }

        public object this[string key]
        {
            get { return cacheManager[key]; }
        }

        #endregion
        /// <summary>
        /// Converts the cache item priority.
        /// </summary>
        /// <param name="priority">The priority.</param>
        /// <returns></returns>
        private CacheItemPriority ConvertCacheItemPriority(EverestCaching.CacheItemPriority priority)
        {
            switch (priority)
            {
                case Everest.Library.Providers.Caching.CacheItemPriority.None:
                    return CacheItemPriority.None;
                    break;
                case Everest.Library.Providers.Caching.CacheItemPriority.Low:
                    return CacheItemPriority.Low;
                    break;
                case Everest.Library.Providers.Caching.CacheItemPriority.Normal:
                    return CacheItemPriority.Normal;
                    break;
                case Everest.Library.Providers.Caching.CacheItemPriority.High:
                    return CacheItemPriority.High;
                    break;
                case Everest.Library.Providers.Caching.CacheItemPriority.NotRemovable:
                    return CacheItemPriority.NotRemovable;
                    break;
                default:
                    return CacheItemPriority.None;
                    break;
            }
        }
        /// <summary>
        /// Converts the refresh action.
        /// </summary>
        /// <param name="priority">The priority.</param>
        /// <returns></returns>
        private ICacheItemRefreshAction ConvertRefreshAction(EverestCaching.ICacheItemRefreshAction refreshAction)
        {
            if (refreshAction == null)
            {
                return null;
            }
            return new RefreshActionWrapper(refreshAction);
        }
        /// <summary>
        /// Converts the expirations.
        /// </summary>
        /// <param name="expirations">The expirations.</param>
        /// <returns></returns>
        private ICacheItemExpiration[] ConvertExpirations(EverestCaching.ICacheItemExpiration[] expirations)
        {
            ICacheItemExpiration[] cacheItemExpirations = new ICacheItemExpiration[expirations.Length];
            for (int i = 0; i < expirations.Length; i++)
            {
                cacheItemExpirations[i] = ConvertExpiration(expirations[i]);
            }
            return cacheItemExpirations;
        }
        /// <summary>
        /// Converts the expiration.
        /// </summary>
        /// <param name="expiration">The expiration.</param>
        /// <returns></returns>
        private ICacheItemExpiration ConvertExpiration(EverestCaching.ICacheItemExpiration expiration)
        {
            if (expiration is EverestCaching.AbsoluteTime)
            {
                return new AbsoluteTime(((EverestCaching.AbsoluteTime)expiration).ExpirationTime);
            }
            if (expiration is EverestCaching.FileDependency)
            {
                return new FileDependency(((EverestCaching.FileDependency)expiration).FileName);
            }
            if (expiration is EverestCaching.SlidingTime)
            {
                return new SlidingTime(((EverestCaching.SlidingTime)expiration).SlidingExpiration);
            }
            if (expiration is EverestCaching.NeverExpired)
            {
                return new NeverExpired();
            }
            if (expiration is EverestCaching.DirectoryDependency)
            {
                EverestCaching.DirectoryDependency dependency = (EverestCaching.DirectoryDependency)expiration;
                return new DirectoryDependency(dependency.DirectoryName);
            }
            return null;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    class RefreshActionWrapper : ICacheItemRefreshAction
    {
        private EverestCaching.ICacheItemRefreshAction refreshAction = null;
        /// <summary>
        /// Initializes a new instance of the <see cref="RefreshActionWrapper"/> class.
        /// </summary>
        /// <param name="refreshAction">The refresh action.</param>
        public RefreshActionWrapper(EverestCaching.ICacheItemRefreshAction refreshAction)
        {
            this.refreshAction = refreshAction;
        }
        #region ICacheItemRefreshAction Members

        /// <summary>
        /// Called when an item expires from the cache. This method can be used to notify an application that
        /// the expiration occured, cause the item to be refetched and refreshed from its original location, or
        /// perform any other application-specific action.
        /// </summary>
        /// <param name="removedKey">Key of item removed from cache. Will never be null.</param>
        /// <param name="expiredValue">Value from cache item that was just expired</param>
        /// <param name="removalReason">Reason the item was removed from the cache. See <see cref="T:Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemRemovedReason"/></param>
        /// <remarks>This method should catch and handle any exceptions thrown during its operation. No exceptions should leak
        /// out of it.</remarks>
        public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
        {
            this.refreshAction.Refresh(removedKey, expiredValue, ConvertRemovedReason(removalReason));
        }

        #endregion

        private EverestCaching.CacheItemRemovedReason ConvertRemovedReason(CacheItemRemovedReason removalReason)
        {
            switch (removalReason)
            {
                case CacheItemRemovedReason.Expired:
                    return EverestCaching.CacheItemRemovedReason.Expired;
                    break;
                case CacheItemRemovedReason.Removed:
                    return Everest.Library.Providers.Caching.CacheItemRemovedReason.Removed;
                    break;
                case CacheItemRemovedReason.Scavenged:
                    return Everest.Library.Providers.Caching.CacheItemRemovedReason.Scavenged;
                    break;
                case CacheItemRemovedReason.Unknown:
                    return Everest.Library.Providers.Caching.CacheItemRemovedReason.Unknown;
                    break;
                default:
                    return Everest.Library.Providers.Caching.CacheItemRemovedReason.Expired;
                    break;
            }
        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.