DEV Community

loizenai
loizenai

Posted on

Kotlin – SpringCache

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-springcache

Kotlin – SpringCache

Performance is a big problem in software development. And Caching is one solution to speed up system. The tutorial will show you how to start with Spring Cache using Kotlin Spring Boot.

I. Technologies

– Java 1.8
– Kotlin 1.2.20
– Spring Tool Suite – Version 3.9.2.RELEASE
– Spring Boot – 1.5.10.RELEASE
– Apache Maven 3.5.2

II. Goal

1. Project Structure

We build a Kotlin SpringBoot project that uses SpringCache annotations to cache data for enhancing performance.

kotlin spring cache - project structure

2. Caching

We define 3 restAPIs {'/cacheput', '/cachable', '/cacheevict'}.


@RestController
public class RestAPIs {
     
    @Autowired
    lateinit var service: CacheService
     
    @RequestMapping("/cacheput")
    fun put(...){
        service.put(firstName, id)
        ...
    }
    
    @RequestMapping("/cachable")
    fun get(...){
        return service.getById(id)
    }
     
    @RequestMapping("/cacheevict")
    fun evict(...){
        service.evict(id);
        ...
    }
}

With a CacheService component that builds on Spring Cache annotations {@CachePut, @Cacheable, @CacheEvict}.

More at:

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-springcache

Kotlin – SpringCache

Top comments (0)