zoukankan      html  css  js  c++  java
  • CacheManager Net Core Redis 缓存

    using CacheManager.Core;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Service.Cache
    {
    public class QKCacheManager
    {
    public static QKCacheFactory factory = new QKCacheFactory();
    /// <summary>
    /// 读取缓存
    /// </summary>
    /// <param name="cacheKey"></param>
    /// <returns></returns>
    public static T GetCache<T>(CacheKeyEnum cacheKey)
    {
    var cacheInfo = factory.Create(cacheKey);
    var result = cacheInfo.Get();
    if (result != null)
    {
    return (T)Convert.ChangeType(result, typeof(T));
    }
    else
    {
    return default(T);
    }
    }
    
    /// <summary>
    /// 默认缓存时间
    /// </summary>
    static int ExpireSeconds = 30;
    /// <summary>
    /// 新增
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="obj"></param>
    /// <param name="expire"></param>
    /// <returns></returns>
    public static bool Add<T>(ICacheManager<T> cache, string key, T obj)
    {
    return Add(cache, key, obj, ExpireSeconds);
    }
    /// <summary>
    /// 放入缓存,如果存在则替换,否则新建
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="cache"></param>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">缓存对象</param>
    /// <param name="expire">缓存时限(秒)</param>
    /// <returns>True:放入缓存成功,False:不做缓存或放入缓存失败</returns>
    public static bool Add<T>(ICacheManager<T> cache, string key, T obj, int expire = 0)
    {
    bool result = false;
    expire = expire > 0 ? expire : ExpireSeconds;
    if (obj != null)
    {
    cache.Put(new CacheItem<T>(key, obj,
    ExpirationMode.Absolute,
    TimeSpan.FromSeconds(expire)));
    result = true;
    }
    return result;
    }
    /// <summary>
    /// 移除指定缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="cache"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static bool Remove<T>(ICacheManager<T> cache, string key)
    {
    bool result = false;
    if (string.IsNullOrEmpty(key))
    {
    cache.Remove(key);
    result = true;
    }
    return result;
    }
    /// <summary>
    /// 获取指定缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="cache"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static T Get<T>(ICacheManager<T> cache, string key)
    {
    return cache.Get<T>(key);
    }
    
    /// <summary>
    /// 生成缓存key
    /// </summary>
    /// <param name="dtoName">当前缓存的dto</param>
    /// <returns></returns>
    public static string GetCacheKey(string dtoName)
    {
    return GetCacheKey(dtoName, 0);
    }
    /// <summary>
    /// 生成缓存key
    /// </summary>
    /// <param name="dtoName">当前缓存的dto</param>
    /// <param name="pageIndex">缓存当前页索引</param>
    /// <returns></returns>
    public static string GetCacheKey(string dtoName, int pageIndex)
    {
    return GetCacheKey("DataCenter", dtoName, pageIndex);
    }
    /// <summary>
    /// 生成缓存key
    /// </summary>
    /// <param name="regionName">区域名称</param>
    /// <param name="dtoName">当前缓存的dto</param>
    /// <returns></returns>
    public static string GetCacheKey(string regionName, string dtoName)
    {
    return GetCacheKey(regionName, dtoName, 0);
    }
    /// <summary>
    /// 生成缓存key
    /// </summary>
    /// <param name="regionName">区域名称</param>
    /// <param name="dtoName">当前缓存的dto</param>
    /// <param name="pageIndex">缓存当前页索引</param>
    /// <returns></returns>
    public static string GetCacheKey(string regionName, string dtoName, int pageIndex)
    {
    return regionName + "_" + dtoName + "_" + pageIndex.ToString();
    }
    }
    }
    

      

  • 相关阅读:
    MYSQL新特性secure_file_priv对读写文件的影响
    weblogic反序列化漏洞CVE-2018-2628-批量检测脚本
    WebLogic WLS-WebServices组件反序列化漏洞—Linux可执行反弹一句话版本
    攻击溯源_一次断网事件的分析
    建立加密socks5转发的两种方法
    【原创】Flash XSS 挖掘
    【转载】SQL的注入类型
    【原创】批处理文本
    【转载】nmap 官方指南
    【转载】对抗 DDoS 攻击的 15 个方法
  • 原文地址:https://www.cnblogs.com/gylhaut/p/9118120.html
Copyright © 2011-2022 走看看