Quick cache is lightweight library developed on java 17. But you can use on Java 8 as well. It can cache the data on specific pod or server. FYI, it is not distributed. Best case to use it, when you have some static data (Should be loaded when application starts) or dynamic limited data (Depending on system capacity). Quick cache library can be used with Spring, Spring-boot and core-java applications. You can specify your on eviction policy in this cache.
Supported eviction policy
By default, LRU (Least recently used) cache strategy is used. List of supported eviction policies are -
- LRU (default)
- LFU
- MRU
- FIFO
You can use this cache with below build tools
- Apache Maven
- Gradle
- Gradle (Short)
- Gradle (Kotlin)
- sbt
- ivy
- grape
- leiningen
- buildr
Example using maven -
<dependency>
<groupId>io.github.shussain333</groupId>
<artifactId>quick-cache</artifactId>
<version>1.0.0</version>
</dependency>
You can find latest version and other build tool support on Maven Central
This section divided into 2 path -
- How to use quick-cache with spring or spring-boot application
- How to use quick-cache with core-java application
There is little bit config you will have to add in application.yml or application.properties or anywhere else wherever you have hosted your config. Here are 2 ways supported and you can use either 1 at once -
- Quick cache supports default single cache for entire application
- application.yml config
com: sartaj: quick-cache: default: maxCapacity: 200 evictionPolicy: FIFO
- or application.properties config
com.sartaj.quick-cache.default.maxCapacity=200 com.sartaj.quick-cache.default.evictionPolicy=FIFO
- Once either one of above step is done, You can inject dependency of quick-cache in anywhere in your application. And making changes at once place impact change at other place.
import com.sartaj.cache.factory.InMemoryCacheFactory; import io.swagger.v3.oas.annotations.responses.ApiResponse; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.Optional; /** * @author sartajhussain */ @RestController("Check my status") @RequestMapping("ping") public class PingController { // You can use either constructor dependency or use @autowired with variable declaration // @Autowired private final InMemoryCacheFactory inMemoryCacheFactory; /** * Inject quick-cache dependency to the constructor. * @param inMemoryCacheFactory quick cache entry point */ public PingController(InMemoryCacheFactory inMemoryCacheFactory) { this.inMemoryCacheFactory = inMemoryCacheFactory; } /** * Sample API to see how quick cache can be used * @return string */ @GetMapping @ResponseBody @ApiResponse(description = "If service is running fine the response some content") public ResponseEntity<String> ping() { // Push string data in the cache Optional<String> stringOptional = inMemoryCacheFactory.getInMemoryCache("default").put("test", "Hi, I am from admin cache store"); // Retrieve data from cache and return in the API response return ResponseEntity.ok(inMemoryCacheFactory.getInMemoryCache("default").get("test", String.class).get()); } }
- application.yml config
- And Quick cache supports context based multi cache for entire application
- application.yml config
com: sartaj: quick-cache: multiCache: admin: maxCapacity: 200 evictionPolicy: FIFO user: maxCapacity: 200 evictionPolicy: LFU
- or application.properties config
com.sartaj.quick-cache.multiCache.admin.maxCapacity=200 com.sartaj.quick-cache.multiCache.admin.evictionPolicy= FIFO com.sartaj.quick-cache.multiCache.user.maxCapacity=200 com.sartaj.quick-cache.multiCache.user.evictionPolicy=LFU
- Once either one of above step is done, You can inject dependency of quick-cache in anywhere in your application. And making changes at once place impact change at other place.
import com.sartaj.cache.factory.InMemoryCacheFactory; import io.swagger.v3.oas.annotations.responses.ApiResponse; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.Optional; /** * @author sartajhussain */ @RestController("Check my status") @RequestMapping("ping") public class PingController { // You can use either constructor dependency or use @autowired with variable declaration // @Autowired private final InMemoryCacheFactory inMemoryCacheFactory; /** * Inject quick-cache dependency to the constructor. * @param inMemoryCacheFactory quick cache entry point */ public PingController(InMemoryCacheFactory inMemoryCacheFactory) { this.inMemoryCacheFactory = inMemoryCacheFactory; } /** * Sample API to see how quick cache can be used * @return string */ @GetMapping @ResponseBody @ApiResponse(description = "If service is running fine the response some content") public ResponseEntity<String> ping() { // Push string data in the cache Optional<String> stringOptional = inMemoryCacheFactory.getInMemoryCache("admin").put("test", "Hi, I am from admin cache store"); // Retrieve data from cache and return in the API response return ResponseEntity.ok(inMemoryCacheFactory.getInMemoryCache("admin").get("test", String.class).get()); } }
- application.yml config