How to cache a rest endpoint? (Groovy Scriptrunner)

Hello!

We have a script rest endpoint that each time that it's called get an information from an external database.

We would like to cache this function since that multiple times the result query returns the same value.

Looking in the internet we have found someways with Guava framework (https://github.com/google/guava) but we would like your help to see if is there any example to this need.

 

What do you recommend ?

Thanks!

1 answer

1 accepted

Not yet, this looks promissor. I'll give it a try!

@Gregor Kasmann,

I'm trying to to use the provided document.

It seems that each time that I call  CacheManager.getCache function the cache is erased and created a new one.

I'm testing using the Script Console (feature of Scriptrunner)

import com.atlassian.cache.Cache;
import com.atlassian.cache.CacheLoader;
import com.atlassian.cache.CacheManager;
import com.atlassian.cache.CacheSettingsBuilder;
import com.atlassian.util.concurrent.NotNull;
import com.atlassian.cache.CacheSettings;
import java.util.concurrent.TimeUnit;
import com.atlassian.jira.component.ComponentAccessor;

def CacheManager cacheManager = ComponentAccessor.getComponent(CacheManager.class);

def cache = cacheManager.getCache("com.foo.test.cache",
new ListPagesCacheLoader(),
new CacheSettingsBuilder().expireAfterAccess(5, TimeUnit.MINUTES as TimeUnit).build());

log.error cache.getKeys()
cache.get("hi")
log.error cache.getKeys()
cache.get("hi")
log.error cache.getKeys()

public class ListPagesCacheLoader implements CacheLoader<String, String> {
@Override public String load(@NotNull String value){
sleep(3000);
return value;
}
}

 image.png

@Gregor Kasmann,

It seems that the cache is being recreated each time that I call CacheManager#getCache

We need this cache stored in disk or memory to each time that the rest endpoint be called we still have the keys into it.

Do you have any tip ?

import com.atlassian.cache.Cache;
import com.atlassian.cache.CacheLoader;
import com.atlassian.cache.CacheManager;
import com.atlassian.cache.CacheSettingsBuilder;
import com.atlassian.util.concurrent.NotNull;
import com.atlassian.cache.CacheSettings;
import java.util.concurrent.TimeUnit;
import com.atlassian.jira.component.ComponentAccessor;

def CacheManager cacheManager = ComponentAccessor.getComponent(CacheManager.class);

def cache = cacheManager.getCache("com.foo.test.cache",
new ListPagesCacheLoader(),
new CacheSettingsBuilder().expireAfterAccess(5, TimeUnit.MINUTES as TimeUnit).build());

log.error cache.getKeys()
cache.get("hi")
log.error cache.getKeys()
cache.get("hi")
log.error cache.getKeys()

public class ListPagesCacheLoader implements CacheLoader<String, String> {
@Override
public String load(@NotNull String value){
sleep(3000);
return value;
}
}

image.png

As I can see on your example, cache works properly:

2017-10-24 10_25_13-How to cache a rest endpoint_ (Groovy Scriptrunner....png

Yes, it did work :)

But if I run the same script again the expected output should get data from cache. But the actual output is the same and I keep getting the first call with 3 seconds delay.

I'm afraid that when I call the getCache method the cache is being flushed.

def cache = cacheManager.getCache("com.foo.test.cache",
new ListPagesCacheLoader(),
new CacheSettingsBuilder().expireAfterAccess(5, TimeUnit.MINUTES as TimeUnit).build());

Do you know how can I define the cache in a way that I can run multiple times the same script without flush the cache's keys? Maybe this happens because I'm running within the script console, this behavior might be different when writing a P2 addon...

Thanks for your help!

Maybe the below picture helps to understand my issue:

image.png

Never mind, I've just tested with Script Endpoint and the cache worked just fine! :D

 

Thanks!!!!!!!!!!

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,129 views 13 19
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot