Class: Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/ip2location.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.



115
116
117
# File 'lib/logstash/filters/ip2location.rb', line 115

def cache
  @cache
end

.times_queriedObject (readonly)

Returns the value of attribute times_queried.



117
118
119
# File 'lib/logstash/filters/ip2location.rb', line 117

def times_queried
  @times_queried
end

.timestampsObject (readonly)

Returns the value of attribute timestamps.



116
117
118
# File 'lib/logstash/filters/ip2location.rb', line 116

def timestamps
  @timestamps
end

Class Method Details

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



149
150
151
152
153
# File 'lib/logstash/filters/ip2location.rb', line 149

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

.cache_full?(cache_size) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/logstash/filters/ip2location.rb', line 145

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

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/logstash/filters/ip2location.rb', line 120

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_roomObject



139
140
141
142
143
# File 'lib/logstash/filters/ip2location.rb', line 139

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

.synchronize(&block) ⇒ Object



155
156
157
# File 'lib/logstash/filters/ip2location.rb', line 155

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

.too_old?(ip) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/logstash/filters/ip2location.rb', line 135

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