As far as I know, each query issues Identity
, depending on the SQL query, its type of command and its parameters. A cache is a parallel access dictionary.
Dictionary<Identity, CacheInfo> _queryCache
This CacheInfo
object contains the IDataReader
and IDBCommand
and some watch counters that limit the cached amount.
Since the server side (database schema, etc.) is not cached, it actually has no effect.
Edit: The way the Identity class looks for caching.
private Identity(string sql, CommandType? commandType, string connectionString, Type type, Type parametersType, Type[] otherTypes, int gridIndex) { this.sql = sql; this.commandType = commandType; this.connectionString = connectionString; this.type = type; this.parametersType = parametersType; this.gridIndex = gridIndex; unchecked { hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this hashCode = hashCode * 23 + commandType.GetHashCode(); hashCode = hashCode * 23 + gridIndex.GetHashCode(); hashCode = hashCode * 23 + (sql == null ? 0 : sql.GetHashCode()); hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode()); if (otherTypes != null) { foreach (var t in otherTypes) { hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode()); } } hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode()); hashCode = hashCode * 23 + (parametersType == null ? 0 : parametersType.GetHashCode()); } }
And here is CacheInfo
class CacheInfo { public Func<IDataReader, object> Deserializer { get; set; } public Func<IDataReader, object>[] OtherDeserializers { get; set; } public Action<IDbCommand, object> ParamReader { get; set; } private int hitCount; public int GetHitCount() { return Interlocked.CompareExchange(ref hitCount, 0, 0); } public void RecordHit() { Interlocked.Increment(ref hitCount); } }
And finally, the cache container.
static readonly System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo> _queryCache = new System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo>();
Look at the source code, it is very well written and easy to follow / debug. Just drag and drop the file into your project.