using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manu.Web.UI.WebControls;
using Manu.Web.UI.WebControls.Caching;
///
<summary>A cache that saves the data in the
session</summary>
public class SessionCache : BaseCache
{
public SessionCache(CompatObjectDataSourceView
view) : base(view)
{
}
///
<summary>Gets the
cached data.</summary>
public override
CachedResult GetData(DataSourceSelectArguments
arguments, string
filterExpression,
IOrderedDictionary allParameters)
{
// if the view supports custom
paging, error
if (View.EnablePaging)
{
throw
new NotSupportedException(View.Owner.ID
+ ": Caching not supported with
paging");
}
// if the view supports custom
sorting, error
if (!String.IsNullOrEmpty(arguments.SortExpression)
&&
(View.SortParameterName.Length > 0))
{
throw
new NotSupportedException(View.Owner.ID
+ ": Caching not supported with
custom sorting");
}
// if the view supports custom
filtering, error
if(!String.IsNullOrEmpty(filterExpression)&&(View.FilterParameterName.Length>0)){
throw
new NotSupportedException(View.Owner.ID
+ ": Caching not supported with
custom filtering");
}
// gets the select parameters
IOrderedDictionary
parameters = CopyDictionary(View.SelectParameters.GetValues
(View.Context, View.Owner));
// generate a key based on the
select parameters
string cacheKey = GenerateBaseCacheKey(parameters).ToString();
return
HttpContext.Current.Session[cacheKey] as
CachedResult;
}
// saves data to the cache
public override
void SaveData(CachedResult
data, DataSourceSelectArguments arguments,
string filterExpression,
IOrderedDictionary selectParams)
{
// gets the select parameters
IOrderedDictionary
parameters = CopyDictionary(View.SelectParameters.GetValues
(View.Context, View.Owner));
// generate a key to save the
data to the cache
string cacheKey = GenerateBaseCacheKey(parameters).ToString();
if (HttpContext.Current.Session[cacheKey]
== null) {
HttpContext.Current.Session[cacheKey]
= data;
}
}
// invalidates the data stored in the cache
public override
void InvalidateData()
{
// gets the select parameters
IOrderedDictionary
parameters = CopyDictionary(View.SelectParameters.GetValues
(View.Context, View.Owner));
// remove the base key to remove
dependent entries
string cacheKey = GenerateBaseCacheKey(parameters).ToString();
HttpContext.Current.Session.Remove(cacheKey);
}
}