Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Web/Caching
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv5195
Modified Files:
AspNetCache.cs
Log Message:
SPRNET-683, SPRNET-712
Index: AspNetCache.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Web/Caching/AspNetCache.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** AspNetCache.cs 3 Aug 2007 06:08:59 -0000 1.3
--- AspNetCache.cs 24 Aug 2007 22:44:16 -0000 1.4
***************
*** 38,41 ****
--- 38,154 ----
public class AspNetCache : AbstractCache
{
+ #region Internal Abstractions
+
+ /// <summary>
+ /// Abstracts the underlying runtime cache
+ /// </summary>
+ public interface IRuntimeCache : IEnumerable
+ {
+ /// <summary>
+ /// Insert an item into the cache.
+ /// </summary>
+ void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration,
+ TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
+
+ /// <summary>
+ /// Removes an item from the cache.
+ /// </summary>
+ /// <param name="key">The key of the item to remove</param>
+ /// <returns>The object that has been removed from the cache</returns>
+ object Remove(string key);
+
+ /// <summary>
+ /// Retrieve an item with the specified key from the cache.
+ /// </summary>
+ /// <param name="key">The key of the item to be retrieved</param>
+ /// <returns>The item, if found. <value>null</value> otherwise</returns>
+ object Get(string key);
+ }
+
+ /// <summary>
+ /// Actually delegates all calls to the underlying <see cref="HttpRuntime.Cache"/>
+ /// </summary>
+ private class RuntimeCache : IRuntimeCache
+ {
+ private readonly Cache _runtimeCache = HttpRuntime.Cache;
+
+ public object Remove(string key)
+ {
+ return _runtimeCache.Remove(key);
+ }
+
+ public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback )
+ {
+ _runtimeCache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
+ }
+
+ public object Get(string key)
+ {
+ return _runtimeCache.Get(key);
+ }
+
+ public IEnumerator GetEnumerator()
+ {
+ return _runtimeCache.GetEnumerator();
+ }
+ }
+
+ #endregion
+
+ #region Fields
+
+ // the concrete cache implementation
+ private readonly IRuntimeCache _cache;
+ // the (unique!) name of this particular cache instance.
+ private readonly string _cacheName;
+
+ // hold default values for this cache instance.
+ private CacheItemPriority _priority = CacheItemPriority.Default;
+ private bool _slidingExpiration = false;
+
+ #endregion
+
+ /// <summary>
+ /// Initializes a new instance of <see cref="AspNetCache"/>
+ /// </summary>
+ public AspNetCache()
+ :this(new RuntimeCache())
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of <see cref="AspNetCache"/>
+ /// with the specified <see cref="IRuntimeCache"/> implementation.
+ /// </summary>
+ /// <param name="runtimeCache"></param>
+ public AspNetCache(IRuntimeCache runtimeCache)
+ {
+ _cache = runtimeCache;
+ // noop
+ _cacheName = typeof(AspNetCache).FullName + "[" + this.GetHashCode() + "].";
+ }
+
+ #region Configurable Properties
+
+ /// <summary>
+ /// Gets/Sets a flag, whether to use sliding expiration policy.
+ /// </summary>
+ public bool SlidingExpiration
+ {
+ get { return _slidingExpiration; }
+ set { _slidingExpiration = value; }
+ }
+
+ /// <summary>
+ /// Gets/Sets a default priority to be applied to all items inserted into this cache.
+ /// </summary>
+ public CacheItemPriority Priority
+ {
+ get { return _priority; }
+ set { _priority = value; }
+ }
+
+ #endregion
+
/// <summary>
/// Gets a collection of all cache item keys.
***************
*** 45,57 ****
get
{
- Cache cache = HttpRuntime.Cache;
ArrayList keys = new ArrayList();
! foreach (DictionaryEntry entry in cache)
{
! keys.Add(entry.Key);
}
! return (string[]) keys.ToArray(typeof(string));
}
}
--- 158,173 ----
get
{
ArrayList keys = new ArrayList();
! foreach (DictionaryEntry entry in _cache)
{
! string key = (string) entry.Key;
! if (key.StartsWith(_cacheName))
! {
! keys.Add(key.Substring(_cacheName.Length));
! }
}
! return keys;
}
}
***************
*** 68,72 ****
public override object Get(object key)
{
! return key == null ? null : HttpRuntime.Cache.Get(key.ToString());
}
--- 184,188 ----
public override object Get(object key)
{
! return key == null ? null : _cache.Get(GenerateKey(key));
}
***************
*** 81,85 ****
if (key != null)
{
! HttpRuntime.Cache.Remove(key.ToString());
}
}
--- 197,201 ----
if (key != null)
{
! _cache.Remove(GenerateKey(key));
}
}
***************
*** 88,91 ****
--- 204,210 ----
/// Inserts an item into the cache.
/// </summary>
+ /// <remarks>
+ /// Items inserted using this method have default <see cref="Priority"/> and default <see cref="SlidingExpiration"/>
+ /// </remarks>
/// <param name="key">
/// Item key.
***************
*** 95,99 ****
/// </param>
/// <param name="timeToLive">
! /// Item's time-to-live (TTL) in milliseconds.
/// </param>
/// <param name="slidingExpiration">
--- 214,235 ----
/// </param>
/// <param name="timeToLive">
! /// Item's time-to-live (TTL).
! /// </param>
! protected override void DoInsert(object key, object value, TimeSpan timeToLive)
! {
! this.Insert( key, value, timeToLive, _slidingExpiration, _priority );
! }
!
! /// <summary>
! /// Inserts an item into the cache.
! /// </summary>
! /// <param name="key">
! /// Item key.
! /// </param>
! /// <param name="value">
! /// Item value.
! /// </param>
! /// <param name="timeToLive">
! /// Item's time-to-live.
/// </param>
/// <param name="slidingExpiration">
***************
*** 101,137 ****
/// when the item is accessed.
/// </param>
! /// <param name="priority">
/// Item priority.
/// </param>
! public override void Insert(object key, object value, int timeToLive, bool slidingExpiration, CachePriority priority)
{
AssertUtils.ArgumentNotNull(key, "key");
! CacheItemPriority itemPriority = (CacheItemPriority) Enum.Parse(typeof(CacheItemPriority), priority.ToString());
! if (timeToLive > 0)
{
if (slidingExpiration)
{
! TimeSpan slidingExpirationSpan = new TimeSpan(0, 0, timeToLive);
! HttpRuntime.Cache.Insert(key.ToString(), value, null, Cache.NoAbsoluteExpiration, slidingExpirationSpan, itemPriority, null);
}
else
{
! DateTime absoluteExpiration = DateTime.Now.AddSeconds(timeToLive);
! HttpRuntime.Cache.Insert(key.ToString(), value, null, absoluteExpiration, Cache.NoSlidingExpiration, itemPriority, null);
}
}
else
{
! HttpRuntime.Cache.Insert(key.ToString(), value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, itemPriority, null);
}
}
/// <summary>
! /// Gets the number of items in the cache.
/// </summary>
! public override int Count
{
! get { return HttpRuntime.Cache.Count; }
}
}
--- 237,296 ----
/// when the item is accessed.
/// </param>
! public void Insert(object key, object value, TimeSpan timeToLive, bool slidingExpiration)
! {
! this.Insert(key, value, timeToLive, slidingExpiration, _priority);
! }
!
! /// <summary>
! /// Inserts an item into the cache.
! /// </summary>
! /// <param name="key">
! /// Item key.
! /// </param>
! /// <param name="value">
! /// Item value.
! /// </param>
! /// <param name="timeToLive">
! /// Item's time-to-live.
! /// </param>
! /// <param name="slidingExpiration">
! /// Flag specifying whether the item's time-to-live should be reset
! /// when the item is accessed.
! /// </param>
! /// <param name="itemPriority">
/// Item priority.
/// </param>
! public void Insert(object key, object value, TimeSpan timeToLive, bool slidingExpiration, CacheItemPriority itemPriority)
{
AssertUtils.ArgumentNotNull(key, "key");
+ AssertUtils.State( TimeSpan.Zero <= timeToLive, "timeToLive" );
! if (TimeSpan.Zero < timeToLive)
{
if (slidingExpiration)
{
! TimeSpan slidingExpirationSpan = timeToLive;
! _cache.Insert(GenerateKey(key), value, null, Cache.NoAbsoluteExpiration, slidingExpirationSpan, itemPriority, null);
}
else
{
! DateTime absoluteExpiration = DateTime.Now.Add(timeToLive);
! _cache.Insert(GenerateKey(key), value, null, absoluteExpiration, Cache.NoSlidingExpiration, itemPriority, null);
}
}
else
{
! _cache.Insert(GenerateKey(key), value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, itemPriority, null);
}
}
/// <summary>
! /// Generate a key to be used for the underlying <see cref="Cache"/> implementation.
/// </summary>
! /// <param name="key"></param>
! /// <returns></returns>
! public string GenerateKey( object key )
{
! return _cacheName + key.ToString();
}
}
|