public static QueryResults GetQueryResults(Query q, bool aggressiveCaching, out string cacheKey) { int startIndex = q.StartResultIndex; int endIndex = q.EndResultIndex; if (endIndex < 50 && aggressiveCaching) { q.StartResultIndex = 0; q.EndResultIndex = 50; } int hashCode = q.GetHashCode(); cacheKey = CacheKey(q); QueryResults finalResults = null; Cacheable results = null; try { using (queryLock.Lock(cacheKey, defaultTimeout)) { results = HttpRuntime.Cache[cacheKey] as Cacheable; if (results == null) { results = RunQueryAndCache(q, cacheKey); } } } catch (NamedLock<Query>.TimeoutException timeout) { throw new QueryBrokerException( string.Format("The timeout period expired when trying to retrieve query results: {0}", hashCode), timeout ); } if (results == null || results.Results == null) { throw new QueryBrokerException("Unable to retrieve query results"); } if (results.LastRunTime < lastIndexUpdate) { QueueQuery(q, cacheKey, results); } finalResults = QueryResults.BuildFromRawQuery(results.Results, CachedQueryObjectLoader); if (startIndex != finalResults.FirstIndex || endIndex != finalResults.LastIndex) { finalResults = finalResults.GetRange(startIndex, endIndex); } return finalResults; }
static string CacheKey(Query q) { return string.Concat("Query[", q.GetHashCode(), "]"); }