Atlassian Cache 2 is a version of the Atlassian Cache API (atlassian-cache-api-2.x) that should be used within Atlassian products (initially Jira 6.2 and Confluence 5.5). The API is designed to be backwardly compatible with atlassian-cache-api-0.1.
The main interfaces in the API from an end-user perspective are:
CachedReference
to generate the referenced object.CachedReference
instances that may be configured using CacheSettings.Cache API supports three distinct types of cache:
For more information on how to specify the type of cache, see cache creation later on this page.
For populating the data in cache, the Cache API supports two styles:
CacheLoader
when the cache is created. This is the preferred style for new development.Any implementation of CacheLoader
that is associated with a nonlocal cache must not make calls to any other nonlocal
cache or CachedReference
.
This limitation also applies to any implementation of Supplier
that is associated with a non-local CachedReference
.
The Cache API supports two rules for eviction of values from cache, which are specified in the CacheSettings
:
To be precise, the rules are actually hints to the underlying cache provider that may or may not be followed. The user of the cache should be prepared to handle their values being evicted at any time.
When creating cache, you can provide CacheSettings
to control the configuration of cache, which are created
using the CacheSettingsBuilder
. See CacheSettingsBuilder for all the settings that can be specified.
When settings are not explicitly specified, the product specific default will be used. See each product's documentation for detailed information on how defaults are determined.
The following table describes the how the different cache types are created based on the two properties remote() and replicateViaCopy().
remote() | replicateViaCopy() | Cache Type Created |
---|---|---|
false | false | Local |
false | true | Local |
true | false | Hybrid |
true | true | Cluster |
When creating a hybrid cache, pay extra attention to the following points:
CacheLoader
with the cache using getCache(String, CacheLoader) or getCache(String, CacheLoader, CacheSettings)
methods when obtaining the cache. If CacheLoader
is not associated, and the put(K, V)
or
putIfAbsent(K, V) are used, then the cache will not operate correctly in a cluster, as each put operation
will invalidate local entries in all other nodes in the cluster.This section provides guidelines for developers wanting to share data stored in cache. Sharing can be between:
Local cache keys and values are stored by reference. Therefore, to ensure that the data can be accessed between plugins and plugin restarts, use only the classes provided by one of the following:
java.lang.String
).Note that a plugin can serialize custom classes to a portable format that is stored as a java.lang.String
or byte[]
.
Cluster cache keys and values are stored by value, which means they are serialized on insertion to the cache, and deserialized on extraction.
Therefore, to ensure that the data can be shared, both product and plugin developers must ensure that the objects placed into a cluster cache implement serialization with support for versioning. See Versioning of serializable objects.
It's recommended that plugin developers use classes from the Java JDK and product core. If custom classes are required to be shared between plugins, then a common shared library should be used for defining them.
Hybrid cache keys are stored by value, and values are stored by reference.
Therefore:
The following example shows how to obtain a cache called old-style
using defaults.
1 2Cache<String, String> cache = cacheManager.getCache("old-style"); // Returns null cache.get("money");
The following example shows how to obtain a cache called with-loader
that uses a CacheLoader
.
1 2Cache<String, String> cache = cacheManager.getCache( "with-loader", new CacheLoader<String, String>() { public String load(String key) { return "value for " + key; } }); // Returns "value for money" cache.get("money");
The following example shows how to obtain a hybrid cache called adam
that expires values after 60 seconds of no access.
1 2CacheSettings required = new CacheSettingsBuilder(). remote(). expireAfterAccess(60, TimeUnit.SECONDS). build(); Cache<String, String> c = cacheManager.getCache("adam", someCacheLoader, required);
CachedReference
code examplesThe following example shows how to use a CachedReference.
1 2Supplier<Date> generateTheAnswer = new Supplier<>() { public Date get() { return new Date(); } } CachedReference<Date> cr = factory.getCachedReference( "mycache", generateTheAnswer); cr.get(); // will call generateTheAnswer cr.get(); // will get from the cache cr.reset(); cr.get(); // will call generateTheAnswer
Rate this page: