新しいプロジェクトで Massive micro-ORM を使用しています。キャッシングを追加しましたが、キャッシュされたオブジェクト (動的) がまだデータベースにクエリを実行していることに気付くまで、すべてが機能しているように見えました。これにより、キャッシングのポイント全体が失われます。
結果セットをデータベースから切断する最も簡単な方法は何でしょうか。ほとんどすべての呼び出しは読み取り専用です。
次のようなコードを使用してレコードをクエリしています。
public static dynamic GetActive()
{
return Caching.LoadFromCache("Units_GetActive", 120,
() =>
{
dynamic tbl = new Units();
return tbl.Find(Enabled: 1, orderby: "SortOrder");
});
}
私のキャッシュコードは次のようになります。
public static dynamic LoadFromCache(string cacheKey, int secondsToCache, Func<object> query)
{
object tocache = null;
// result will always get the value here
// tocache will only have the value when it was pulled from our method
object result = MemoryCache.Default[cacheKey] ?? (tocache = query.Invoke());
if (secondsToCache > 0)
{
if (tocache != null) // only save to cache if it wasn't there
MemoryCache.Default.Add(cacheKey, tocache, DateTime.UtcNow.AddSeconds(secondsToCache));
}
else
{
// remove from cache only if secondsToCache was zero or less
MemoryCache.Default.Remove(cacheKey);
}
return result;
}
キャッシュ コードが機能します。問題は、返された動的オブジェクト ( IEnumerable<dynamic>
) がデータベースへの別の接続を開くことです。
アイデア?