-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Backend): setup Caffeine as cache provider
- Loading branch information
Showing
7 changed files
with
84 additions
and
2 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
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
71 changes: 71 additions & 0 deletions
71
...d/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/cache/CaffeineConfiguration.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,71 @@ | ||
package fr.gouv.dgampa.rapportnav.infrastructure.cache | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import com.github.benmanes.caffeine.cache.Ticker | ||
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 | ||
|
||
@EnableCaching | ||
@Configuration | ||
class CaffeineConfiguration { | ||
|
||
// long term caches for very static data | ||
val natinfs = "natinfs" | ||
val controlPlans = "controlPlans" | ||
|
||
// short term caches to reduce API calls | ||
val envMissions = "envMissions" | ||
val envMission = "envMission" | ||
val fishActions = "fishActions" | ||
|
||
@Bean | ||
fun cacheManager(ticker: Ticker): CacheManager? { | ||
|
||
// long term caches for very static data | ||
val natinfsCache = builCache(natinfs, ticker, TimeUnit.DAYS, 7) | ||
val controlPlansCache = builCache(controlPlans, ticker, TimeUnit.DAYS, 7) | ||
|
||
// short term caches for Missions and Actions | ||
val envMissionsCache = builCache(envMissions, ticker, TimeUnit.MINUTES, 5) | ||
val envMissionCache = builCache(envMission, ticker, TimeUnit.MINUTES, 5) | ||
val fishActionsCache = builCache(fishActions, ticker, TimeUnit.MINUTES, 5) | ||
|
||
|
||
val manager = SimpleCacheManager() | ||
manager.setCaches( | ||
listOf( | ||
// long term caches | ||
natinfsCache, | ||
controlPlansCache, | ||
|
||
// short term caches | ||
envMissionsCache, | ||
envMissionCache, | ||
fishActionsCache, | ||
), | ||
) | ||
|
||
return manager | ||
} | ||
|
||
private fun builCache(name: String, ticker: Ticker, timeUnit: TimeUnit, durationInUnit: Int): CaffeineCache { | ||
return CaffeineCache( | ||
name, | ||
Caffeine.newBuilder() | ||
.expireAfterWrite(durationInUnit.toLong(), timeUnit) | ||
.recordStats() | ||
.ticker(ticker) | ||
.build(), | ||
) | ||
} | ||
|
||
@Bean | ||
fun ticker(): Ticker? { | ||
return Ticker.systemTicker() | ||
} | ||
} |