-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1039 Using Redis as a cache manager
- Loading branch information
1 parent
006002f
commit 288724b
Showing
13 changed files
with
83 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...xtension-api/src/main/java/net/nemerosa/ontrack/extension/api/CacheConfigExtensionData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.nemerosa.ontrack.extension.api | ||
|
||
import java.time.Duration | ||
|
||
/** | ||
* Cache definition | ||
* | ||
* @property ttl TTL for a given entry in a cache | ||
*/ | ||
data class CacheConfigExtensionData( | ||
val ttl: Duration, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 1 addition & 43 deletions
44
ontrack-service/src/main/java/net/nemerosa/ontrack/service/CacheConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,8 @@ | ||
package net.nemerosa.ontrack.service | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import net.nemerosa.ontrack.common.Caches | ||
import net.nemerosa.ontrack.extension.api.CacheConfigExtension | ||
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 java.util.concurrent.TimeUnit | ||
|
||
@Configuration | ||
@EnableCaching | ||
class CacheConfig( | ||
private val cacheConfigProperties: CacheConfigProperties, | ||
private val cacheConfigExtensions: List<CacheConfigExtension> | ||
) { | ||
@Bean | ||
fun cacheManager(): CacheManager { | ||
val manager = SimpleCacheManager() | ||
|
||
manager.setCaches( | ||
// Built in caches | ||
listOf( | ||
// Cache for settings | ||
CaffeineCache( | ||
Caches.SETTINGS, | ||
Caffeine.newBuilder() | ||
.maximumSize(1) | ||
.expireAfterWrite(10, TimeUnit.HOURS) | ||
.build() | ||
) | ||
) + cacheConfigExtensions.flatMap { | ||
it.caches.map { (name, spec) -> toCache(name, spec) } | ||
} | ||
) | ||
|
||
return manager | ||
} | ||
|
||
private fun toCache(name: String, defaultSpec: String) = CaffeineCache( | ||
name, | ||
Caffeine.from( | ||
cacheConfigProperties.specs[name] ?: defaultSpec | ||
).build() | ||
) | ||
|
||
} | ||
class CacheConfig |
33 changes: 33 additions & 0 deletions
33
ontrack-service/src/main/java/net/nemerosa/ontrack/service/CacheConfigCustomizer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.nemerosa.ontrack.service | ||
|
||
import net.nemerosa.ontrack.common.Caches | ||
import net.nemerosa.ontrack.extension.api.CacheConfigExtension | ||
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration | ||
import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder | ||
import org.springframework.stereotype.Component | ||
import java.time.Duration | ||
|
||
@Component | ||
class CacheConfigCustomizer( | ||
private val cacheConfigExtensions: List<CacheConfigExtension>, | ||
) : RedisCacheManagerBuilderCustomizer { | ||
override fun customize(builder: RedisCacheManagerBuilder) { | ||
// Core caches | ||
builder.withCacheConfiguration( | ||
Caches.SETTINGS, | ||
RedisCacheConfiguration.defaultCacheConfig() | ||
.entryTtl(Duration.ofHours(1)) | ||
) | ||
// Extensions | ||
cacheConfigExtensions.forEach { extension -> | ||
extension.caches.forEach { (name, config) -> | ||
builder.withCacheConfiguration( | ||
name, | ||
RedisCacheConfiguration.defaultCacheConfig() | ||
.entryTtl(config.ttl) | ||
) | ||
} | ||
} | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
ontrack-service/src/main/java/net/nemerosa/ontrack/service/CacheConfigProperties.kt
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
ontrack-service/src/main/java/net/nemerosa/ontrack/service/CoreCacheConfigExtension.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.