public override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) { CacheValue <T> cacheValue = _memoryProvider.Get <T>(cacheKey); if (cacheValue.IsNull) { cacheValue = _redisProvider.Get <T>(cacheKey); if (cacheValue.HasValue) { var expration = _redisProvider.GetExpiration(cacheKey); if (expration != null && expration != TimeSpan.Zero) { _memoryProvider.Set <T>(cacheKey, cacheValue.Value, expration); } } } if (cacheValue.HasValue) { return(cacheValue); } T data = dataRetriever(); if (data != null) { _memoryProvider.Set <T>(cacheKey, data, expiration); _redisProvider.Set <T>(cacheKey, data, expiration); return(new CacheValue <T>(data, true)); } return(new CacheValue <T>(data, false)); }
public async Task <string> GetToken() { string res = _cacheFactory.Get("accessToken") as string; if (string.IsNullOrEmpty(res)) { var response = await Client.RequestTokenAsync(new TokenRequest { Address = _appSettings.Value.MsApplication.url + _appSettings.Value.MsApplication.tokenurl, ClientId = _appSettings.Value.MsApplication.client_id, ClientSecret = _appSettings.Value.MsApplication.client_secret, GrantType = _appSettings.Value.MsApplication.grant_type }); if (response.AccessToken != null) { _cacheFactory.Set("accessToken", response.AccessToken, new TimeSpan(0, 100, 0)); return(response.AccessToken); } else { return(null); } } else { return(res); } }
public async Task <string> GetToken() { string res = _cacheFactory.Get("accessToken") as string; if (string.IsNullOrEmpty(res)) { var response = await Client.RequestTokenAsync(new TokenRequest { Address = Options.Address, ClientId = Options.ClientId, ClientSecret = Options.ClientSecret, GrantType = Options.GrantType }); if (response.AccessToken != null) { _cacheFactory.Set("accessToken", response.AccessToken, new TimeSpan(0, 100, 0)); return(response.AccessToken); } else { return(response.Error); } } else { return(res); } }
//Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义 public override void Intercept(IInvocation invocation) { var method = invocation.MethodInvocationTarget ?? invocation.Method; //对当前方法的特性验证 //如果需要验证 if (method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) is CachingAttribute qCachingAttribute) { //获取自定义缓存键 var cacheKey = CustomCacheKey(invocation); //根据key获取相应的缓存值 var cacheValue = _cache.Get(cacheKey); if (cacheValue != null) { //将当前获取到的缓存值,赋值给当前执行方法 invocation.ReturnValue = cacheValue; return; } //去执行当前的方法 invocation.Proceed(); //存入缓存 if (!string.IsNullOrWhiteSpace(cacheKey)) { _cache.Set(cacheKey, invocation.ReturnValue); } } else { invocation.Proceed();//直接执行被拦截方法 } }
/// <summary> /// Returns the cached result if exists, or will execute the query and add the result in cache. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="slidingExpiration"></param> /// <returns></returns> public static IEnumerable <T> FromCache <T>(this IQueryable <T> query, TimeSpan expiration) { string key = query.GetKey(); ICachingProvider provider = ProviderManager.Provider; provider.Get(out IEnumerable <T> result, key, expiration, query); return(result); }
public async Task GetSetTest() { var c = ContainerHelper.Builder(); ICachingProvider _cp = c.Resolve <ICachingProvider>(); var val = await _cp.Get("a", async() => { await Task.CompletedTask; return("aab"); }); Assert.AreEqual(val, "aab"); }
public void HandleDeivce(Object obj) { TcpClient client = (TcpClient)obj; var stream = client.GetStream(); Byte[] bytes = new Byte[2048]; int i; try { while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { BinaryFormatter binaryFormatter = new BinaryFormatter(); ICacheResponse response; using (MemoryStream ms = new MemoryStream(bytes)) { ICacheRequest cacheRequest = (ICacheRequest)binaryFormatter.Deserialize(ms); if (cacheRequest is ISetCacheRequest setCacheRequest) { _cachingProvider.Set(setCacheRequest.Key, setCacheRequest.Value, new TimeSpan(0, 0, setCacheRequest.ExpiresIn)); response = new SetCacheResponse(true); } else if (cacheRequest is IGetCacheRequest getCacheRequest) { response = _cachingProvider.Get(getCacheRequest.Key); } else { throw new InvalidOperationException("Unknown action!"); } } using (MemoryStream responseMs = new MemoryStream()) { binaryFormatter.Serialize(responseMs, response); byte[] outputBytes = responseMs.ToArray(); stream.Write(outputBytes, 0, outputBytes.Length); } } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); client.Close(); } }
/// <summary> /// 从cache中取出数据 /// </summary> /// <param name="invocation"></param> /// <param name="attribute"></param> private void ProceedCaching(IInvocation invocation, QCachingAttribute attribute) { var cacheKey = GenerateCacheKey(invocation); var cacheValue = _cacheProvider.Get(cacheKey); if (cacheValue != null) { invocation.ReturnValue = cacheValue; return; } invocation.Proceed(); if (!string.IsNullOrWhiteSpace(cacheKey)) { _cacheProvider.Set(cacheKey, invocation.ReturnValue, TimeSpan.FromSeconds(attribute.AbsoluteExpiration)); } }
public IActionResult RedisGet(string key) { var value = _redisProvider.Get <string>(key); return(Json(value)); }
public IActionResult MemoryGet(string key) { var value = _memoryProvider.Get <string>(key); return(Json(value)); }
/// <summary> /// 获取缓存 /// </summary> /// <returns></returns> public object Get() { var res = _cacheFactory.Get(this.Key); return(res); }