0

我正在尝试缓存已经存储在数据库中的应用程序资源等数据。

如何在 .NET Core 中使用 SQL 缓存依赖项实现内存缓存?

4

2 回答 2

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;
    }
}
于 2019-12-17T14:24:39.263 回答
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);
    }
}

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

于 2019-12-17T13:11:25.937 回答