Class: Gitlab::Utils::RedisThrottle
- Inherits:
-
Object
- Object
- Gitlab::Utils::RedisThrottle
- Defined in:
- lib/gitlab/utils/redis_throttle.rb
Class Method Summary collapse
-
.execute_every(period, cache_key, skip_in_development: true) ⇒ Object, false
Executes a block of code at most once within a given time period using Redis for throttling.
Class Method Details
.execute_every(period, cache_key, skip_in_development: true) ⇒ Object, false
Executes a block of code at most once within a given time period using Redis for throttling. This is useful for scheduled tasks that should not execute too frequently.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gitlab/utils/redis_throttle.rb', line 21 def self.execute_every(period, cache_key, skip_in_development: true) return yield if skip_in_development && Rails.env.development? return yield unless period Gitlab::Redis::SharedState.with do |redis| key_set = redis.set(cache_key, 1, ex: period, nx: true) break false unless key_set yield end end |