public void PutIntoCache(string source, Language language, string query, long totalHits, SearchEngineResultSet resultSet) { Utils.ThrowException(source == null ? new ArgumentNullException("source") : null); Utils.ThrowException(query == null ? new ArgumentNullException("query") : null); Utils.ThrowException(resultSet == null ? new ArgumentNullException("resultSet") : null); Utils.ThrowException(totalHits < resultSet.Count ? new ArgumentValueException("totalHits") : null); string normalizedQuery = string.Format("{0} {1} {2}", source, language, NormalizeQuery == null ? query : NormalizeQuery(query)); int actualSize = resultSet.Count; CacheRecord cacheRecord = new CacheRecord(); cacheRecord.TotalHits = totalHits; cacheRecord.ActualSize = resultSet.Count; StringWriter stringWriter = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); xmlWriter.Formatting = Formatting.Indented; resultSet.SaveXml(xmlWriter); cacheRecord.ResultSetXml = stringWriter.ToString(); cacheRecord.TimeStamp = DateTime.Now; if (mCache.ContainsKey(normalizedQuery)) { mCache[normalizedQuery] = cacheRecord; } else { mCache.Add(normalizedQuery, cacheRecord); } }
// *** ISearchEngineCache interface implementation *** public bool GetFromCache(string source, Language language, string query, int maxSize, ref long totalHits, ref SearchEngineResultSet resultSet) { Utils.ThrowException(source == null ? new ArgumentNullException("source") : null); Utils.ThrowException(query == null ? new ArgumentNullException("query") : null); Utils.ThrowException(maxSize < 0 ? new ArgumentOutOfRangeException("maxSize") : null); string normalizedQuery = string.Format("{0} {1} {2}", source, language, NormalizeQuery == null ? query : NormalizeQuery(query)); if (mCache.ContainsKey(normalizedQuery)) { CacheRecord cacheRecord = mCache[normalizedQuery]; if (mTtl == 0 || DateTime.Now.Subtract(cacheRecord.TimeStamp).TotalDays <= mTtl) // record is not outdated { if (cacheRecord.TotalHits == cacheRecord.ActualSize || maxSize <= cacheRecord.ActualSize) { totalHits = cacheRecord.TotalHits; XmlTextReader xmlReader = new XmlTextReader(new StringReader(cacheRecord.ResultSetXml)); resultSet = new SearchEngineResultSet(xmlReader, maxSize); Utils.VerboseLine("Cache hit."); return(true); } } } Utils.VerboseLine("Cache miss."); return(false); }
public void Load(BinarySerializer reader) { Utils.ThrowException(reader == null ? new ArgumentNullException("reader") : null); mCache.Clear(); // the following functions throw serialization-related exceptions int count = reader.ReadInt(); for (int i = 0; i < count; i++) { string key = reader.ReadString(); CacheRecord cacheRecord = new CacheRecord(reader); mCache.Add(key, cacheRecord); } mTtl = reader.ReadInt(); }