|
@@ -1,28 +1,28 @@
|
|
|
-package com.gxzc.zen.common.config.cache.caffeine
|
|
|
+package cn.gygxzc.tina.cache.caffeine
|
|
|
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine
|
|
|
+import org.springframework.beans.factory.annotation.Autowired
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
|
|
|
-import org.springframework.boot.context.properties.ConfigurationProperties
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|
|
import org.springframework.cache.CacheManager
|
|
|
-import org.springframework.cache.annotation.EnableCaching
|
|
|
import org.springframework.cache.caffeine.CaffeineCache
|
|
|
import org.springframework.cache.support.SimpleCacheManager
|
|
|
import org.springframework.context.annotation.Bean
|
|
|
import org.springframework.context.annotation.Configuration
|
|
|
-import org.springframework.context.annotation.Primary
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
/**
|
|
|
- * 缓存配置类
|
|
|
- * @author NorthLan at 2018/2/4
|
|
|
+ * Created by niantuo on 2018/9/29.
|
|
|
+ * caffeine 缓存配置
|
|
|
*/
|
|
|
+
|
|
|
@Configuration
|
|
|
-@EnableCaching
|
|
|
-@ConfigurationProperties(prefix = "cache.caffeine")
|
|
|
-@ConditionalOnProperty(prefix = "cache.caffeine", name = ["enable"], havingValue = "true")
|
|
|
-class CaffeineConfig {
|
|
|
+@EnableConfigurationProperties(CaffeineProperties::class)
|
|
|
+@ConditionalOnProperty(prefix = "cache.caffeine", name = arrayOf("enable"), havingValue = "true")
|
|
|
+class CaffeineConfiguration {
|
|
|
|
|
|
- var cacheSpecs: Map<String, CacheSpec>? = null
|
|
|
+ @Autowired
|
|
|
+ private lateinit var properties: CaffeineProperties
|
|
|
|
|
|
@Bean("caffeineCacheManager")
|
|
|
fun caffeineCacheManager(): CacheManager {
|
|
@@ -33,37 +33,37 @@ class CaffeineConfig {
|
|
|
|
|
|
fun buildCache(): MutableCollection<CaffeineCache> {
|
|
|
val collection = mutableListOf<CaffeineCache>()
|
|
|
- cacheSpecs?.forEach { name, spec ->
|
|
|
+ properties.cacheSpec.forEach { name, spec ->
|
|
|
run {
|
|
|
val builder = Caffeine.newBuilder().also {
|
|
|
- if (spec.initialCapacity >= 0) {
|
|
|
+ if (spec.initialCapacity > 0) {
|
|
|
it.initialCapacity(spec.initialCapacity)
|
|
|
}
|
|
|
- if (spec.maximumSize >= 0) {
|
|
|
+ if (spec.maximumSize > 0) {
|
|
|
it.maximumSize(spec.maximumSize)
|
|
|
}
|
|
|
- if (spec.maximumWeight >= 0) {
|
|
|
+ if (spec.maximumWeight > 0) {
|
|
|
it.maximumWeight(spec.maximumWeight)
|
|
|
}
|
|
|
- if (spec.expireAfterAccess >= 0) {
|
|
|
+ if (spec.expireAfterAccess > 0) {
|
|
|
it.expireAfterAccess(spec.expireAfterAccess, TimeUnit.SECONDS)
|
|
|
}
|
|
|
- if (spec.expireAfterWrite >= 0) {
|
|
|
+ if (spec.expireAfterWrite > 0) {
|
|
|
it.expireAfterWrite(spec.expireAfterWrite, TimeUnit.SECONDS)
|
|
|
}
|
|
|
- if (spec.refreshAfterWrite >= 0) {
|
|
|
+ if (spec.refreshAfterWrite > 0) {
|
|
|
it.refreshAfterWrite(spec.refreshAfterWrite, TimeUnit.SECONDS)
|
|
|
}
|
|
|
- if (spec.weakKeys) {
|
|
|
+ if (spec.isWeakKeys) {
|
|
|
it.weakKeys()
|
|
|
}
|
|
|
- if (spec.weakValues) {
|
|
|
+ if (spec.isWeakValues) {
|
|
|
it.weakValues()
|
|
|
}
|
|
|
- if (spec.softValues) {
|
|
|
+ if (spec.isSoftValues) {
|
|
|
it.softValues()
|
|
|
}
|
|
|
- if (spec.recordStats) {
|
|
|
+ if (spec.isRecordStats) {
|
|
|
it.recordStats()
|
|
|
}
|
|
|
}
|
|
@@ -72,4 +72,4 @@ class CaffeineConfig {
|
|
|
}
|
|
|
return collection
|
|
|
}
|
|
|
-}
|
|
|
+}
|