• Magicodes.WeiChat——缓存管理


    本框架支持缓存管理,内部机制使用开源库CacheManager。支持全局缓存、租户缓存,默认使用的系统缓存实现,可以在Web.config将其配置为其他缓存类型,比如支持Redis、内存等。

    开源库地址:https://github.com/MichaCo/CacheManager

    1.1.1 配置

    默认配置如下,见Web.config cacheManager元素内容:

    <!--缓存配置-->
    
    <cacheManager xmlns="http://tempuri.org/CacheManagerCfg.xsd">
    
    <managers>
    
    <cache name="defaultCache" updateMode="Up" enableStatistics="true" enablePerformanceCounters="true">
    
    <handle ref="sysCache" name="defaultHandle" expirationMode="Absolute" timeout="3600s"/>
    
    </cache>
    
    </managers>
    
    <cacheHandles>
    
    <handleDef id="sysCache" type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching" />
    
    </cacheHandles>
    
    </cacheManager>

    相关配置说明请参考:http://cachemanager.net/Documentation/Index/cachemanager_configuration

    1.1.2 相关函数介绍:
    /// <summary>
    
    /// 获取缓存
    
    /// </summary>
    
    /// <typeparam name="T">值类型</typeparam>
    
    /// <param name="key">key</param>
    
    /// <returns></returns>
    
    public T Get<T>(string key)
    
    /// <summary>
    
    /// 获取缓存
    
    /// </summary>
    
    /// <typeparam name="T">值类型</typeparam>
    
    /// <param name="key">key</param>
    
    /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param>
    
    /// <returns></returns>
    
    public T GetByTenant<T>(string key, string tenantKey = null)
    
    /// <summary>
    
    /// 添加或更新缓存
    
    /// </summary>
    
    /// <typeparam name="T">值类型</typeparam>
    
    /// <param name="key">key</param>
    
    /// <param name="value"></param>
    
    public void AddOrUpdate<T>(string key, T value)
    
    /// <summary>
    
    /// 添加或更新缓存
    
    /// </summary>
    
    /// <typeparam name="T">值类型</typeparam>
    
    /// <param name="key">key</param>
    
    /// <param name="value"></param>
    
    /// <param name="expire">过期时间</param>
    
    public void AddOrUpdate<T>(string key, T value, TimeSpan expire)
    
    /// <summary>
    
    /// 根据租户缓存
    
    /// </summary>
    
    /// <typeparam name="T"></typeparam>
    
    /// <param name="key"></param>
    
    /// <param name="value"></param>
    
    /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param>
    
    public void AddOrUpdateByTenant<T>(string key, T value, string tenantKey = null)
    
    /// <summary>
    
    /// 根据租户缓存
    
    /// </summary>
    
    /// <typeparam name="T"></typeparam>
    
    /// <param name="key"></param>
    
    /// <param name="value"></param>
    
    /// <param name="expire"></param>
    
    /// <param name="tenantKey"></param>
    
    public void AddOrUpdateByTenant<T>(string key, T value, TimeSpan expire, string tenantKey = null)
    
    /// <summary>
    
    /// 移除
    
    /// </summary>
    
    /// <param name="key">缓存Key</param>
    
    /// <returns>是否移除成功</returns>
    
    public bool Remove<T>(string key)
    
    /// <summary>
    
    /// 根据租户移除
    
    /// </summary>
    
    /// <param name="key">缓存Key</param>
    
    /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param>
    
    /// <returns>是否移除成功</returns>
    
    public bool RemoveByTenant<T>(string key, string tenantKey = null)
    
    /// <summary>
    
    /// 清理所有
    
    /// </summary>
    
    /// <typeparam name="T">值类型</typeparam>
    
    public void Clear<T>()
    
    /// <summary>
    
    /// 清理所有
    
    /// </summary>
    
    /// <typeparam name="T">值类型</typeparam>
    
    /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param>
    
    public void ClearByTenant<T>(string tenantKey = null)
    1.1.3 Demo

    获取缓存对象:

    using Magicodes.WeiChat.Infrastructure.Cache;

    var cache =CacheManager.Current;

    获取值(全局):

    var value = cache.Get<int>("UserSummaryCount");

    获取值(按租户)

    var value = cache.GetByTenant<int>("UserSummaryCount");

    添加或更新值(全局):

    cache.AddOrUpdate("UserSummaryCount", value);

    cache.AddOrUpdate("UserSummaryCount", value, TimeSpan.FromHours(1));

    添加或更新值(按租户):

    cache.AddOrUpdateByTenant("UserSummaryCount", value);

    cache.AddOrUpdateByTenant("UserSummaryCount", value, TimeSpan.FromHours(1));

  • 相关阅读:
    转移虚拟机后ubuntu network available SIOCSIFADDR: No such device
    模板中国剩余定理
    数论 CF27E Number With The Given Amount Of Divisors
    模板 输入输出优化
    模板 欧拉定理
    洛谷P1141 01迷宫
    图论拓扑排序
    归并排序 分治
    HZNUACM寒假集训Day12小结 数论入门 题解
    组合数学基础
  • 原文地址:https://www.cnblogs.com/codelove/p/5476404.html
Copyright © 2020-2023  润新知