How do I cache data in a plugin?
Confluence includes a caching API,
atlassian-cache, which should be used instead of custom cache solutions. The provided API ensures:
- proper expiry of cache data (default expiry: 1 hour)
- enables monitoring of cache usage by administrators
- functions correctly in a Confluence cluster.
The remainder of this document describes how to use the
Below is a short example of some code using the
To use the Atlassian Cache API, you first need to get a CacheManager injected into your component (macro, action, etc). You do this by adding a setter or constructor parameter to your component, depending on whether you are using setter-based or constructor-based dependency injection.
To retrieve a cache from the cache manager, use a cache key which is unique to your plugin. We recommend using the fully qualified name of the class which uses the cache, plus a name which describes the contents of the cache.
The returned Cache has an API very similar to Java's
Map. You can call
put(Object, Object) to store a value in the cache, and
get(Object) to look up a previously stored value.
In a single instance of Confluence, you can store any objects in the cache. In a clustered instance, you can only store keys and values which implement
If you don't specify a cache configuration explicitly then it is determined by Confluence by default, with the ability for the Confluence administrator to change the settings at runtime. The default expiry is one hour and the cache will store up to 1000 items. The least-recently used items will be automatically expired or removed if space is needed for new items. However it is preferable to create a cache specifically configured to the plugin requirements.
You should always try and set an expiry policy on the caches you create in order to avoid consuming excessive amounts of memory. For most applications a timed base expiry is most appropriate. It is usually very difficult to determine an appropriate number of cache entries that suits clients at all scales.