Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
22f8c33b
D
Dapper
项目概览
int
/
Dapper
大约 1 年 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dapper
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
22f8c33b
编写于
6月 03, 2011
作者:
M
mgravell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cache multi-grid readers; purge cache when exception during read
上级
b0b39807
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
89 addition
and
26 deletion
+89
-26
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+89
-26
未找到文件。
Dapper/SqlMapper.cs
浏览文件 @
22f8c33b
...
...
@@ -122,6 +122,11 @@ private static bool TryGetQueryCache(Identity key, out CacheInfo value)
{
lock
(
_queryCache
)
{
return
_queryCache
.
TryGetValue
(
key
,
out
value
);
}
}
private
static
void
PurgeQueryCache
(
Identity
key
)
{
lock
(
_queryCache
)
{
_queryCache
.
Remove
(
key
);
}
}
#else
static
readonly
System
.
Collections
.
Concurrent
.
ConcurrentDictionary
<
Identity
,
CacheInfo
>
_queryCache
=
new
System
.
Collections
.
Concurrent
.
ConcurrentDictionary
<
Identity
,
CacheInfo
>();
private
static
void
SetQueryCache
(
Identity
key
,
CacheInfo
value
)
...
...
@@ -132,6 +137,11 @@ private static bool TryGetQueryCache(Identity key, out CacheInfo value)
{
return
_queryCache
.
TryGetValue
(
key
,
out
value
);
}
private
static
void
PurgeQueryCache
(
Identity
key
)
{
CacheInfo
info
;
_queryCache
.
TryRemove
(
key
,
out
info
);
}
#endif
static
readonly
Dictionary
<
RuntimeTypeHandle
,
DbType
>
typeMap
;
...
...
@@ -190,18 +200,25 @@ private static DbType LookupDbType(Type type)
throw
new
NotSupportedException
(
string
.
Format
(
"The type : {0} is not supported by dapper"
,
type
));
}
private
class
Identity
:
IEquatable
<
Identity
>
internal
class
Identity
:
IEquatable
<
Identity
>
{
internal
Identity
(
string
sql
,
IDbConnection
cnn
,
Type
type
,
Type
parametersType
,
Type
[]
otherTypes
)
internal
Identity
ForGrid
(
Type
primaryType
,
int
gridIndex
)
{
return
new
Identity
(
sql
,
connectionString
,
primaryType
,
parametersType
,
null
,
gridIndex
);
}
internal
Identity
(
string
sql
,
IDbConnection
connection
,
Type
type
,
Type
parametersType
,
Type
[]
otherTypes
)
:
this
(
sql
,
connection
.
ConnectionString
,
type
,
parametersType
,
otherTypes
,
0
)
{
}
private
Identity
(
string
sql
,
string
connectionString
,
Type
type
,
Type
parametersType
,
Type
[]
otherTypes
,
int
gridIndex
)
{
this
.
sql
=
sql
;
this
.
connectionString
=
c
nn
.
C
onnectionString
;
this
.
connectionString
=
connectionString
;
this
.
type
=
type
;
this
.
parametersType
=
parametersType
;
unchecked
{
hashCode
=
17
;
// we *know* we are using this in a dictionary, so pre-compute this
hashCode
=
hashCode
*
23
+
gridIndex
.
GetHashCode
();
hashCode
=
hashCode
*
23
+
(
sql
==
null
?
0
:
sql
.
GetHashCode
());
hashCode
=
hashCode
*
23
+
(
type
==
null
?
0
:
type
.
GetHashCode
());
if
(
otherTypes
!=
null
)
...
...
@@ -220,7 +237,7 @@ public override bool Equals(object obj)
return
Equals
(
obj
as
Identity
);
}
private
readonly
string
sql
;
private
readonly
int
hashCode
;
private
readonly
int
hashCode
,
gridIndex
;
private
readonly
Type
type
;
private
readonly
string
connectionString
;
internal
readonly
Type
parametersType
;
...
...
@@ -232,6 +249,7 @@ public bool Equals(Identity other)
{
return
other
!=
null
&&
gridIndex
==
other
.
gridIndex
&&
type
==
other
.
type
&&
sql
==
other
.
sql
&&
connectionString
==
other
.
connectionString
&&
...
...
@@ -339,7 +357,7 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
{
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
(
object
)
param
,
commandTimeout
,
commandType
);
reader
=
cmd
.
ExecuteReader
();
return
new
GridReader
(
cmd
,
reader
);
return
new
GridReader
(
cmd
,
reader
,
identity
);
}
catch
{
...
...
@@ -356,7 +374,9 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
{
var
identity
=
new
Identity
(
sql
,
cnn
,
typeof
(
T
),
param
==
null
?
null
:
param
.
GetType
(),
null
);
var
info
=
GetCacheInfo
(
identity
);
bool
clean
=
true
;
try
{
using
(
var
cmd
=
SetupCommand
(
cnn
,
transaction
,
sql
,
info
.
ParamReader
,
param
,
commandTimeout
,
commandType
))
{
using
(
var
reader
=
cmd
.
ExecuteReader
())
...
...
@@ -371,8 +391,20 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
while
(
reader
.
Read
())
{
yield
return
deserializer
(
reader
);
clean
=
false
;
var
next
=
deserializer
(
reader
);
clean
=
true
;
yield
return
next
;
}
}
}
clean
=
false
;
}
finally
{
// throw away query plan on failure - could
if
(!
clean
)
{
PurgeQueryCache
(
identity
);
}
}
}
...
...
@@ -547,9 +579,22 @@ class DontMap {}
}
if
(
mapIt
!=
null
)
{
bool
clean
=
true
;
try
{
while
(
reader
.
Read
())
{
yield
return
mapIt
(
reader
);
clean
=
false
;
TReturn
next
=
mapIt
(
reader
);
clean
=
true
;
yield
return
next
;
}
}
finally
{
if
(!
clean
)
PurgeQueryCache
(
identity
);
}
}
}
}
...
...
@@ -1195,10 +1240,12 @@ public class GridReader : IDisposable
{
private
IDataReader
reader
;
private
IDbCommand
command
;
internal
GridReader
(
IDbCommand
command
,
IDataReader
reader
)
private
Identity
identity
;
internal
GridReader
(
IDbCommand
command
,
IDataReader
reader
,
Identity
identity
)
{
this
.
command
=
command
;
this
.
reader
=
reader
;
this
.
identity
=
identity
;
}
/// <summary>
/// Read the next grid of results
...
...
@@ -1207,24 +1254,40 @@ public IEnumerable<T> Read<T>()
{
if
(
reader
==
null
)
throw
new
ObjectDisposedException
(
GetType
().
Name
);
if
(
consumed
)
throw
new
InvalidOperationException
(
"Each grid can only be iterated once"
);
var
deserializer
=
GetDeserializer
<
T
>(
reader
,
0
,
-
1
,
false
);
var
typedIdentity
=
identity
.
ForGrid
(
typeof
(
T
),
gridIndex
);
CacheInfo
cache
=
GetCacheInfo
(
typedIdentity
);
var
deserializer
=
(
Func
<
IDataReader
,
T
>)
cache
.
Deserializer
;
if
(
deserializer
==
null
)
{
deserializer
=
GetDeserializer
<
T
>(
reader
,
0
,
-
1
,
false
);
cache
.
Deserializer
=
deserializer
;
}
consumed
=
true
;
return
ReadDeferred
(
gridIndex
,
deserializer
);
return
ReadDeferred
(
gridIndex
,
deserializer
,
typedIdentity
);
}
// todo multimapping.
private
IEnumerable
<
T
>
ReadDeferred
<
T
>(
int
index
,
Func
<
IDataReader
,
T
>
deserializer
)
private
IEnumerable
<
T
>
ReadDeferred
<
T
>(
int
index
,
Func
<
IDataReader
,
T
>
deserializer
,
Identity
typedIdentity
)
{
bool
clean
=
true
;
try
{
while
(
index
==
gridIndex
&&
reader
.
Read
())
{
yield
return
deserializer
(
reader
);
clean
=
false
;
T
next
=
deserializer
(
reader
);
clean
=
true
;
yield
return
next
;
}
}
finally
// finally so that First etc progresses things even when multiple rows
{
if
(!
clean
)
{
PurgeQueryCache
(
typedIdentity
);
}
if
(
index
==
gridIndex
)
{
NextResult
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录