Class: Cache
- Inherits:
-
Object
- Object
- Cache
- Defined in:
- lib/logstash/filters/ip2location.rb
Constant Summary collapse
- ONE_DAY_IN_SECONDS =
86_400
Class Attribute Summary collapse
-
.cache ⇒ Object
readonly
Returns the value of attribute cache.
-
.times_queried ⇒ Object
readonly
Returns the value of attribute times_queried.
-
.timestamps ⇒ Object
readonly
Returns the value of attribute timestamps.
Class Method Summary collapse
- .cache_event(event, ip, filter) ⇒ Object (also: refresh_event)
- .cache_full?(cache_size) ⇒ Boolean
- .find(event, ip, filter, cache_size) ⇒ Object
- .make_room ⇒ Object
- .synchronize(&block) ⇒ Object
- .too_old?(ip) ⇒ Boolean
Class Attribute Details
.cache ⇒ Object (readonly)
Returns the value of attribute cache.
114 115 116 |
# File 'lib/logstash/filters/ip2location.rb', line 114 def cache @cache end |
.times_queried ⇒ Object (readonly)
Returns the value of attribute times_queried.
116 117 118 |
# File 'lib/logstash/filters/ip2location.rb', line 116 def times_queried @times_queried end |
.timestamps ⇒ Object (readonly)
Returns the value of attribute timestamps.
115 116 117 |
# File 'lib/logstash/filters/ip2location.rb', line 115 def @timestamps end |
Class Method Details
.cache_event(event, ip, filter) ⇒ Object Also known as: refresh_event
148 149 150 151 152 |
# File 'lib/logstash/filters/ip2location.rb', line 148 def cache_event(event, ip, filter) filter.handleEvent(event) cache[ip] = event [ip] = Time.now end |
.cache_full?(cache_size) ⇒ Boolean
144 145 146 |
# File 'lib/logstash/filters/ip2location.rb', line 144 def cache_full?(cache_size) cache.size >= cache_size end |
.find(event, ip, filter, cache_size) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/logstash/filters/ip2location.rb', line 119 def find(event, ip, filter, cache_size) synchronize do if cache.has_key?(ip) refresh_event(ip) if too_old?(ip) else if cache_full?(cache_size) make_room end cache_event(event, ip, filter) end times_queried.increment(ip) cache[ip] end end |
.make_room ⇒ Object
138 139 140 141 142 |
# File 'lib/logstash/filters/ip2location.rb', line 138 def make_room key = times_queried.delete_least_used cache.delete(key) .delete(key) end |
.synchronize(&block) ⇒ Object
154 155 156 |
# File 'lib/logstash/filters/ip2location.rb', line 154 def synchronize(&block) @mutex.synchronize(&block) end |
.too_old?(ip) ⇒ Boolean
134 135 136 |
# File 'lib/logstash/filters/ip2location.rb', line 134 def too_old?(ip) [ip] < Time.now - ONE_DAY_IN_SECONDS end |