Class: Contrast::Agent::Protect::Rule::InputClassification::LRUCache
- Inherits:
-
Utils::LRUCache
- Object
- Utils::LRUCache
- Contrast::Agent::Protect::Rule::InputClassification::LRUCache
- Includes:
- Utils, Components::Logger::InstanceMethods
- Defined in:
- lib/contrast/agent/protect/rule/input_classification/lru_cache.rb
Overview
This Class with store the input classification results for a given user input. Among the most used inputs are the headers values for session_id and path values.
Constant Summary collapse
- RESULTS_CAPACITY =
Protect rules will always be fixed number, on other hand the number of inputs will grow, we need to limit the number of inputs to be cached.
20
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the cache and statistics.
-
#clear_statistics ⇒ Object
Clear only the statistics.
-
#empty? ⇒ Boolean
Check if the cache is empty.
-
#initialize(capacity = 10) ⇒ LRUCache
constructor
Initialize the LRU Cache.
-
#lookout(rule_id, input, input_type, request) ⇒ Contrast::Agent::Protect::InputClassification::CachedResult?
Check if the input is cached and returns it if so and record the statistics for the required input.
- #mutex ⇒ Object
-
#save(rule_id, result, request) ⇒ Object
Save the input classification result for a given user input.
-
#statistics ⇒ Contrast::Agent::Protect::Rule::InputClassification::Statistics
Capacity of the statistics will always be the number of rule_id Protect supports.
- #with_mutex(&block) ⇒ Object
Methods included from Utils
Methods included from Components::Logger::InstanceMethods
Methods inherited from Utils::LRUCache
#[], #[]=, #key?, #keys, #values
Constructor Details
#initialize(capacity = 10) ⇒ LRUCache
Initialize the LRU Cache.
instance it will never reach outside of the number of supported Protect rules.
31 32 33 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 31 def initialize capacity = 10 super(capacity) end |
Instance Method Details
#clear ⇒ Object
Clear the cache and statistics.
53 54 55 56 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 53 def clear with_mutex { @cache.clear } clear_statistics end |
#clear_statistics ⇒ Object
Clear only the statistics.
59 60 61 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 59 def clear_statistics with_mutex { statistics.send(:clear) } end |
#empty? ⇒ Boolean
Check if the cache is empty.
66 67 68 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 66 def empty? Contrast::Utils::DuckUtils.empty_duck?(@cache) end |
#lookout(rule_id, input, input_type, request) ⇒ Contrast::Agent::Protect::InputClassification::CachedResult?
Check if the input is cached and returns it if so and record the statistics for the required input.
77 78 79 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 77 def lookout rule_id, input, input_type, request with_mutex { _loockout(rule_id, input, input_type, request) } end |
#mutex ⇒ Object
35 36 37 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 35 def mutex @_mutex ||= Mutex.new end |
#save(rule_id, result, request) ⇒ Object
Save the input classification result for a given user input.
87 88 89 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 87 def save rule_id, result, request with_mutex { _save(rule_id, result, request) } end |
#statistics ⇒ Contrast::Agent::Protect::Rule::InputClassification::Statistics
Capacity of the statistics will always be the number of rule_id Protect supports.
48 49 50 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 48 def statistics @_statistics ||= Contrast::Agent::Protect::Rule::InputClassification::Statistics.new end |
#with_mutex(&block) ⇒ Object
39 40 41 42 43 |
# File 'lib/contrast/agent/protect/rule/input_classification/lru_cache.rb', line 39 def with_mutex &block return_type = mutex.synchronize(&block) ensure return_type end |