Пример #1
0
        public T Get <T>(string key, out IConcurrencyHandle handle) where T : new()
        {
            var retryPolicy = new LockRetryPolicy();
            DataCacheLockHandle lockHandle = null;
            object item = null;

            HandleBadDataCacheBug(() => retryPolicy.Handle(() =>
            {
                item = _dataCache.GetAndLock(key, TimeSpan.FromSeconds(1), out lockHandle, true);
            }));

            handle = new AzureLockHandle
            {
                Instance = lockHandle
            };
            return((T)item);
        }
Пример #2
0
        public T Get <T>(string key, out IConcurrencyHandle handle)
            where T : class, new()
        {
            using (MiniProfiler.StepStatic("RedisCache_GetConcurrent_" + key))
            {
                var token       = this.GetType().ToString();
                var retryPolicy = new LockRetryPolicy();

                var result = TryOnClient(c =>
                {
                    T item = default(T);
                    retryPolicy.Handle(() =>
                    {
                        if (c.Database.LockTake(key, token, TimeSpan.FromSeconds(1)))
                        {
                            try
                            {
                                object obj = c.Get <object>(key);
                                item       = (T)obj;
                            }
                            finally
                            {
                                c.Database.LockRelease(key, token);
                            }
                        }
                    });
                    return(item);
                });

                handle = new RedisLockHandle
                {
                    Token = token
                };

                return(result);
            }
        }