Module: ExceptionsBegone::Cache

Defined in:
lib/exceptions_begone/exceptions_support/cache.rb

Constant Summary collapse

HOURLY_SEND_LIMIT =
500
TTL =
60

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(target) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/exceptions_begone/exceptions_support/cache.rb', line 8

def self.extended(target)
  target.instance_eval do
    class << self 
      alias_method :send_exception_without_cache, :send_exception
      alias_method :send_exception, :send_exception_with_cache
    end
  end
end

Instance Method Details

#hourly_send_limit_reached?Boolean



33
34
35
36
37
38
39
# File 'lib/exceptions_begone/exceptions_support/cache.rb', line 33

def hourly_send_limit_reached?
  key = "exceptions_sent_in_last_hour.#{Time.now.hour}"
  sent_number = Rails.cache.fetch(key, :expires_in => 1.hour, :raw => true) do
    0
  end
  Rails.cache.increment(key) > HOURLY_SEND_LIMIT
end

#save_signature_in_cache(exception_signature) ⇒ Object



41
42
43
# File 'lib/exceptions_begone/exceptions_support/cache.rb', line 41

def save_signature_in_cache(exception_signature)
  Rails.cache.write('last_exception_signature', exception_signature, :expires_in => TTL, :raw => true)
end

#send_exception_with_cache(exception, controller, request, connection_options = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/exceptions_begone/exceptions_support/cache.rb', line 17

def send_exception_with_cache(exception, controller, request, connection_options = {})
  exception_signature = Digest::MD5.hexdigest(exception.backtrace.join.to_s)
  return if skip_sending?(exception_signature)

  save_signature_in_cache(exception_signature)
  send_exception_without_cache(exception, controller, request, connection_options = {})
end

#signature_equal?(exception_signature) ⇒ Boolean



29
30
31
# File 'lib/exceptions_begone/exceptions_support/cache.rb', line 29

def signature_equal?(exception_signature)
  Rails.cache.read('last_exception_signature', :raw => true) == exception_signature
end

#skip_sending?(exception_signature) ⇒ Boolean



25
26
27
# File 'lib/exceptions_begone/exceptions_support/cache.rb', line 25

def skip_sending?(exception_signature)
   signature_equal?(exception_signature) || hourly_send_limit_reached?    
end