support clear_cache and cache_size methods in StringInterpolation, adjust specs

Fixes #4325
This commit is contained in:
Colin Surprenant 2015-12-09 15:10:53 -05:00 committed by Jordan Sissel
parent aaaa6de51a
commit 6a3e0215a3
5 changed files with 40 additions and 3 deletions

View file

@ -2,6 +2,7 @@
require "logstash/namespace"
require "logstash/json"
require "logstash/string_interpolation"
# transcient pipeline events for normal in-flow signaling as opposed to
# flow altering exceptions. for now having base classes is adequate and

View file

@ -0,0 +1,18 @@
# encoding: utf-8
module LogStash
module StringInterpolation
extend self
# clear the global compiled templates cache
def clear_cache
Java::ComLogstash::StringInterpolation.get_instance.clear_cache;
end
# @return [Fixnum] the compiled templates cache size
def cache_size
Java::ComLogstash::StringInterpolation.get_instance.cache_size;
end
end
end

View file

@ -29,6 +29,14 @@ public class StringInterpolation {
this.cache = new ConcurrentHashMap<>();
}
public void clearCache() {
this.cache.clear();
}
public int cacheSize() {
return this.cache.size();
}
public String evaluate(Event event, String template) throws IOException {
TemplateNode compiledTemplate = (TemplateNode) this.cache.get(template);

View file

@ -4,7 +4,7 @@ require "forwardable"
module LogStash
module StringInterpolation
extend self
extend self
# Floats outside of these upper and lower bounds are forcibly converted
# to scientific notation by Float#to_s
@ -27,6 +27,16 @@ module LogStash
compiled.evaluate(event)
end
# clear the global compiled templates cache
def clear_cache
CACHE.clear
end
# @return [Fixnum] the compiled templates cache size
def cache_size
CACHE.size
end
private
def not_cachable?(template)
template.index("%").nil?

View file

@ -506,11 +506,11 @@ describe LogStash::Event do
let(:event2) { LogStash::Event.new({ "host" => "bar", "message" => "foo"}) }
it "should cache only one template" do
LogStash::StringInterpolation::CACHE.clear
LogStash::StringInterpolation.clear_cache
expect {
event1.to_s
event2.to_s
}.to change { LogStash::StringInterpolation::CACHE.size }.by(1)
}.to change { LogStash::StringInterpolation.cache_size }.by(1)
end
it "return the string containing the timestamp, the host and the message" do