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();
}
}
}

  

posted @ 2018-05-31 18:01  流星小子  阅读(422)  评论(0编辑  收藏  举报