public void SetSlice(DatastoreEntities context, SummarySlice slice, int repositoryId, SummarySliceValue results) { if (!ConfigHelper.AllowCaching) { return; } int changeStamp = 0; int queryHash = 0; CacheResultsSlice item; using (var q = new AcquireReaderLock(QueryCacheID, "QueryCache")) { //Do not cache big items if (results.RecordList.Count > 500) { return; } if (slice.Query != null && !string.IsNullOrEmpty(slice.Query.Keyword) && !ConfigHelper.AllowCacheWithKeyword) { return; } queryHash = slice.GetHashCode(); changeStamp = RepositoryManager.GetRepositoryChangeStamp(context, repositoryId); item = _cacheSlice.FirstOrDefault(x => x.QueryHash == queryHash && x.RepositoryId == repositoryId && x.ChangeStamp == changeStamp); //If data has not changed and results are in cache then do nothing except mark as accessed if (item != null) { item.Results = results; item.Timestamp = DateTime.Now; return; } } using (var q = new AcquireWriterLock(QueryCacheID, "QueryCache")) { //Remove previous cache _cacheSlice.RemoveAll(x => x.QueryHash == queryHash && x.RepositoryId == repositoryId); //Create a new cache item item = new CacheResultsSlice() { QueryHash = queryHash, RepositoryId = repositoryId, ChangeStamp = changeStamp, Results = results, QueryString = slice.ToString(), ParentId = RepositoryManager.GetSchemaParentId(repositoryId), }; _cacheSlice.Add(item); } }
public SummarySliceValue GetSlice(DatastoreEntities context, SummarySlice slice, int repositoryId) { if (!ConfigHelper.AllowCaching) { return(null); } using (var q = new AcquireReaderLock(QueryCacheID, "QueryCache")) { var queryHash = slice.GetHashCode(); var changeStamp = RepositoryManager.GetRepositoryChangeStamp(context, repositoryId); var item = _cacheSlice.FirstOrDefault(x => x.QueryHash == queryHash && x.RepositoryId == repositoryId && x.ChangeStamp == changeStamp); if (item == null) { return(null); } item.Timestamp = DateTime.Now; item.HitCount++; return(item.Results); } }