C#如何将数据保持在本地,而不是从数据库多次加载数据
本文关键字:数据 数据库 加载 | 更新日期: 2023-09-27 18:25:36
我有一个保存函数和一个刷新函数。Save调用刷新函数。保存耗时4’756ms,刷新耗时4’187ms-->不刷新的保存耗时569ms。在刷新功能中,它从数据库中重新加载数据。我如何将数据保存在本地,但将更改保存到数据库中,这样我就不必多次从数据库加载数据了?
我使用实体框架5并使用存储库。
谢谢。
您可以使用类似HttpContext.Current.cache:的缓存
internal static class Cache<T>
{
public static void Save(IList<T> list, int insertAt, string cacheKey)
{
// Cache key per Session
cacheKey = HttpContext.Current.Session.SessionID + "~" + cacheKey;
if (list != null && list.Count > 0)
{
// if list is already there, insert items in list.
IList<T> cachedList = HttpContext.Current.Cache[cacheKey] as IList<T>;
if (cachedList != null)
{
// remove list from cache.
HttpContext.Current.Cache.Remove(cacheKey);
// insert items at position.
if (insertAt < 0)
{
insertAt = cachedList.Count;
}
foreach (T item in list)
{
cachedList.Insert(insertAt, item);
insertAt++;
}
}
else
{
// check that list is no array, because that cannot be removed.
cachedList = list.GetType().IsArray ? new List<T>(list) : list;
}
// save list in cache for 30 seconds.
HttpContext.Current.Cache.Insert(
cacheKey
, cachedList
, null
, DateTime.Now.AddSeconds(30)
, Cache.NoSlidingExpiration
);
}
}
public static void Save(IList<T> list, string cacheKey)
{
Save(list, -1, cacheKey);
}
public static void Save(IList<T> list, int insertAt)
{
Save(list, insertAt, typeof(T).Name);
}
public static int Remove(T item, string cacheKey)
{
// index of removed item.
int removedIndex = -1;
// cache key per Session
cacheKey = HttpContext.Current.Session.SessionID + "~" + cacheKey;
if (item != null)
{
// remove from cache.
IList<T> cachedList = HttpContext.Current.Cache[cacheKey] as IList<T>;
if (cachedList != null)
{
// remove list from cache.
HttpContext.Current.Cache.Remove(cacheKey);
// remove item from cached list..
removedIndex = cachedList.IndexOf(item);
cachedList.Remove(item);
// insert list for 30 sec.
HttpContext.Current.Cache.Insert(
cacheKey
, cachedList
, null
, DateTime.Now.AddSeconds(30)
, Cache.NoSlidingExpiration
);
}
}
return removedIndex;
}
public static int Remove(T item)
{
return Remove(item, typeof(T).Name);
}
public static void Clear(string cacheKey)
{
// cache Key per Session
cacheKey = HttpContext.Current.Session.SessionID + "~" + cacheKey;
HttpContext.Current.Cache.Remove(cacheKey);
}
public static void Clear()
{
Clear(typeof(T).Name);
}
}