
test.web.WebCache.kt Maven / Gradle / Ivy
package test.web
import jchanghong.kotlin.log
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer
import org.springframework.cache.annotation.Cacheable
import org.springframework.cache.interceptor.KeyGenerator
import org.springframework.cache.jcache.config.JCacheConfigurerSupport
import org.springframework.data.redis.cache.RedisCacheManager
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.lang.reflect.Method
@Component
class ConfigKey:JCacheConfigurerSupport(){
override fun keyGenerator(): KeyGenerator? {
return KeyGenerator { target, method, params ->
/**
* Generate a key for the given method and its parameters.
* @param target the target instance
* @param method the method being called
* @param params the method parameters (with any var-args expanded)
* @return a generated key
*/
/**
* Generate a key for the given method and its parameters.
* @param target the target instance
* @param method the method being called
* @param params the method parameters (with any var-args expanded)
* @return a generated key
*/
return@KeyGenerator "${target.javaClass.name}_${method.name}_${params.joinToString{it.toString()}}"
}
}
}
//@RestController
class WebCache {
@Cacheable("test")
@GetMapping("/testcache")
fun testcache():String{
log.info("in testcache()")
return "hello"
}
@Cacheable("test")
@GetMapping("/testcache1")
fun testcache1():String{
log.info("in testcache(1)")
return "hello"
}
@Cacheable("test")
@GetMapping("/testcache2")
fun testcache2(@RequestParam("a")a: String):String{
log.info("in testcache(2)")
return "hello"+a
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy