据我所知,每个查询都会发出一个Identity
,具体取决于sql查询,它的命令类型及其参数.缓存是具有并发访问权限的字典.
Dictionary<Identity, CacheInfo> _queryCache
Run Code Online (Sandbox Code Playgroud)
此CacheInfo
对象包含IDataReader
和IDBCommand
函数以及一些限制缓存量的控制计数器.
由于没有缓存服务器端(数据库架构等),它实际上没有任何影响.
编辑:这是Identity类看起来如何用于缓存.
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());
}
}
Run Code Online (Sandbox Code Playgroud)
这是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); }
}
Run Code Online (Sandbox Code Playgroud)
最后是缓存的容器.
static readonly System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo> _queryCache = new System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo>();
Run Code Online (Sandbox Code Playgroud)
看看源代码,它写得很好,易于跟踪/调试.只需将文件拖到项目中即可.
归档时间: |
|
查看次数: |
1487 次 |
最近记录: |