Class: VelocityCheck
- Inherits:
-
Object
- Object
- VelocityCheck
- Defined in:
- lib/velocity_check.rb,
lib/velocity_check/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
- #check(key) ⇒ Object
-
#initialize(options) ⇒ VelocityCheck
constructor
A new instance of VelocityCheck.
Constructor Details
#initialize(options) ⇒ VelocityCheck
Returns a new instance of VelocityCheck.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/velocity_check.rb', line 4 def initialize() raise ArgumentError, ":name is required" unless [:name] raise ArgumentError, ":name must only contain letters, numbers, and underscore" unless [:name] =~ /\A\w+\z/ raise ArgumentError, ":limit is required" unless [:limit] raise ArgumentError, ":time_period is required" unless [:time_period] raise ArgumentError, ":client is required" unless [:client] = [:name, :limit, :time_period, :client] = .keys - raise ArgumentError, "Unknown options: #{.map(&:to_s).sort.join(', ')}" unless .empty? @name = [:name] @client = [:client] @time_period = [:time_period] @limit = [:limit] end |
Instance Method Details
#check(key) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/velocity_check.rb', line 21 def check(key) full_key = "#{@name}_#{key}" # It would nice if we could incr + touch, but touch is buggy # in most versions of memcached (and doesn't even exist in # debian squeeze's version). previous = @client.get(full_key) || 0 @client.set(full_key, previous + 1, @time_period) previous >= @limit rescue Dalli::RingError false end |