How to implement JIRA plugin caching?


I'm new to JIRA development and I'm following this article on implementing plugin caching:

However, after installing my plugin and trying to test it, it does not behave as expected.

Each time I make a request to my plugin, it basically instantiates a new instance of my plugin.  The constructor is called for every request I make, so a new CacheManager is created and each request does the expensive retrieval of data from the database.  In other words, each request is like visiting my plugin for the very first time and effectively there is no caching because of this.

Why does my plugin not live beyond the scope of a single request?  Is this perhaps a plugin scope configuration that I need to configure?  I read the article about JIRA plugin lifecycle:  and also read articles about "JIRA plugin scope" but they're not talking about plugin scope as it relates to development.

Thanks in advance!


1 answer

1 accepted

0 votes
Accepted answer


You need to inject CacheManager. In this case CacheManager always stays the same. Also you can make your class as a bean adding Named annotation to the class. It is difficult to say for sure what is wrong with your code because you did not provide it.

Hi Alexey,

Thanks for your help!  I am injecting the CacheManager into my constructor and also I have just tried adding the "Named" annotation, however it still does not work.  Here is a simplified version of my plugin code:

public class MyPlugin extends JiraWebActionSupport
private static final Logger log = LoggerFactory.getLogger(MyPlugin.class);

private MyDBService db;

private final PermissionManager permissionMgr;

private final CacheManager cachMgr;

private final Cache myCache;
private List<MyField> myFields;

public MyPlugin(MyDBService mydb, PermissionManager permissionMgr, CacheManager cachMgr) {
this.db = mydb;
this.permissionMgr = permissionMgr;
this.cachMgr = cachMgr;
myCache = cachMgr.getCache(MyPlugin.class.getName() + "-MyCache.cache",
new MyDataLoader(),
new CacheSettingsBuilder().expireAfterAccess(50, TimeUnit.MINUTES).build());
log.debug("MyPlugin Constructor called!");

public String execute() throws Exception {

myFields = (List<MyField>) myCache.get("myData")
return super.execute();

private class MyDataLoader implements CacheLoader<String,List<MyField>> {

public List<MyField> load(@Nonnull String s) {

if(s.equals("myData")) {
log.debug("retrieved data from db!");
return db.getAllData();

return null;



You put @Named annotation and made atlas-clean and then atlas-run? And then every time you enter your webwork item you fall into the constructor? If so, make you private MyDataLoader class as a bean and pass it to the constructor.

Also I found this article

The point is that

myCache = cachMgr.getCache(MyPlugin.class.getName() + "-MyCache.cache",
new MyDataLoader(),
new CacheSettingsBuilder().expireAfterAccess(50, TimeUnit.MINUTES)

must be called once. So I would make a bean and make the myCache as a property of the bean. Then I would inject it into the constructor of your Webwork. 

But That would be true only if the constructor of your Webwork is called everytime you call the webwork. I need to check it. But I can not check it right now. But I ll try to check it tomorrow.

Hi Alexey, 

Thank you so much for your help.  I did some quick testing and it seems your suggestion will work:

  - create a separate dedicated 'cache bean' with the cache objects as properties (and instantiate the cache properties in this bean's constructor -- which will only be called once)

  - inject this dedicated 'cache bean' into my plugin bean's constructor

Even though my plugin bean constructor is called once per request (intended by design or not), the 'cache bean' constructor will only be invoked once when the bean is initially injected (so the cache objects stay the same and the cached data are reused). 

Thanks again, Alexey!

Best regards,


Oh btw, re: your question about atlas-clean atlas-run..

I was just making my changes, then doing:  atlas-mvn package

Then I'd uninstall and reinstall the newly compiled .jar into the JIRA instance and test it.

(But I also tried doing atlas-clean and then atlas-mvn package and it didn't fix my problem.. it seems since my plugin constructor is called many times, the only solution is to refactor out the instantiation of the cache objects into a separate bean and inject back into my plugin)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,356 views 14 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you