Class: IP2ProxyCache

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/ip2proxy.rb

Constant Summary collapse

ONE_DAY_IN_SECONDS =
86_400

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject (readonly)

Returns the value of attribute cache.



146
147
148
# File 'lib/logstash/filters/ip2proxy.rb', line 146

def cache
  @cache
end

.times_queriedObject (readonly)

Returns the value of attribute times_queried.



148
149
150
# File 'lib/logstash/filters/ip2proxy.rb', line 148

def times_queried
  @times_queried
end

.timestampsObject (readonly)

Returns the value of attribute timestamps.



147
148
149
# File 'lib/logstash/filters/ip2proxy.rb', line 147

def timestamps
  @timestamps
end

Class Method Details

.cache_event(event, ip, filter) ⇒ Object Also known as: refresh_event



179
180
181
182
183
# File 'lib/logstash/filters/ip2proxy.rb', line 179

def cache_event(event, ip, filter)
  filter.handleEvent(event)
  cache[ip] = event
  timestamps[ip] = Time.now
end

.cache_full?(cache_size) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/logstash/filters/ip2proxy.rb', line 175

def cache_full?(cache_size)
  cache.size >= cache_size
end

.find(event, ip, filter, cache_size) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/logstash/filters/ip2proxy.rb', line 150

def find(event, ip, filter, cache_size)
  synchronize do
    if cache.has_key?(ip)
      refresh_event(event, ip, filter) 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_roomObject



169
170
171
172
173
# File 'lib/logstash/filters/ip2proxy.rb', line 169

def make_room
  key = times_queried.delete_least_used
  cache.delete(key)
  timestamps.delete(key)
end

.synchronize(&block) ⇒ Object



185
186
187
# File 'lib/logstash/filters/ip2proxy.rb', line 185

def synchronize(&block)
  @mutex.synchronize(&block)
end

.too_old?(ip) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/logstash/filters/ip2proxy.rb', line 165

def too_old?(ip)
  timestamps[ip] < Time.now - ONE_DAY_IN_SECONDS
end