c# - dapper 缓存的 "information"到底是什么?

标签 c# caching dapper

在 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对象包含IDataReaderIDBCommand函数以及一些限制缓存量的控制计数器。

由于没有缓存服务器端(数据库架构等),因此实际上没有任何影响。

编辑:这就是 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/

相关文章:

c# - 有没有一种使用 Dapper 将对象或对象集合作为表值参数传递的优雅方法?

c# - dapper 中的动态 where 子句

c# - 在 this.Dispose() 之后使用 form.ShowDialog();

c# - Google Apps 脚本执行 API 服务授权每小时失败一次

javascript - IE 8 缓存问题

java - android 缓存 > 内部存储与对象缓存

java - 在 Java 中与多个打开的应用程序共享文件存储索引

asp.net-core - 在 .Net Core API 中使用 Dapper 选择计数 (*) 查询返回不正确的值

c# - Autofac 约束绑定(bind)

c# - 检查 Windows 安装程序互斥锁的可用性