在 Dapper 的文档中发现 here它指出:
Limitations and caveats
Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object.
这到底是什么意思? 例如:它是缓存返回的数据,还是查询本身,还是两者的位?
它还表示“此[缓存]数据永远不会刷新”。如果您正在查询的表的设计模式发生更改,这对“缓存信息”有何影响?
最佳答案
据我所知,您发出的每个查询都有一个Identity
,具体取决于sql查询、其命令类型及其参数。缓存是一个并发访问的字典。
Dictionary<Identity, CacheInfo> _queryCache
此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());
}
}
这是 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); }
}
最后是缓存的容器。
static readonly System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo> _queryCache = new System.Collections.Concurrent.ConcurrentDictionary<Identity, CacheInfo>();
看看源代码,它写得很好并且易于遵循/调试。只需将文件拖到您的项目中即可。
关于c# - dapper 缓存的 "information"到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897750/