Exemple #1
0
 // *** 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);
                 mLogger.Trace("GetFromCache", "Cache hit.");
                 return true;
             }
         }
     }
     mLogger.Trace("GetFromCache", "Cache miss.");
     return false;
 }
 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);
     }
 }