Exemplo n.º 1
0
        public virtual bool TryIncrement(string name, RateLimitOptions options)
        {
            var entity = Get(name);

            if (entity == null)
            {
                entity = new RateEntity
                {
                    Name      = name,
                    Count     = 1,
                    ExpiresAt = CreateExpireTime(options)
                };
                Save(entity);
                return(true);
            }

            if (entity.ExpiresAt < DateTime.Now)
            {
                entity.Count     = 1;
                entity.ExpiresAt = CreateExpireTime(options);
                Save(entity);
                return(true);
            }

            if (entity.Count < options.Limit)
            {
                entity.Count++;
                Save(entity);
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
 protected abstract void Save(RateEntity entity);