What I used
// Groovy Version: 2.4.5 JVM: 1.8.0_77 Vendor: Oracle Corporation OS: Mac OS X
@Grab('javax.cache:cache-api:1.0.0')
@Grab('org.ehcache:ehcache:3.2.0')
@Grab('org.jsr107.ri:cache-annotations-ri-guice:1.0.0')
@Grab('com.google.inject:guice:4.1.0')
import com.google.inject.AbstractModule
import com.google.inject.Guice
import com.google.inject.Injector
import org.jsr107.ri.annotations.guice.module.CacheAnnotationsModule
import javax.cache.CacheManager
import javax.cache.Caching
import javax.cache.annotation.CacheDefaults
import javax.cache.annotation.CacheResult
import javax.cache.configuration.Configuration
import javax.cache.configuration.MutableConfiguration
import javax.cache.expiry.AccessedExpiryPolicy
import javax.cache.expiry.Duration
import javax.cache.spi.CachingProvider
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
@CacheDefaults(cacheName = 'simpleCache')
class Sample {
@CacheResult
String hello() {
new SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(new Date())
}
}
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
Configuration<Object, Object> config =
new MutableConfiguration<Object, Object>()
.setTypes(Object.class, Object.class)
//The cache is valid for 1 second
.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
cacheManager.createCache('simpleCache', config)
Injector injector = Guice.createInjector(new CacheAnnotationsModule())
Sample sample = injector.getInstance(Sample.class);
println sample.hello()
println sample.hello()
println sample.hello()
println sample.hello()
println sample.hello()
//↑ means that the same time is output
TimeUnit.SECONDS.sleep(1)
//The time different from the above is output after re-acquiring due to cache out.
println sample.hello()
result
2016-12-24 14:04:27.284
2016-12-24 14:04:27.284
2016-12-24 14:04:27.284
2016-12-24 14:04:27.284
2016-12-24 14:04:27.284
2016-12-24 14:04:28.312 #The result is different only here. As expected
I just tried to cache the method results without using CDI, but it took about 20 hours. ..
The first assumption is
@ javax.cache.annotation.CacheResult
to the method you want to cache in the POJO.I thought it would be possible, but it was completely different.
In fact,
cache-annotations-ri
for Guice to dependencies@ javax.cache.annotation.CacheResult
to the method you want to cache in the POJO.javax.cache.annotation.CacheResolverFactory
and JCache InterceptorIt needed to be quite complicated.
Both key and value are used in java.lang.Object
, which is ʻorg.jsr107.ri.annotations.DefaultCacheResolverFactory # getCacheResolver is
javax.cache.CacheManager # getCache (java.lang.String) Calling inside [I saw the implementation of Ehcache](https://github.com/ehcache/ehcache3/blob/v3.2.0/107/src/main/java/org/ehcache/jsr107/Eh107CacheManager .java # L288-L292), because it turned out that key and value must be ʻObject
.
If you call javax.cache.CacheManager # getCache (java.lang.String, java.lang.Class <K>, java.lang.Class <V>)
, you should be able to handle it with any type, so If you implement CacheResolverFactory
yourself, you can change it. For the time being, I set key and value to ʻObject` with an emphasis on moving quickly.
The Interceptor settings are entirely based on the framework code Fathom. Thank you. .. I would like to try this framework itself next time.
[Addition] ↑ As pointed out by @kazuhira_r, the amount of code has been reduced by using ʻorg.jsr107.ri.annotations.guice.module.CacheAnnotationsModule`.
JCache wasn't something you could try with the mood and momentum like Bean Validation. The sample documentation didn't help either.
Recommended Posts