public VideosModel GetVideos(string searchValue) { if (string.IsNullOrEmpty(searchValue)) { return(null); } var cache = MemoryCacheUtil.GetCacheItem <VideosModel>("PixabayApi_videos_" + searchValue); if (cache != null) { return(cache); } var url = string.Format("https://pixabay.com/api/videos/?key={0}&q={1}", key, searchValue); try { logger.Debug($"Get:{url}"); var result = Get(url); var resultJson = JsonConvert.DeserializeObject <VideosModel>(result); MemoryCacheUtil.Set("PixabayApi_videos_" + searchValue, resultJson, 3600 * 24); return(resultJson); } catch (Exception ex) { logger.Error($"获取视频资源:{searchValue} 失败", ex); throw ex; } }
/// <summary> /// 获取日志 /// </summary> /// <param name="name"></param> /// <returns></returns> public static ILog GetLogger(string name) { if (string.IsNullOrEmpty(name)) { return(null); } if (MemoryCacheUtil.Contains(name)) { return(MemoryCacheUtil.GetCacheItem <ILog>(name)); } else { lock (lockObject) { if (MemoryCacheUtil.Contains(name)) { return(MemoryCacheUtil.GetCacheItem <ILog>(name)); } var logger = CreateLoggerInstance(name); var now = DateTime.Now.AddDays(1); var expireDate = new DateTime(now.Year, now.Month, now.Day); // 晚上0点过期 MemoryCacheUtil.Set(name, logger, expireDate); return(logger); } } }
public ImagesModel GetImages(string searchValue, int pageIndex, int pageSize) { if (string.IsNullOrEmpty(searchValue)) { return(null); } var cache = MemoryCacheUtil.GetCacheItem <ImagesModel>($"PixabayApi_Images_{searchValue}_{pageIndex}_{pageSize}"); if (cache != null) { return(cache); } var url = string.Format("https://pixabay.com/api/?key={0}&q={1}&image_type=photo&lang=zh&page={2}&per_page={3}", key, searchValue, pageIndex, pageSize); try { logger.Debug($"Get:{url}"); var result = Get(url); var resultJson = JsonConvert.DeserializeObject <ImagesModel>(result); // 缓存24小时 MemoryCacheUtil.Set($"PixabayApi_Images_{searchValue}_{pageIndex}_{pageSize}", resultJson, 3600 * 24); return(resultJson); } catch (Exception ex) { logger.Error($"获取图片资源:{searchValue} 失败", ex); throw ex; } }
public static T GetConfig <T>(string key) where T : class, new() { T retValue = new T(); var models = MemoryCacheUtil.Get <T>(CacheKeyConfig, nameof(retValue)); if (models == null) { var config = new ConfigurationBuilder() .AddInMemoryCollection() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .Build(); retValue = new ServiceCollection() .AddOptions() .Configure <T>(config.GetSection(key)) .BuildServiceProvider() .GetService <IOptions <T> >() .Value; MemoryCacheUtil.Set(CacheKeyConfig, retValue, TimeSpan.FromDays(30)); } else { retValue = models; } return(retValue); }
/// <summary> /// 缓存过期获取access_token /// </summary> public void SetAccessToken() { var getAccessTokenUrl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={Constants.WxAppId}&secret={Constants.WxSecret}"; var rep2 = HttpUtil.GetString(getAccessTokenUrl); var repObj2 = rep2.GetModel <Code2SessionRep>(); MemoryCacheUtil.Set(Constants.WxAccessToken, repObj2?.Access_Token ?? "", 120); }
/// <summary> /// 清除用户权限信息缓存 /// </summary> public static void Clear(IPersistBroker broker) { UserPrivliege.Clear(); ServiceContainer.ResolveAll <IRole>().Each(item => { (item as BasicRole).Broker = broker; item.ClearCache(); MemoryCacheUtil.RemoveCacheItem(item.GetRoleKey); MemoryCacheUtil.Set(item.GetRoleKey, new RolePrivilegeModel() { Role = item.GetSysRole(), Privileges = item.GetRolePrivilege() }, 3600 * 12); }); }
public static T Get <T>(string key, Func <T> fetch) { T result = MemoryCacheUtil.Get <T>(key); if (result == null) { result = fetch(); if (result != null) { MemoryCacheUtil.Set(key, result); } } return(result); }
public static IApplicationBuilder UseSysRole(this IApplicationBuilder app) { var roles = ServiceContainer.ResolveAll <IRole>(); var broker = PersistBrokerFactory.GetPersistBroker(); new SysRolePrivilegeService(broker).CreateRoleMissingPrivilege(); // 权限读取到缓存 roles.Each(item => MemoryCacheUtil.Set(item.GetRoleKey, new RolePrivilegeModel() { Role = item.GetSysRole(), Privileges = item.GetRolePrivilege() }, 3600 * 12)); return(app); }
public static bool Set(string key, DateTimeOffset?absoluteExpiration, object obj) { bool isok = false; if (obj != null) { if (!absoluteExpiration.HasValue) { absoluteExpiration = DateTimeOffset.Now.AddSeconds(3); } MemoryCacheUtil.Set(key, obj, absoluteExpiration.Value); isok = true; } return(isok); }
/// <summary> /// 刷新Token /// </summary> public static AccessTokenResponse RefreshToken() { var result = WeChatApi.GetAccessToken(_appid, _secret); var accessToken = new AccessTokenResponse() { AccessToken = result.AccessToken, Expire = result.Expire }; MemoryCacheUtil.RemoveCacheItem("AccessToken"); MemoryCacheUtil.Set("AccessToken", accessToken); var logger = LogFactory.GetLogger("wechat"); logger.Debug("获取微信access_token成功:" + accessToken.AccessToken); return(accessToken); }
public static T Get <T>(string key, TimeSpan?slidingExpiration, Func <T> fetch) { T result = MemoryCacheUtil.Get <T>(key); if (result == null) { result = fetch(); if (result != null) { if (!slidingExpiration.HasValue) { slidingExpiration = TimeSpan.FromMinutes(30); } MemoryCacheUtil.Set(key, result, slidingExpiration.Value); } } return(result); }
public static T Get <T>(string key, DateTimeOffset?absoluteExpiration, Func <T> fetch) { T result = MemoryCacheUtil.Get <T>(key); if (result == null) { result = fetch(); if (result != null) { if (!absoluteExpiration.HasValue) { absoluteExpiration = DateTimeOffset.Now.AddSeconds(3); } MemoryCacheUtil.Set(key, result, absoluteExpiration.Value); } } return(result); }
/// <summary> /// 非今日历史数据,滑动缓存 /// </summary> /// <param name="cacheKey"></param> /// <param name="periodDrawRecords"></param> /// <param name="slidingExpiration"></param> /// <returns></returns> public static string SetHistoryDrawInfoNoToday(string cacheKey, List <PeriodDrawRecordDto> periodDrawRecords, TimeSpan?slidingExpiration = null, bool isIgnoreNullAndZero = true) { if (!slidingExpiration.HasValue) { slidingExpiration = TimeSpan.FromMinutes(30); } string jsonHistoryDraw = string.Empty; if (isIgnoreNullAndZero) { jsonHistoryDraw = JsonUtil.SerializeFilterZeroAndNull(periodDrawRecords); } else { jsonHistoryDraw = JsonUtil.Serialize(periodDrawRecords); } MemoryCacheUtil.Set(cacheKey, jsonHistoryDraw, slidingExpiration.Value); return(jsonHistoryDraw); }
/// <summary> /// 今日历史数据缓存 /// </summary> /// <param name="cacheKey"></param> /// <param name="periodDrawRecords"></param> /// <param name="absoluteExpiration"></param> /// <returns></returns> public static string SetHistoryDrawInfoToday(string cacheKey, List <PeriodDrawRecordDto> periodDrawRecords, DateTimeOffset?absoluteExpiration = null, bool isIgnoreNullAndZero = true) { if (!absoluteExpiration.HasValue) { absoluteExpiration = DateTimeOffset.Now.AddSeconds(3); } string jsonHistoryDraw = string.Empty; if (isIgnoreNullAndZero) { jsonHistoryDraw = JsonUtil.SerializeFilterZeroAndNull(periodDrawRecords); } else { jsonHistoryDraw = JsonUtil.Serialize(periodDrawRecords); } MemoryCacheUtil.Set(cacheKey, jsonHistoryDraw, absoluteExpiration.Value); // 属于今日期次的缓存3秒 return(jsonHistoryDraw); }
/// <summary> /// 设置实时开奖信息 /// </summary> /// <param name="cacheKeyPre"></param> /// <param name="realTimeInfoDto"></param> /// <param name="absoluteExpiration"></param> public static string SetRealTimeDrawInfo(string cacheKeyPre, LotteryRealTimeInfoDto realTimeInfoDto, DateTimeOffset?absoluteExpiration = null, bool isIgnoreNullAndZero = true) { if (!absoluteExpiration.HasValue) { absoluteExpiration = DateTimeOffset.Now.AddSeconds(3); } string jsonRealTime = string.Empty; if (isIgnoreNullAndZero) { jsonRealTime = JsonUtil.SerializeFilterZeroAndNull(realTimeInfoDto); } else { jsonRealTime = JsonUtil.Serialize(realTimeInfoDto); } MemoryCacheUtil.Set(cacheKeyPre + "0", jsonRealTime, absoluteExpiration.Value); // 最新开奖的缓存3秒,以便及时取得最新开奖结果 return(jsonRealTime); }