Skip to content

Instantly share code, notes, and snippets.

@csim
Last active August 29, 2015 13:58
Show Gist options
  • Save csim/10284798 to your computer and use it in GitHub Desktop.
Save csim/10284798 to your computer and use it in GitHub Desktop.
SharePoint Content Database as a Cache
public abstract class RapidCachedDataAdapterBase<TData>
where TData : class
{
protected Guid SiteID;
protected virtual bool AutoRefreshCache;
protected virtual string CacheKey;
protected virtual string CacheExpireKey;
protected virtual int CacheTimeoutMinutes;
public virtual TData CachedData;
public RapidCachedDataAdapterBase(Guid siteID);
public RapidCachedDataAdapterBase(SPSite site);
protected abstract TData GetNativeData();
protected virtual TData GetData();
protected virtual TData GetCache();
protected virtual void SetCache(TData data);
protected virtual string Serialize(TData data);
protected virtual TData Deserialize(string data);
protected string SerializeInternal(object data);
protected T DeserializeInternal<T>(string data);
public virtual void InvalidateCache();
}
public class FaqDataAdapter : RapidCachedDataAdapterBase<DataTable>
{
public FaqDataAdapter() : base(SPContext.Current.Site.ID)
{
}
public FaqDataAdapter(Guid siteID) : base(siteID)
{
}
public FaqDataAdapter(SPSite site) : base(site)
{
}
protected override DataTable GetNativeData()
{
DataTable table = null;
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (SPSite esite = new SPSite(SiteID))
{
SPList list = esite.RootWeb.GetList("faq");
table = list.Items.GetDataTable();
}
});
return table;
}
protected override string Serialize(DataTable data)
{
if (data == null)
return null;
var ds = new DataSet();
ds.Tables.Add(data);
return SerializeInternal(ds);
}
protected override DataTable Deserialize(string data)
{
if (string.IsNullOrEmpty(data))
return null;
var ds = DeserializeInternal<DataSet>(data);
return ds.Tables[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment