Esempio n. 1
0
        public static void CheckFlood(string key, int duration = CheckIntervalDurationSeconds, int count = MaxCountInInterval)
        {
            var checkTime = DateTime.UtcNow.TotalSeconds() / duration;

            Dict.AddOrUpdate(key, new FloodCheck {
                Time = checkTime, Count = 1
            }, (l, check) =>
            {
                lock (check)
                {
                    if (check.Time == checkTime)
                    {
                        if (++check.Count > count)
                        {
                            throw new HttpException(403, $"Too fast, wait {DateTime.UtcNow.TotalSeconds() - checkTime * duration + 1} sec pls");
                        }
                    }
                    else
                    {
                        check.Time  = checkTime;
                        check.Count = 1;
                    }
                    return(check);
                }
            });
        }
Esempio n. 2
0
        public void Set(string key, object value, TimeSpan?expiry = null)
        {
            var val = new CacheValue(value)
            {
                Expiry = expiry
            };

            Data.AddOrUpdate(key, val, (k, v) =>
            {
                v.Value      = val.Value;
                v.UpdateTime = DateTime.Now;
                return(v);
            });
        }
Esempio n. 3
0
        public T TryAddValue(string key, T p)
        {
            SpoolItem <T> item = Dict.AddOrUpdate(key, item => ToSpoolItem(p), (item, old) => ToSpoolItem(p));

            return(item.Data);
        }