Hi!
I am currently developing my own plugin that utilizes active objects. In order to lessen the load on the DB, I want to utilize atlassian-cache for this. I have written code and it seems to work, but I’m unsure if I am thinking correctly. Is my implementation below correct?
public class PluginServiceImpl implements PluginService {
private final Cache<Integer, Plugin> cache;
private final CacheSettings cacheSettings = new CacheSettingsBuilder()
.expireAfterWrite(60, TimeUnit.MINUTES)
.maxEntries(100000)
.flushable()
.replicateAsynchronously()
.statisticsEnabled()
.build();
private static final Logger log = LoggerFactory.getLogger(PluginServiceImpl.class);
private final ActiveObjects ao;
@Inject
public PluginServiceImpl(@JiraImport ActiveObjects ao, @JiraImport CacheManager cacheManager) {
this.ao = checkNotNull(ao);
cache = cacheManager.getCache(PluginServiceImpl.class.getName() + ".cache", new PluginCacheLoader(), cacheSettings);
}
@Override
public Plugin createPlugin(Boolean active, String title, String description, String pluginIcon, String content) {
Transaction txn = Txn.begin();
try {
final Plugin plugin = ao.create(Plugin.class, new DBParam("CREATED_DATE", new java.util.Date()));
plugin.setActive(active);
plugin.setTitle(title);
plugin.setDescription(description);
plugin.setPluginIcon(pluginIcon);
plugin.setContent(content);
plugin.save();
cache.putIfAbsent(plugin.getID(), plugin);
txn.commit();
return plugin;
} finally {
txn.finallyRollbackIfNotCommitted();
}
}
@Override
public JSONObject allActivePluginsAsJSON() {
Transaction txn = Txn.begin();
try {
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
Plugin[] plugins = ao.find(Plugin.class, "ACTIVE = ?", "TRUE");
for (Plugin plugin : plugins) {
log.error("plugin id: " + plugin.getID());
log.error("cache result: " + cache.get(plugin.getID()).getContent());
try {
JSONObject tempJO = new JSONObject();
tempJO.put("id", plugin.getID());
tempJO.put("title", plugin.getTitle());
tempJO.put("description", plugin.getDescription());
tempJO.put("plugin_icon", plugin.getPluginIcon());
tempJO.put("content", plugin.getContent());
ja.put(tempJO);
} catch (JSONException ex) {
txn.finallyRollbackIfNotCommitted();
}
}
jo.put("activePlugins", ja);
txn.commit();
return jo;
} finally {
txn.finallyRollbackIfNotCommitted();
}
}
private class PluginCacheLoader implements CacheLoader<Integer, Plugin> {
@Override
@Nonnull
public Plugin load(@Nonnull Integer pluginId) {
return getPlugin(pluginId);
}
}
}
When I check the log, I can indeed see that it is able to find the active objects in the cache. But this also worked for Plugin objects that were created before the implementation, e.g. I haven’t used “cache.put” on them. Why is this so?
2022-12-16 15:30:33,273+0900 http-nio-2990-exec-18 ERROR admin 930x33819x1 ayk0wu 0:0:0:0:0:0:0:1 / [c.r.j.custompluginapp.ao.PluginServiceImpl] plugin id: 1
2022-12-16 15:30:33,279+0900 http-nio-2990-exec-18 ERROR admin 930x33819x1 ayk0wu 0:0:0:0:0:0:0:1 / [c.r.j.custompluginapp.ao.PluginServiceImpl] cache result: Testing 123
2022-12-16 15:30:33,280+0900 http-nio-2990-exec-18 ERROR admin 930x33819x1 ayk0wu 0:0:0:0:0:0:0:1 / [c.r.j.custompluginapp.ao.PluginServiceImpl] plugin id: 2
2022-12-16 15:30:33,282+0900 http-nio-2990-exec-18 ERROR admin 930x33819x1 ayk0wu 0:0:0:0:0:0:0:1 / [c.r.j.custompluginapp.ao.PluginServiceImpl] cache result: There
Thanks for the help!