Skip to content

Commit

Permalink
feat(Backend): setup Caffeine as cache provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Jul 17, 2024
1 parent 596f946 commit f958209
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-log4j2:$springVersion")
implementation("org.springframework.boot:spring-boot-starter-security:$springVersion")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server:$springVersion")
implementation("org.springframework.boot:spring-boot-starter-cache:$springVersion")
implementation("com.github.ben-manes.caffeine:caffeine:3.1.8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.GeometryFactory
import org.locationtech.jts.geom.MultiPoint
import org.slf4j.LoggerFactory
import org.springframework.cache.annotation.Cacheable
import java.time.ZonedDateTime
import java.util.*

Expand Down Expand Up @@ -190,6 +191,7 @@ class GetEnvMissionById(
return mission
}

@Cacheable(value = ["envMission"])
fun execute(missionId: Int): ExtendedEnvMissionEntity? {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionSourceEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionTypeEnum
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.IEnvMissionRepository
import org.slf4j.LoggerFactory
import org.springframework.cache.annotation.Cacheable
import java.time.ZonedDateTime


Expand Down Expand Up @@ -102,6 +103,7 @@ class GetEnvMissions(

val mockedMissions = listOf(mission1, mission2, mission3, mission4, mission5, mission6)

@Cacheable(value = ["envMissions"])
fun execute(
startedAfterDateTime: ZonedDateTime? = null,
startedBeforeDateTime: ZonedDateTime? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fr.gouv.dgampa.rapportnav.domain.repositories.mission.IFishActionReposito
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.action.AttachControlsToActionControl
import io.sentry.Sentry
import org.slf4j.LoggerFactory
import org.springframework.cache.annotation.Cacheable
import java.time.ZonedDateTime


Expand Down Expand Up @@ -189,6 +190,7 @@ class GetFishActionsByMissionId(
return actions
}

@Cacheable(value = ["fishActions"])
fun execute(missionId: Int): List<ExtendedFishActionEntity> {
try {
val fishActions = fishActionRepo.findFishActions(missionId = missionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class GetAllControlPlans(
)
}

@Cacheable("controlPlans")
// TODO move @Cacheable into findAllControlPlans() once there is an API mocking service in place
@Cacheable(value = ["controlPlans"])
fun execute(): ControlPlansEntity? {
try {
val response =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import java.net.http.HttpResponse
@UseCase
class GetNatinfs(private val mapper: ObjectMapper) {

@Cacheable("natinfs")
@Cacheable(value = ["natinfs"])
fun execute(): List<NatinfEntity> {

// TODO: move this into infrastructure/APIMonitorEnv
// TODO: and then move @Cacheable in that newly created function
val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder().uri(
URI.create(
Expand Down
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()
}
}

0 comments on commit f958209

Please sign in to comment.