public void SetLock(IThrottleKey key, Limiter limiter)
        {
            string throttleId = CreateThrottleKey(key, limiter);
            _store.Remove(throttleId);

            string lockId = CreateLockKey(key, limiter);
            DateTime expiration = CurrentDate().Add(limiter.LockDuration.Value);
            _store.Set(lockId, true, expiration);
        }
Exemplo n.º 2
0
        private List <object> CreateBaseKeyValues(IThrottleKey key, Limiter limiter)
        {
            List <object> values = key.Values.ToList();

            if (PolicyIdentityValues != null && PolicyIdentityValues.Length > 0)
            {
                values.InsertRange(0, PolicyIdentityValues);
            }

            return(values);
        }
        public string CreateLockKey(IThrottleKey key, Limiter limiter)
        {
            List<object> values = CreateBaseKeyValues(key, limiter);

            string lockKeySuffix = TimeSpanToFriendlyString(limiter.LockDuration.Value);
            values.Add("lock");
            values.Add(lockKeySuffix);

            string id = string.Join(":", values);
            return id;
        }
Exemplo n.º 4
0
        public void SetLock(IThrottleKey key, Limiter limiter)
        {
            string throttleId = CreateThrottleKey(key, limiter);

            _store.Remove(throttleId);

            string   lockId     = CreateLockKey(key, limiter);
            DateTime expiration = CurrentDate().Add(limiter.LockDuration.Value);

            _store.Set(lockId, true, expiration);
        }
Exemplo n.º 5
0
        private long?GetLimiterCount(TimeSpan span)
        {
            Limiter item   = Limiters.FirstOrDefault(l => l.Period == span);
            long?   result = null;

            if (item != null)
            {
                result = item.Count;
            }

            return(result);
        }
Exemplo n.º 6
0
        public string CreateLockKey(IThrottleKey key, Limiter limiter)
        {
            List <object> values = CreateBaseKeyValues(key, limiter);

            string lockKeySuffix = TimeSpanToFriendlyString(limiter.LockDuration.Value);

            values.Add("lock");
            values.Add(lockKeySuffix);

            string id = string.Join(":", values);

            return(id);
        }
Exemplo n.º 7
0
        public long?GetThrottleCount(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            var cacheItem = _store.Get(id) as ThrottleCacheItem;

            if (cacheItem != null)
            {
                return(cacheItem.Count);
            }

            return(null);
        }
        public string CreateThrottleKey(IThrottleKey key, Limiter limiter)
        {
            List<object> values = CreateBaseKeyValues(key, limiter);

            string countKey = TimeSpanToFriendlyString(limiter.Period);
            values.Add(countKey);

            // Using the Unix timestamp to the key allows for better
            // precision when querying a key from Redis
            if (limiter.Period.TotalSeconds == 1)
                values.Add(GetUnixTimestamp());

            string id = string.Join(":", values);
            return id;
        }
Exemplo n.º 9
0
        public string CreateThrottleKey(IThrottleKey key, Limiter limiter)
        {
            List <object> values = CreateBaseKeyValues(key, limiter);

            string countKey = TimeSpanToFriendlyString(limiter.Period);

            values.Add(countKey);

            // Using the Unix timestamp to the key allows for better
            // precision when querying a key from Redis
            if (limiter.Period.TotalSeconds == 1)
            {
                values.Add(GetUnixTimestamp());
            }

            string id = string.Join(":", values);

            return(id);
        }
Exemplo n.º 10
0
        public void AddOrIncrementWithExpiration(IThrottleKey key, Limiter limiter)
        {
            string id        = CreateThrottleKey(key, limiter);
            var    cacheItem = _store.Get(id) as ThrottleCacheItem;

            if (cacheItem != null)
            {
                cacheItem.Count = cacheItem.Count + 1;
            }
            else
            {
                cacheItem = new ThrottleCacheItem()
                {
                    Count      = 1,
                    Expiration = CurrentDate().Add(limiter.Period)
                };
            }

            _store.Set(id, cacheItem, cacheItem.Expiration);
        }
Exemplo n.º 11
0
        public void AddOrIncrementWithExpiration(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);
            var cacheItem = _store.Get(id) as ThrottleCacheItem;

            if (cacheItem != null)
            {
                cacheItem.Count = cacheItem.Count + 1;
            }
            else
            {
                cacheItem = new ThrottleCacheItem()
                {
                    Count = 1,
                    Expiration = CurrentDate().Add(limiter.Period)
                };
            }

            _store.Set(id, cacheItem, cacheItem.Expiration);
        }
Exemplo n.º 12
0
        private void SetLimiter(TimeSpan span, long?count)
        {
            Limiter item = Limiters.FirstOrDefault(l => l.Period == span);

            if (item != null)
            {
                _limits.Remove(item);
            }

            if (!count.HasValue)
            {
                return;
            }

            item = new Limiter
            {
                Count  = count.Value,
                Period = span
            };

            _limits.Add(item);
        }
 public long? GetThrottleCount(IThrottleKey key, Limiter limiter)
 {
     string id = CreateThrottleKey(key, limiter);
     return _store.Get(id) as long?;
 }
Exemplo n.º 14
0
        public void RemoveThrottle(IThrottleKey key, Limiter limiter)
        {
            string lockId = CreateThrottleKey(key, limiter);

            _store.Remove(lockId);
        }
Exemplo n.º 15
0
        public bool LockExists(IThrottleKey key, Limiter limiter)
        {
            string lockId = CreateLockKey(key, limiter);

            return(_store.Contains(lockId));
        }
Exemplo n.º 16
0
 public bool LockExists(IThrottleKey key, Limiter limiter)
 {
     string lockId = CreateLockKey(key, limiter);
     return _store.Contains(lockId);
 }
Exemplo n.º 17
0
 public void RemoveThrottle(IThrottleKey key, Limiter limiter)
 {
     string lockId = CreateThrottleKey(key, limiter);
     _store.Remove(lockId);
 }
Exemplo n.º 18
0
        private void SetLimiter(TimeSpan span, long? count)
        {
            Limiter item = Limiters.FirstOrDefault(l => l.Period == span);
            if (item != null)
                _limits.Remove(item);

            if (!count.HasValue)
                return;

            item = new Limiter
            {
                Count = count.Value,
                Period = span
            };

            _limits.Add(item);
        }
        public bool LockExists(IThrottleKey key, Limiter limiter)
        {
            string lockId = CreateLockKey(key, limiter);

            return(_store.TryGetValue(lockId, out _));
        }
Exemplo n.º 20
0
        private List<object> CreateBaseKeyValues(IThrottleKey key, Limiter limiter)
        {
            List<object> values = key.Values.ToList();
            if (PolicyIdentityValues != null && PolicyIdentityValues.Length > 0)
                values.InsertRange(0, PolicyIdentityValues);

            return values;
        }
Exemplo n.º 21
0
        public long? GetThrottleCount(IThrottleKey key, Limiter limiter)
        {
            string id = CreateThrottleKey(key, limiter);

            var cacheItem = _store.Get(id) as ThrottleCacheItem;
            if (cacheItem != null)
            {
                return cacheItem.Count;
            }

            return null;
        }