I am finding in SharePoint 2010 that the Object Caching doesn't seem to be caching any queries for us and is instead hitting the database to retrieve items from SharePoint lists each time.
We are looking to improve the performance of a SharePoint site, which has custom code that utilises SharePoint lists in many places. I would like to cache the queries I make to these lists given that the underlying data is fairly static.
We have investigated the various ways to query the SharePoint list, with this link suggesting that CrossListQueryCache is the way to go to utilize SharePoint's caching,taking care to use a constructor that takes in an SPSite object not SPWeb (as using the later apparently will never use the cache).
The basic code structure I am using in our application is as such:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteBaseUrl, token))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["NameOfAList"];
var crossListQuery = new CrossListQueryInfo
{
Lists = string.Format("<Lists><List ID='{0}' /></Lists>", list.ID.ToString("D")),
ViewFields = "<FieldRef Name='NameOfAField' />",
UseCache = true,
Query = string.Empty,
Webs = @"<Webs Scope=""Recursive"" />"
};
var cache = new CrossListQueryCache(crossListQuery);
var results = cache.GetSiteData(site, siteBaseUrl);
foreach (DataRow item in results.Rows)
{
// use the results
}
}
}
});
What I have found, with a trace on the database, is that the cache.GetSiteData(site, siteBaseUrl)
call will grab the list items from the database even though I've performed the same query numerous times in the past and in my mind this query should be cached.
I have set up the "super user" and "super reader" accounts which has been suggested as an issue that could cause problems with Object caching in various forums, but this hasn't helped at all.
Is there something I'm missing?