Vishal Kawade
Vishal Kawade

Reputation: 443

How to use JCS with Spring boot(Rest api)

I am new to Spring boot caching, as per my requirement i want to use JCS with spring boot(rest api). I have searched over the google but, haven't got much information about it.

There is info available with java class. Does any one knows how to use it or any related tutorial or information.

This might be the opinion based question. But it is much needed.

  public static void main(String[] args) {
//   Initialize the JCS object and get an instance of the default cache region
try {
    JCS cache = JCS.getInstance("default");

String key = "key0";
String value = "value0";

cache.put(key, value);
cache.put("vasu","dev");


} catch (CacheException e) {
    e.printStackTrace();
}

}

Upvotes: 2

Views: 1739

Answers (2)

Vikram Thakur
Vikram Thakur

Reputation: 1

With spring boot you can specify the following property to plugin JCS as a cache implementation compliant with JSR 107.

spring.cache.jcache.config=classpath:cache.ccf

cache.ccf is the configuration file for JCS which can be placed anywhere in the classpath.

Ref: spring JSR-107 Ref: JCS overview

The spring.cache.jcache.provider property is not required to be specified, as long as you are just using one caching provider.

Upvotes: 0

Sambit
Sambit

Reputation: 8031

As far as caching with spring boot is concerned, spring supports the following cache providers, as it has been mentioned in the following link.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html

To quote, It says

If you have not defined a bean of type CacheManager or a CacheResolver named cacheResolver (see CachingConfigurer), Spring Boot tries to detect the following providers (in the indicated order):

  • Generic
  • JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)
  • EhCache 2.x
  • Hazelcast
  • Infinispan
  • Couchbase
  • Redis
  • Caffeine
  • Simple

I will suggest to use Ehcache, you can check more details on Ehcache with Spring framework in the below link. https://www.baeldung.com/spring-cache-tutorial

If you are only interested about JCS, then refer below the link to have an understanding.

https://codyburleson.com/quick-and-simple-caching-with-apache-commons-jcs/

In case pf Spring boot, I will suggest to create class and wrap JCS inside that class so that you can wire in any class so that you can abstract away the JCS implementation details. I provide below outline.

@Autowired CacheUpdater cacheUpdate;

in the method, you can write like this,

public void someMethod(... params) { cacheUpdater.update(key,value) }

It is not necessary that, you have to have a method called update(), you can create any method and it should internally call JCS to put the key and value in cache.

Upvotes: 2

Related Questions