KB Article #177865

Finding the contents of ehCache

Problem

How can you see what's stored inside an API Gateway ehCache?


Resolution

Use a script, like the following Nashorn syntax JavaScript:


//# sorceURL=Examine ehCache
var script_name = "Examine ehCache";
// This script prints the contents of a particular ehCache to the trace at INFO level.
// It also prints out the current message.key to help debug item not found errors.
// Change the variable cacheName to match the cache you want to examine. For example:
// var cacheName = "OAuth Client Access Token Cache";
var imp = new JavaImporter(com.vordel.trace, com.vordel.circuit, com.vordel.circuit.cache);
with (imp) {
    var cacheName = "YOUR CACHE NAME";  // TODO - change to an actual cache name.
    function invoke(msg) {
        try {
            Trace.info("Listing items in cache:");
            Trace.info("The message.key value is " + msg.get("message.key"));
            var cache = CacheContainer.getInstance().getCache(cacheName);
            var keys = cache.getKeys();
            Trace.info("There are " + keys.size() + " items in cache");
            for (var i = 0; i < keys.size(); i++) {
                var k = keys.get(i);
                Trace.info(k + " = " + cache.get(k));
            }
        } catch (e) {
            Trace.error(script_name + " error: " + e.stack);
            throw new CircuitAbortException(e); // Used to follow the abort path on error.
        }
        return true;
    }
};


Sample output:


DEBUG    3/30/21, 17:27:45.777    run filter [Test Script] {
INFO    3/30/21, 17:27:45.931            Listing items in cache:
INFO    3/30/21, 17:27:45.933            The message.key value is ljJObQK7QKy5DnZ+cAqBpGC28lY=
INFO    3/30/21, 17:27:46.104            There are 1 items in cache
INFO    3/30/21, 17:27:46.477            ljJObQK7QKy5DnZ+cAqBpGC28lY= = [ key = ljJObQK7QKy5DnZ+cAqBpGC28lY=, value=com.vordel.circuit.cache.BodySerializer@45a42040, version=1, hitCount=1, CreationTime = 1617150465777, LastAccessTime = 1617150466437 ]
DEBUG    3/30/21, 17:27:46.477            Return from script is: true
DEBUG    3/30/21, 17:27:46.477            ScriptProcessor.invoke: finished with status true
DEBUG    3/30/21, 17:27:46.477    } = 1, filter [Test Script]
DEBUG    3/30/21, 17:27:46.477    Filter [Test Script] completes in 700 milliseconds.