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?
Community moderators have prevented the ability to post new answers.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.