/// <summary>
/// Caches provided data set as a list from IQueryable by specified key
/// </summary>
/// <typeparam name="T">Cacheable type</typeparam>
/// <typeparam name="U">Sitefinity type</typeparam>
/// <param name="sfContentQuery">Sitefintiy data set to be cached</param>
/// <param name="key">Key associated with set in cache</param>
public void CacheDataSetAsList<T, U>(IQueryable<U> sfContentQuery, string key, int expirationMinutes = 0) where T: ICacheData<U>
{
//create instance of a cacheable data type
T cacheableDataItem = (T)Activator.CreateInstance(typeof(T));
//get the content type that we are working with
Type cacheableType = cacheableDataItem.GetContentType<U>();
//get a string of that content type, to be used in the key
string cacheableTypeName = cacheableDataItem.GetContentType<U>().ToString();
Logger.Writer.Write("pulling items from cache");
//try to fetch these cached items from the cache
List<T> cachedItems = (List<T>)CacheManager[key];
//as long as nothing is returned from the cache, proceed with caching items
if (cachedItems == null)
{
Logger.Writer.Write("pulled items null");
//initialize cachedItems to new list
cachedItems = new List<T>();
lock (itemLock)
{
//if the content type is not null, proceed with pulling in data from database
if (cacheableType != null)
{
//initialize a list of cacheable content items
List<U> items = new List<U>();
items = sfContentQuery.ToList<U>();
//if the list of items is not null and there is 1 or more, begin process for adding them to cache
if (items != null && items.Count > 0)
{
//create list of dependencies
List<DataItemCacheDependency> dependencies = new List<DataItemCacheDependency>();
//create list of parameters
List<ICacheItemExpiration> cacheParameters = new List<ICacheItemExpiration>();
//foreach sitefinity content item in items
foreach (U item in items)
{
//create a new instance of a cacheable object
T dataItem = (T)Activator.CreateInstance(typeof(T));
//call process item to move over cacheablecontent item values into the cacheable object
dataItem.ProcessItem(item);
//add the new object to the list of cached items
cachedItems.Add(dataItem);
//create dependency on this items id
DataItemCacheDependency dependency
= new DataItemCacheDependency(typeof(U), dataItem.Id);
//add dependency
dependencies.Add(dependency);
}
//if the list of cachedItems is not null and there is 1 or more, add them to cache
if (cachedItems != null && cachedItems.Count > 0)
{
//if more than 1 dependency add them to cacheparameters
if (dependencies.Count > 0)
cacheParameters.AddRange(dependencies);
//add sliding expiration time to cache parameters
if(expirationMinutes > 0)
cacheParameters.Add(new SlidingTime(TimeSpan.FromMinutes(expirationMinutes)));
else
cacheParameters.Add(new SlidingTime(TimeSpan.FromMinutes(DefaultExpirationMinutes)));
//add items, key, and parameters to cache through CacheManager
CacheManager.Add(
key,
cachedItems,
CacheItemPriority.Normal,
null,
cacheParameters.ToArray<ICacheItemExpiration>());
}
}
else
{
cachedItems = new List<T>();
lock (itemLock)
{
//create list of parameters
List<ICacheItemExpiration> cacheParameters = new List<ICacheItemExpiration>();
//add sliding expiration time to cache parameters
if (expirationMinutes > 0)
cacheParameters.Add(new SlidingTime(TimeSpan.FromMinutes(expirationMinutes)));
else
cacheParameters.Add(new SlidingTime(TimeSpan.FromMinutes(DefaultExpirationMinutes)));
//add items, key, and parameters to cache through CacheManager
CacheManager.Add(
key,
cachedItems,
CacheItemPriority.Normal,
null,
cacheParameters.ToArray<ICacheItemExpiration>());
}
}
}
}
}
}
Snippet from my Sitefinity CacheDataService that will cache a provided dataset (passed through as an IQueryable). If nothing is in the cache, it will execute the query and store it. If there is something already there, it will return that result.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.