Giter VIP home page Giter VIP logo

kotlin-cache2k-spring's Introduction

Cache2K/Spring Boot/Kotlin example

Overview

This is a Spring Boot project that uses the Cache2K Spring integration as described in the Cache2K documentation.

Running the example server

  1. Change to the root directory of the project.
  2. Run: ./gradlew bootRun
  3. Wait until you see the message Started SpringApplicationKt in the logs
  4. Open http://localhost:8080 in your browser

The demo shows a list of people with some sorting and filtering options. In the demo, the data source is very slow; fortunately there's a Use Caching? checkbox that you can click to enable the use of Cache2K. After you click it, the initial request will be slow and then all subsequent requests will be fast. If you unclick it, the cache is evicted (everything is deleted from the cache) and requests will go back to being slow.

Configuring Cache2K with Spring Boot using Gradle/Kotlin

The procedure to set up Cache2K is shown below. See also the Cache2K Spring Setup documentation which provides a more general description of the procedure.

  • Add dependencies for Cache2K to your build (in build.gradle.kts); specifically these lines:
	val cache2kVersion = "1.2.2.Final"
   	implementation("org.cache2k:cache2k-api:$cache2kVersion")
   	runtimeOnly("org.cache2k:cache2k-core:$cache2kVersion")
   	implementation("org.cache2k:cache2k-spring:$cache2kVersion")  
  • Add dependency for spring-boot-starter-cache to your build (in build.gradle.kts)
   	implementation("org.springframework.boot:spring-boot-starter-cache")
  • Add a CachingConfig.kt class (The name of the class isn't important)
    • Annotate it with @org.springframework.context.annotation.Configuration
    • Annotate it with @org.springframework.cache.annotation.EnableCaching
    • Create a fun cacheManager(): CacheManager method annotated with @Bean and create any caches that are needed for your application. See CachingConfig.kt for examples.

Adding Caching to your service layer

Cache2K is now ready to be used in your service layer. See PersonDataService.kt for an example service that uses Cache2K.

To have the results of a service method cached automatically, use the @Cacheable annotation as shown in the code sample below. The first parameter is the name of the cache that was defined in CachingConfig.kt. The key parameter is used to define the key used to look the value up in the cache (See the Spring docs for more information on key).

  @Cacheable("allPeopleCache", key = "#root.methodName")
    fun loadPeopleDataUsingCache():PeopleData {
        return loadPeopleDataFromDatabase()
    }

kotlin-cache2k-spring's People

Stargazers

Akito avatar

Watchers

 avatar

kotlin-cache2k-spring's Issues

Cache configuration with expirypolicy

The cache configuration looks strange:

        val expiryPolicy = { key:Any, value:Any?, loadTime:Long, oldEntry:Any? ->
            if (value == null) { ExpiryTimeValues.NO_CACHE } else { ExpiryTimeValues.ETERNAL }
        }
       ....
           expireAfterWrite(30, TimeUnit.SECONDS).permitNullValues(true).
           expiryPolicy(expiryPolicy)

Is there really any troubles with null values? Probably you can remove permitNullValues(true) and the expiry policy.

One person per cache entry?

The example right now, reads all persons in one go and just all persons in one entry.
This kind of scenario is quite rare in real world. In case you have millions of persons, you would use a cache, so the persons you are currently working with are available faster.

So I would put one person per cache entry.

Suggestions:

  • Establish a person ID
  • have a cachable method to get a person: Person get(String id)
  • hava a cachable (the search cache) method to find persons: List<String> findPersons(String searchString)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.