Last active
August 29, 2015 13:58
-
-
Save csim/10284798 to your computer and use it in GitHub Desktop.
SharePoint Content Database as a Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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