// *** 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); }
// *** 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); Utils.ThrowException(mConnection == null ? new InvalidOperationException() : null); string normalizedQuery = string.Format("{0} {1} {2}", source, language, NormalizeQuery == null ? query : NormalizeQuery(query)); bool cacheMiss = true; DataTable dataTable = mConnection.ExecuteQuery("select * from Queries where Query = ?", normalizedQuery); if (dataTable.Rows.Count != 0) { DateTime timeStamp = (DateTime)dataTable.Rows[0]["TimeStamp"]; if (mTtl == 0 || DateTime.Now.Subtract(timeStamp).TotalDays <= mTtl) // record is not outdated { int actualSizeCached = (int)dataTable.Rows[0]["ActualSize"]; long totalHitsCached = (long)dataTable.Rows[0]["TotalHits"]; if (totalHitsCached == actualSizeCached || maxSize <= actualSizeCached) { totalHits = totalHitsCached; XmlTextReader xmlReader = new XmlTextReader(new StringReader((string)dataTable.Rows[0]["ResultSetXml"])); resultSet = new SearchEngineResultSet(xmlReader, maxSize); mLogger.Trace("GetFromCache", "Cache hit."); cacheMiss = false; } } } if (cacheMiss) { mLogger.Trace("GetFromCache", "Cache miss."); } return(!cacheMiss); }
// *** ICloneable interface implementation *** public object Clone() { SearchEngineResultSet clone = new SearchEngineResultSet(); clone.mItems = mItems.Clone(); return(clone); }
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); } }
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)); mConnection.StartTransaction(); // start transaction // check if such query already exists DataTable dataTable = mConnection.ExecuteQuery("select * from Queries where Query = ?", normalizedQuery); if (dataTable.Rows.Count > 0) { // remove old database record mConnection.ExecuteNonQuery("delete from Queries where Query = ?", normalizedQuery); } // add new database record StringWriter strWriter = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(strWriter); xmlWriter.Formatting = Formatting.Indented; resultSet.SaveXml(xmlWriter); mConnection.ExecuteNonQuery("insert into Queries (Query, TotalHits, ActualSize, ResultSetXml, TimeStamp) values (?, ?, ?, ?, ?)", normalizedQuery, totalHits, resultSet.Count, strWriter.ToString(), DateTime.Now); mConnection.Commit(); // commit }