确切地说,“信息”是什么?精致的缓存? [英] What exactly is the "information" that dapper caches?
问题描述
在Dapper的文档中此处指出:
On Dapper's documentation found here it states:
限制和警告
Dapper会缓存有关其运行的每个查询的信息 ,这允许它快速实现对象并快速处理参数。当前的实现将这些信息缓存在ConcurrentDictionary对象中。
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."
确切是什么?这是否意味着?
例如:是缓存返回的数据,还是查询本身,还是两者都缓存?
What exactly does this mean? Ex: Is it caching returned data, or the query itself, or bits of both?
它还说 此[缓存的]数据永远不会被刷新。如果要查询的表的设计方案已更改,这对缓存的信息有何影响?
It also says that "this [cached] data is never flushed". How does this effect the "cached information" if the design schema of the table(s) you are querying is changed?
推荐答案
据我所知,每个查询都会根据SQL查询,其命令类型和参数发出 Identity
。缓存是具有并发访问权限的字典。
As far as I know has each query you issue an Identity
, depending on the sql query, its command type and its parameters. The cache is a dictionary with concurrent access.
Dictionary<Identity, CacheInfo> _queryCache
此 CacheInfo
对象包含 IDataReader
和 IDBCommand
函数以及一些限制缓存量的控制计数器。
This CacheInfo
object contains the IDataReader
and IDBCommand
functions and some control counters which limit the cached amount.
由于未缓存任何服务器端(数据库架构等),因此实际上没有任何影响。
Since no server-side (database schema etc.) are cached, it actually doesn't have any influence.
编辑:这就是Identity类的外观
Thats how the Identity class looks like used 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());
}
}
这是CacheInfo
And here's the 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>();
看看源代码,它写得很好,易于遵循/调试。只需将文件拖到您的项目中即可。
Have a look at the source code, its very well written and easy to follow / debug. Just drag the file into your project.
这篇关于确切地说,“信息”是什么?精致的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!