/// <summary> /// Executes the command text against the connection. /// </summary> /// <param name="behavior">An instance of <see cref="T:System.Data.CommandBehavior"/>.</param> /// <returns> /// A <see cref="T:System.Data.Common.DbDataReader"/>. /// </returns> protected override System.Data.Common.DbDataReader ExecuteDbDataReader(CommandBehavior behavior) { Query query = Query.CreateQuery(this.WrappedCommand, this.Definition.IsStoredProcedure); ICache cache = Application.Instance.Cache; DbResultItem item = new DbResultItem(); item.ConnectionString = this.Connection.ConnectionString; List <string> parameterList = null; bool cacheable = Application.Instance.CachePolicy.GetEffectivePolicy(query.QueryText, out item.AbsoluteExpiration, out item.SlidingExpiration, out item.TargetDatabase, out item.DbSyncDependency, out parameterList); ///No dependency for stored procedures if (this.Definition.IsStoredProcedure) { item.DbSyncDependency = false; } if (cache == null || query.QueryText == null || !this.Definition.IsCacheable() || !cacheable) { Interlocked.Increment(ref nonCacheableCommands); return(WrappedCommand.ExecuteReader(behavior)); } object value = null; Interlocked.Increment(ref cacheableCommands); string cacheKey = query.GetCacheKey(parameterList, this.Parameters); if (cache.GetItem(cacheKey, out value)) { Interlocked.Increment(ref cacheHits); // got cache entry - create reader based on that return(new CachingDataReaderCacheReader((DbQueryResults)value, behavior)); } else { Interlocked.Increment(ref cacheMisses); return(new EFCachingDataReaderCacheWriter( this.WrappedCommand.ExecuteReader(behavior), delegate(DbQueryResults entry) { try { item.Value = entry; cache.PutItem(cacheKey, item, WrappedCommand); Interlocked.Increment(ref cacheAdds); } catch (Exception exc) { Logger.Instance.TraceError(exc.Message); } }, behavior)); } }
public NCacheQueryable(IQueryable <T> queryable, DateTime?absoluteExpiration, TimeSpan?slidingExpiration, bool dbSynDependency) { _resultItem = new DbResultItem(); _queryable = queryable; if (absoluteExpiration != null) { _resultItem.AbsoluteExpiration = (DateTime)absoluteExpiration; } if (slidingExpiration != null) { _resultItem.SlidingExpiration = (TimeSpan)slidingExpiration; } _resultItem.DbSyncDependency = dbSynDependency; _resultItem.TargetDatabase = Application.Instance.CachePolicy.DatabaseType; }
/// <summary> /// Adds the specified entry to the cache. /// </summary> /// <param name="key">The entry key.</param> /// <param name="resultItem">The entry value.</param> public void PutItem(string key, DbResultItem resultItem, DbCommand dbCommand) { Logger.Instance.TraceDebug("Caching results against key \"" + key + "\""); if (this.cache == null) { return; } CacheItem cacheItem = new CacheItem(resultItem.Value); if (resultItem.DbSyncDependency) { cacheItem.Dependency = this.GetDependency(resultItem.TargetDatabase, key, resultItem.ConnectionString, dbCommand); } cacheItem.AbsoluteExpiration = resultItem.AbsoluteExpiration; cacheItem.SlidingExpiration = resultItem.SlidingExpiration; CacheItemVersion ver = cache.Insert(key, cacheItem); ulong v = ver.Version; Logger.Instance.TraceDebug("Cached results against key \"" + key + "\""); }
public CachedQueryEnumerator(DbResultItem item) { _enumerator = Convert(item.Value as EnumerableResult); _fromCache = true; }
private void CachingCommandExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext <System.Data.Common.DbDataReader> interceptionContext) { Query query; ICache _cache = Application.Instance.Cache; if (_cache == null) { return; } if (command.CommandType == System.Data.CommandType.StoredProcedure) { query = Query.CreateQuery(command, true); } else { query = Query.CreateQuery(command, false); } List <string> parameterList = null; DbResultItem resultItem = new DbResultItem(); resultItem.ConnectionString = command.Connection.ConnectionString; bool cacheable = Application.Instance.CachePolicy.GetEffectivePolicy(query.QueryText, out resultItem.AbsoluteExpiration, out resultItem.SlidingExpiration, out resultItem.TargetDatabase, out resultItem.DbSyncDependency, out parameterList); if (command.CommandType == System.Data.CommandType.StoredProcedure) { resultItem.DbSyncDependency = false; Logger.Instance.TraceDebug("Database dependency is not supported for store procedure."); } object value = null; string key = query.GetCacheKey(parameterList, command.Parameters); if (cacheable) { try { if (_cache.GetItem(key, out value)) { interceptionContext.SuppressExecution(); interceptionContext.Result = new CachingDataReaderCacheReader((DbQueryResults)value, interceptionContext.CommandBehavior); } else { interceptionContext.SuppressExecution(); if (resultItem.TargetDatabase == CachePolicyElement.DatabaseType.None && resultItem.DbSyncDependency) { Logger.Instance.TraceError("Database information must be provided in provider\'s configuration file in cache-policy-configuration element. Inserting item added with no database dependency."); resultItem.DbSyncDependency = false; } interceptionContext.Result = new EFCachingDataReaderCacheWriter(command.ExecuteReader(interceptionContext.CommandBehavior), delegate(DbQueryResults entry) { try { resultItem.Value = entry; _cache.PutItem(key, resultItem, command); } catch (Exception exc) { Logger.Instance.TraceError(exc.Message); } }, interceptionContext.CommandBehavior); } } catch (Exception exc) { Logger.Instance.TraceError(exc.Message); } } }
public IEnumerator <T> GetEnumerator() { if (_queryable == null) { throw new ArgumentNullException("source"); } var objectQuery = TryGetObjectQuery(_queryable) ?? _queryable as ObjectQuery; string queryString = objectQuery.ToTraceString().StripTabsAndNewlines(); string key = "IQueryableExtensionPrefix:" + queryString; if (objectQuery != null) { ICache cache = Application.Instance.Cache; object result = null; try { if (cache.GetItem(key, out result)) { IEnumerable <T> enumerable = result as IEnumerable <T>; IEnumerator <T> enumerator = enumerable != null?enumerable.GetEnumerator() : _queryable.GetEnumerator(); return(enumerator); } else { if (_resultItem.Equals(null)) { _resultItem = new DbResultItem(); LoadDefaultSettings(); } string connectionString = ((System.Data.Entity.Core.EntityClient.EntityConnection)objectQuery.Context.Connection).StoreConnection.ConnectionString; DbCommand command = null; if (_resultItem.TargetDatabase == CachePolicyElement.DatabaseType.Oracle10i2) { command = new OracleCommand(queryString, new OracleConnection(connectionString)); } else if (_resultItem.TargetDatabase == CachePolicyElement.DatabaseType.Sql2005Above) { command = new SqlCommand(queryString, new SqlConnection(connectionString)); } else if (_resultItem.DbSyncDependency) { Logger.Instance.TraceError("Database information must be provided in provider\'s configuration file in cache-policy-configuration element. Inserting item with no database dependency."); _resultItem.DbSyncDependency = false; } _resultItem.ConnectionString = connectionString; return(new CachedQueryEnumerator <T>(_queryable.GetEnumerator(), delegate(EnumerableResult entry) { _resultItem.Value = entry; cache.PutItem(key, _resultItem, command); }, false)); } } catch (Exception exc) { Logger.Instance.TraceError(exc.Message); } } return(_queryable.GetEnumerator()); }
public virtual void PutItem(string key, DbResultItem value, DbCommand command) { _cache.PutItem(key, value, command); }