引子
项目缓存里面的值,使用频率不高,就不设置过期时间了.希望手动发送一个请求给spring boot 之后相对应的缓存就更新了.
*/在生产中有一张codeMapping表,其中的数据在程序启动时就加载进去.程序运行中访问缓存里面的数据.
表更新以后,发个
➜ ~ curl -X POST http://localhost:8080/actuator/refresh
缓存就会重新加载数据库中的数据
/*
yml配置
management:
endpoints:
web:
exposure:
include: health,info,refresh
default 值: health.info
代码
interface CodeMapper {
fun getValue(type: String, key: String): String?
}
@Component
@RefreshScope
internal class CodeMappingService(jdbcTemplate: JdbcTemplate) : CodeMapper {
private val logger: Logger = LogManager.getLogger()
private val data = mutableMapOf<String, MutableMap<String, String>>()
init {
jdbcTemplate.queryForList("SELECT * FROM code_mapping").forEach {
val type = it["type"] as String
val key = (it["from_code"] as String).toLowerCase()
val value = it["to_code"] as String
data.getOrPut(type) { mutableMapOf() }[key] = value
}
logger.info("code mapping loaded")
}
override fun getValue(type: String, key: String) = data[type]?.get(key.toLowerCase())
}
引申阅读
https://blog.wuwii.com/springboot-actuator.html