Skip to content

Commit

Permalink
Refactored RequestCacheableDictionary into CacheableDictionary and in…
Browse files Browse the repository at this point in the history
…herited IRequestCache from ICache so the cache implementation can be swapped with other types of caches, such as a session cache.
  • Loading branch information
NightOwl888 committed Aug 31, 2013
1 parent 1ffb80a commit 7bc541e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
13 changes: 13 additions & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/Caching/ICache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace MvcSiteMapProvider.Caching
{
/// <summary>
/// Contract for a class to provide type-safe access to a cache dictionary.
/// </summary>
public interface ICache
{
T GetValue<T>(string key);
void SetValue<T>(string key, T value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace MvcSiteMapProvider.Caching
/// Contract for a class to provide type-safe access to a request-level cache.
/// </summary>
public interface IRequestCache
: ICache
{
T GetValue<T>(string key);
void SetValue<T>(string key, T value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ namespace MvcSiteMapProvider.Collections
/// mode will automatically switch to a writeable request-cached copy of the original dictionary
/// during any write operation.
/// </summary>
public class RequestCacheableDictionary<TKey, TValue>
public class CacheableDictionary<TKey, TValue>
: LockableDictionary<TKey, TValue>
{
public RequestCacheableDictionary(
public CacheableDictionary(
ISiteMap siteMap,
IRequestCache requestCache
ICache cache
)
: base(siteMap)
{
if (requestCache == null)
throw new ArgumentNullException("requestCache");
if (cache == null)
throw new ArgumentNullException("cache");

this.requestCache = requestCache;
this.cache = cache;
}

protected readonly IRequestCache requestCache;
protected readonly ICache cache;
protected readonly Guid instanceId = Guid.NewGuid();

#region Write Operations
Expand Down Expand Up @@ -157,15 +157,15 @@ public override ICollection<TValue> Values
/// <summary>
/// Override this property and set it to false to disable all caching operations.
/// </summary>
protected virtual bool RequestCachingEnabled
protected virtual bool CachingEnabled
{
get { return true; }
}


protected virtual string GetCacheKey()
{
return "__REQUEST_CACHEABLE_DICTIONARY_" + this.instanceId.ToString();
return "__CACHEABLE_DICTIONARY_" + this.instanceId.ToString();
}


Expand All @@ -177,10 +177,10 @@ protected virtual string GetCacheKey()
get
{
IDictionary<TKey, TValue> result = null;
if (this.RequestCachingEnabled)
if (this.CachingEnabled)
{
var key = this.GetCacheKey();
result = this.requestCache.GetValue<IDictionary<TKey, TValue>>(key);
result = this.cache.GetValue<IDictionary<TKey, TValue>>(key);
if (result == null)
{
// Request is not cached, return base dictionary
Expand All @@ -203,18 +203,18 @@ protected virtual string GetCacheKey()
get
{
IDictionary<TKey, TValue> result = null;
if (this.IsReadOnly && this.RequestCachingEnabled)
if (this.IsReadOnly && this.CachingEnabled)
{
var key = this.GetCacheKey();
result = this.requestCache.GetValue<IDictionary<TKey, TValue>>(key);
result = this.cache.GetValue<IDictionary<TKey, TValue>>(key);
if (result == null)
{
// This is the first write operation request in read-only mode,
// we need to create a new dictionary and cache it
// with a copy of the current values.
result = new Dictionary<TKey, TValue>();
base.CopyTo(result);
this.requestCache.SetValue<IDictionary<TKey, TValue>>(key, result);
this.cache.SetValue<IDictionary<TKey, TValue>>(key, result);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// This class has infinite recursion problems. This was avoided in the RequestCacheableDictionary class because its base
// This class has infinite recursion problems. This was avoided in the CacheableDictionary class because its base
// class stores the dictionary in a wrapped instance that has protected access. Unfortunately, with List<T> that isn't the case.
// So for this to work, its base class needs to wrap a List<T> and override all members of List<T> or it needs to inherit
// from a base class that does same and exposes the internal list at least at the protected level.
Expand All @@ -18,22 +18,22 @@
// /// mode will automatically switch to a writeable request-cached copy of the original list
// /// during any write operation.
// /// </summary>
// public class RequestCacheableList<T>
// public class CacheableList<T>
// : LockableList<T>
// {
// public RequestCacheableList(
// public CacheableList(
// ISiteMap siteMap,
// IRequestCache requestCache
// ICache cache
// )
// : base(siteMap)
// {
// if (requestCache == null)
// throw new ArgumentNullException("requestCache");
// if (cache == null)
// throw new ArgumentNullException("cache");

// this.requestCache = requestCache;
// this.cache = cache;
// }

// protected readonly IRequestCache requestCache;
// protected readonly ICache cache;
// protected readonly Guid instanceId = Guid.NewGuid();

// #region Write Operations
Expand Down Expand Up @@ -308,15 +308,15 @@
// /// <summary>
// /// Override this property and set it to false to disable all caching operations.
// /// </summary>
// protected virtual bool RequestCachingEnabled
// protected virtual bool CachingEnabled
// {
// get { return true; }
// }


// protected virtual string GetCacheKey()
// {
// return "__REQUEST_CACHEABLE_LIST_" + this.instanceId.ToString();
// return "__CACHEABLE_LIST_" + this.instanceId.ToString();
// }


Expand All @@ -328,10 +328,10 @@
// get
// {
// LockableList<T> result = null;
// if (this.RequestCachingEnabled)
// if (this.CachingEnabled)
// {
// var key = this.GetCacheKey();
// result = this.requestCache.GetValue<LockableList<T>>(key);
// result = this.cache.GetValue<LockableList<T>>(key);
// if (result == null)
// {
// // Request is not cached, return base list
Expand All @@ -355,18 +355,18 @@
// get
// {
// LockableList<T> result = null;
// if (this.IsReadOnly && this.RequestCachingEnabled)
// if (this.IsReadOnly && this.CachingEnabled)
// {
// var key = this.GetCacheKey();
// result = this.requestCache.GetValue<LockableList<T>>(key);
// result = this.cache.GetValue<LockableList<T>>(key);
// if (result == null)
// {
// // This is the first write operation request in read-only mode,
// // we need to create a new dictionary and cache it
// // with a copy of the current values.
// result = new LockableList<T>(this.siteMap);
// base.CopyTo(result);
// this.requestCache.SetValue<LockableList<T>>(key, result);
// this.cache.SetValue<LockableList<T>>(key, result);
// }
// }
// else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace MvcSiteMapProvider.Collections.Specialized
/// localization of custom attributes.
/// </summary>
public class AttributeDictionary
: RequestCacheableDictionary<string, object>, IAttributeDictionary
: CacheableDictionary<string, object>, IAttributeDictionary
{
public AttributeDictionary(
ISiteMap siteMap,
ILocalizationService localizationService,
IRequestCache requestCache
ICache cache
)
: base(siteMap, requestCache)
: base(siteMap, cache)
{
if (localizationService == null)
throw new ArgumentNullException("localizationService");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace MvcSiteMapProvider.Collections.Specialized
/// the behavior of the route values.
/// </summary>
public class RouteValueDictionary
: RequestCacheableDictionary<string, object>, IRouteValueDictionary
: CacheableDictionary<string, object>, IRouteValueDictionary
{
public RouteValueDictionary(
ISiteMap siteMap,
IRequestCache requestCache
) : base(siteMap, requestCache)
ICache cache
) : base(siteMap, cache)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Caching\AspNetCompositeCacheDependency.cs" />
<Compile Include="Caching\AspNetFileCacheDependency.cs" />
<Compile Include="Caching\CacheDetails.cs" />
<Compile Include="Caching\ICache.cs" />
<Compile Include="Caching\ICacheDetails.cs" />
<Compile Include="Caching\ICacheDependency.cs" />
<Compile Include="Caching\ICacheProvider.cs" />
Expand All @@ -129,8 +130,8 @@
<Compile Include="Collections\IThreadSafeDictionary.cs" />
<Compile Include="Collections\LockableDictionary.cs" />
<Compile Include="Collections\LockableList.cs" />
<Compile Include="Collections\RequestCacheableDictionary.cs" />
<Compile Include="Collections\RequestCacheableList.cs" />
<Compile Include="Collections\CacheableDictionary.cs" />
<Compile Include="Collections\CacheableList.cs" />
<Compile Include="Collections\Specialized\SourceMetadataDictionary.cs" />
<Compile Include="CompositeSiteMapNodeVisibilityProvider.cs" />
<Compile Include="DI\Composer.cs" />
Expand Down

0 comments on commit 7bc541e

Please sign in to comment.