具有SQL缓存依赖项的内存缓存

时间:2019-04-06 15:34:05

标签: .net core

我正在尝试缓存已存储在数据库中的数据,例如应用程序资源。 任何想法如何在.net Core中使用sql缓存依赖项实现内存缓存?

2 个答案:

答案 0 :(得分:0)

如果数据库支持SQL依赖关系,则可以使用它来将项目删除或添加回缓存。调用OnChange时,请从缓存中删除该项目或再次插入该项目。

public class CacheWithDbDependency {
    IMemoryCache _cache;
    public CacheWithDbDependency(IMemoryCache cache) {
        _cache = cache;
    }
    private void dep_OnChange(object sender, SqlNotificationEventArgs e) {
        // Here remove or reinsert your item with latest data
    }
    public AddWithDbDependency(string Key, object value) {
        SqlDependency dep = new SqlDependency(com);
        dep.OnChange += new OnChangeEventHandler(dep_OnChange);
        _cache.SetValue(your data);
    }
}

否则,您必须查询数据并在一定间隔后确定更改并使数据无效。

答案 1 :(得分:0)

ASP.NET Core支持几种不同的缓存。最简单的缓存基于IMemoryCache

您可以使用以下代码进行缓存。

我有界面ICacheManagement

public interface ICacheManagement
{
    T GetValue<T>(object key, T defaultValue = default(T));
    void SetValue<T>(object key, T value, int timeoutInSeconds);
    void SetSlidingValue<T>(object key, T value, int timeoutInSeconds);
    void Remove(object key);
    void Remove(List<object> keys);
    void ClearTableCache(string tableName);
    void ClearTableCache(List<string> tableName);
    void ClearAllCaches();

}

CacheManagement

public class CacheManagement : ICacheManagement
{
    private readonly IMemoryCache _memoryCache;
    public CacheManagement(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }
    public T GetValue<T>(object key, T defaultValue = default) 
        => _memoryCache.TryGetValue<T>(key, out var cacheValue) ? cacheValue : defaultValue;

    public void SetValue<T>(object key, T value, int timeoutInSeconds) =>
        _memoryCache.Set<T>(key, value, new MemoryCacheEntryOptions()
            .SetAbsoluteExpiration(TimeSpan.FromSeconds(timeoutInSeconds)));

    public void SetSlidingValue<T>(object key, T value, int timeoutInSeconds) =>
        _memoryCache.Set<T>(key, value, new MemoryCacheEntryOptions()
            .SetSlidingExpiration(TimeSpan.FromSeconds(timeoutInSeconds)));

    public void Remove(object key) => _memoryCache.Remove(key);

    public void Remove(List<object> keys) => keys.ForEach(Remove);

}

使用缓存,我假设您有一个Customer类和一个存储库

public class CustomerRepository
{
    private readonly ICacheManagement _cacheManagement;

    public CustomerRepository(ICacheManagement cacheManagement)
    {
        _cacheManagement = cacheManagement;
    }

    public IEnumerable<Customer> GetAllSetting()
    {
        string cacheKey = $"cacheKey";

        IEnumerable<Customer> listSetting = _cacheManagement.GetValue<IEnumerable<Customer>>(cacheKey, null);
        if (listSetting != null)
            return listSetting;

        IEnumerable<Customer> setting = ...; //get data from database

        int temporaryCacheExpireInSeconds = 20;//time cache for this cacheKey
        _cacheManagement.SetValue<IEnumerable<Customer>>(cacheKey, setting, temporaryCacheExpireInSeconds);

        return setting;
    }
}