/*
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;
}
}
}
}
|