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 }