Exemple #1
0
        public async Task <bool> SetAsync <T>(string key, Func <Task <T> > func, TimeSpan?expiresIn, When when, CommandFlags commandFlags)
        {
            var realKey = Wrapper.GetRealKey(key);

            using (var locker = new RedLockClient(HashHelper.GetHashedString(HashType.MD5, realKey), DependencyResolver.Current.ResolveService <ILogger <RedLockClient> >()))
            {
                if (await locker.TryLockAsync())
                {
                    return(await Wrapper.Database.StringSetAsync(realKey, Wrapper.Wrap(await func()), expiresIn?.Add(GetRandomCacheExpiry()), when,
                                                                 commandFlags));
                }
                Logger.LogInformation($"get lock failed,update cache fail,cache key:{realKey},current time:{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffffff}");
                return(false);
            }
        }
Exemple #2
0
        public async Task <bool> SetAsync <T>(string key, Func <T> func, TimeSpan?expiresIn, When when, CommandFlags commandFlags)
        {
            var realKey = Wrapper.GetRealKey(key);

            using (var locker = new RedLockClient(SecurityHelper.MD5(realKey), DependencyResolver.Current.ResolveService <ILogger <RedLockClient> >()))
            {
                if (await locker.TryLockAsync())
                {
                    return(await Wrapper.Database.StringSetAsync(realKey,
                                                                 Wrapper.Wrap(func()),
                                                                 (expiresIn ?? (RedisManager.RedisConfiguration.AllowNoExpiry ? null : (TimeSpan?)RedisManager.RedisConfiguration.MaxCacheExpiry))?.Add(GetRandomCacheExpiry()),
                                                                 when,
                                                                 commandFlags));
                }
                Logger.LogInformation($"get lock failed,update cache fail,cache key:{realKey},current time:{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffffff}");
                return(false);
            }
        }