Think about SpringBoot cache

We are using spring boot frame work and choose SimpleCacheManager to be the cache manager, scanned some references, there are also CompositeCacheManager and NoOpCacheManager, I thought that NoOpCacheManager was designed to make tests convenient, but what is the scene to use CompositeCacheManager and what is it different from SimpleCacheManager?

In the next part, I’ll show some codes to explain how we use SimpleCacheManager, if there is better strategy to use SimpleCacheManager?

To make the frame work, we need to add @EnableCaching at the entrance and add @Cacheable of the aim service method, cache manager implemented at the entrance.

entrance:


@Configuration
@EnableCaching
public class Application {
  SimpleCacheManager cacheManager = new SimpleCacheManager();
  GuavaCache cache = new GuavaCache("ivan_cache",
  CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build());
  cacheManager.setCaches(ImmutableList.of(cache));
  return cacheManager();
}

Service module


@Cacheable(value = { "ivan_cache" })
public void cacheTest() {
  log.info("Cache not effects.");
}

Refer to:

http://codedevstuff.blogspot.com/2015/07/add-guava-cache-to-spring-boot-to-cache.html

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>