using MyCode.Project.Infrastructure.Cache;
using MyCode.Project.Infrastructure.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyCode.Project.Infrastructure.Common
{
public static class CacheHelper
{
#region GetCacheValue(得到缓存中的值)
///
/// 得到缓存中的值,用法: CacheHelper.GetCacheValue("id",() => IdHelper.GetNewId());
///
/// 要返回的对象实体
/// 缓存key
/// 执行的函数
/// 多少秒,默认60
///
public static T GetCacheValue(string cacheKey,
Func func,
int cacheSecond = 60)
{
if (string.IsNullOrWhiteSpace(cacheKey)) { throw new BaseException("缓存key参数cacheKey不能为空"); }
var cache = new SystemCache();
//该Token已经存在缓存了
if (cache.Exists(cacheKey)) { return cache.Get(cacheKey); }
var returnValue = func.Invoke();
if (returnValue != null) { cache.Set(cacheKey, returnValue, new TimeSpan(0, 0, cacheSecond)); }
return returnValue;
}
#endregion
}
}