-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheDbDataAdapter.cs
More file actions
107 lines (85 loc) · 3.42 KB
/
CacheDbDataAdapter.cs
File metadata and controls
107 lines (85 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Runtime.Caching;
namespace CacheCommander
{
public class CacheDbDataAdapter : DbDataAdapter
{
private readonly CacheDbCommand _cachedCommand;
private static readonly MemoryCache _cache = MemoryCache.Default;
private CacheDbDataAdapter()
{
GC.SuppressFinalize(this);
}
public CacheDbDataAdapter(CacheDbCommand command) : this()
{
_cachedCommand = command ?? throw new ArgumentNullException(nameof(command));
}
public override int Fill(DataSet dataSet)
{
if (dataSet == null)
throw new InvalidOperationException("dataSet cannot be null");
string cacheKey = GenerateCacheKey();
var procedures = Configuration.GetCacheProcedures();
bool useCache = procedures?.Keys?.Contains(_cachedCommand.CommandText) == true && cacheKey?.Length > 0;
if (useCache && _cache.Contains(cacheKey))
{
var cacheDataSet = (DataSet)_cache.Get(cacheKey);
dataSet.Merge(cacheDataSet);
return cacheDataSet.Tables.Cast<DataTable>().Sum(t => t.Rows.Count);
}
var newDataSet = new DataSet();
using (var reader = _cachedCommand.ExecuteCacheDataReader())
{
DataTable param = null;
newDataSet.Load(reader, LoadOption.OverwriteChanges, param);
}
if (useCache)
{
var clonedDataSet = newDataSet.Copy();
TimeSpan cacheDuration = TimeSpan.FromMinutes(procedures[_cachedCommand.CommandText]);
_cache.Set(cacheKey, clonedDataSet, DateTimeOffset.UtcNow.Add(cacheDuration));
}
dataSet.Merge(newDataSet);
return newDataSet.Tables.Cast<DataTable>().Sum(t => t.Rows.Count);
}
public new int Fill(DataTable dataTable)
{
if (dataTable == null)
throw new ArgumentNullException(nameof(dataTable));
string cacheKey = GenerateCacheKey();
var procedures = Configuration.GetCacheProcedures();
bool useCache = procedures?.Keys?.Contains(_cachedCommand.CommandText) == true && cacheKey?.Length > 0;
if (useCache && _cache.Contains(cacheKey))
{
var cacheDataTable = (DataTable)_cache.Get(cacheKey);
dataTable.Merge(cacheDataTable);
return cacheDataTable.Rows.Count;
}
using (var reader = _cachedCommand.ExecuteAdapterDataReader())
{
dataTable.Load(reader);
}
if (useCache)
{
var clonedTable = dataTable.Copy();
TimeSpan cacheDuration = TimeSpan.FromMinutes(procedures[_cachedCommand.CommandText]);
_cache.Set(cacheKey, clonedTable, DateTimeOffset.UtcNow.Add(cacheDuration));
}
return dataTable.Rows.Count;
}
private string GenerateCacheKey()
{
try
{
return _cachedCommand.CommandText + string.Join(",", _cachedCommand.Parameters.Cast<DbParameter>().Select(p => p.Value?.ToString() ?? "NULL"));
}
catch
{
return string.Empty;
}
}
}
}