Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Custom JQL cache

TothJ
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2011

I made a custom JQL function which runs a custom query - this query so runs 4 times since the getValues runs four times. So I use a cache (Google's MapMaker from com.google.common.collect in this case, but you can use your own too) which has the ability to set TimeToLive for the objects in the cache, and I set that to 1 min. Here is the code:

{code}
...
private ConcurrentMap<String, List> cache = null;

@Override
public void init(JqlFunctionModuleDescriptor moduleDescriptor) {
super.init(moduleDescriptor);
cache = new MapMaker().expiration(1, TimeUnit.MINUTES).makeMap();
}

...

@Override
public List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
List<QueryLiteral> literals = new LinkedList<QueryLiteral>();
if (!cache.containsKey(CACHE_KEY)) {
final Collection<Long> duplicatedIssues = getDuplicates();
for (Long issueID : duplicatedIssues) {
literals.add(new QueryLiteral(operand, issueID));
}
cache.put(CACHE_KEY, literals);
} else {
literals = cache.get(CACHE_KEY);
}
return literals;
}

...

{code}

Do you see any problems with this? Concurrency or any other way?

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Radu Dumitriu
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2011

Yes.

ConcurentMap's routines guarantees atomicity at the method level.

So, if you are doing:

if(!cache.containsKey()) {

//theoretically, between these two your key may disappear

cache.get(....);

} else {

//theoretically, between these two your key may be already into the map

cache.put(....)

}

The javadoc for ConcurrentHashMap states (see bolder fragment):

A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. This class obeys the same functional specification as {@link java.util.Hashtable}, and includes versions of methods corresponding to each method of <tt>Hashtable</tt>. However, even though all operations are thread-safe, retrieval operations do <em>not</em> entail locking, [.....]

HTH

TAGS
AUG Leaders

Atlassian Community Events