public static async Task <T> GetOrCreateAsync <T>(this ICacheAsync cache, string key, Func <Task <T> > func, TimeSpan expiresIn, CacheDurationType cacheDuration, bool forceUpdate = false) where T : class { var rtnObj = await cache.Get <T>(key); if (rtnObj != null && forceUpdate == false) { return(rtnObj); } var lockOn = LockCache.GetOrAdd(key, () => new AsyncLock()); using (await lockOn.LockAsync(TimeOut.AsyncLockTimeOut)) { rtnObj = await cache.Get <T>(key); if (rtnObj != null && forceUpdate == false) { return(rtnObj); } rtnObj = await func(); await cache.Set(key, rtnObj, expiresIn, cacheDuration); return(rtnObj); } }
/// <summary> /// 检查Redis中对应key的val是否是本机IP /// </summary> /// <param name="machineId"></param> /// <returns></returns> private async Task <bool> CheckIsLocalIp(string machineId) { string ip = await _cacheAsync.Get(Name + _datacenterId + machineId); return(LocalIp.Equals(ip)); }