I tried lightly the result cache of the method of JCache (Ehcache 3) feat. Guice

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

  1. Add JCache API and implementation to dependencies
  2. Add @ 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,

  1. Add JCache API and implementation to dependencies
  2. Guice seems to be the only option for use other than CDI and Spring Framework, so make Guice a dependency add to
  3. Add cache-annotations-ri for Guice to dependencies
  4. Add @ javax.cache.annotation.CacheResult to the method you want to cache in the POJO.
  5. Set javax.cache.annotation.CacheResolverFactory and JCache Interceptor

It needed to be quite complicated.

Yomoyama

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`.

Impressions

JCache wasn't something you could try with the mood and momentum like Bean Validation. The sample documentation didn't help either.

reference

Recommended Posts

I tried lightly the result cache of the method of JCache (Ehcache 3) feat. Guice
I tried to explain the method
I tried using the profiler of IntelliJ IDEA
I tried using the Server Push function of Servlet 4.0
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried the new feature profiler of IntelliJ IDEA 2019.2.
[WIP] I tried the configuration of Docker + Streama + NFS
I tried to develop the cache function of Application Container Cloud Service in the local environment
[Rails] I tried using the button_to method for the first time
I tried to summarize the basics of kotlin and java
Now, I understand the coordinate transformation method of UIView (Swift)
I want to expand the clickable part of the link_to method
[Swift] I tried to implement the function of the vending machine
I tried JAX-RS and made a note of the procedure
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment of WSL2 + Docker + VSCode
I tried to express the result of before and after of Date class with a number line
I tried the Docker tutorial!
I tried the VueJS tutorial!
I tried the FizzBuzz problem
I tried to solve the problem of "multi-stage selection" with Ruby
I tried the input / output type of Java Lambda ~ Map edition ~
I tried to understand how the rails method "redirect_to" is defined
I tried to check the operation of gRPC server with grpcurl
I tried to understand how the rails method "link_to" is defined
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
I tried to solve the problem of Google Tech Dev Guide