-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from TorMap/dev
Improve Caching
- Loading branch information
Showing
12 changed files
with
308 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.tormap.config | ||
|
||
import org.ehcache.config.builders.CacheConfigurationBuilder | ||
import org.ehcache.config.builders.ResourcePoolsBuilder | ||
import org.ehcache.jsr107.Eh107Configuration | ||
import org.springframework.cache.annotation.EnableCaching | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import javax.cache.CacheManager | ||
import javax.cache.Caching | ||
|
||
@Configuration | ||
@EnableCaching | ||
class CacheConfig { | ||
companion object { | ||
const val RELAY_LOCATION_DISTINCT_DAYS = "RELAY_LOCATION_DISTINCT_DAYS" | ||
const val RELAY_LOCATION_DISTINCT_DAYS_KEY = "RELAY_LOCATION_DISTINCT_DAYS_KEY" | ||
const val RELAY_LOCATIONS_PER_DAY = "RELAY_LOCATIONS_OF_DAY" | ||
} | ||
|
||
@Bean | ||
fun getCacheManager(): CacheManager { | ||
val provider = Caching.getCachingProvider() | ||
val cacheManager = provider.cacheManager | ||
|
||
cacheManager.createCache( | ||
RELAY_LOCATION_DISTINCT_DAYS, | ||
Eh107Configuration.fromEhcacheCacheConfiguration( | ||
CacheConfigurationBuilder.newCacheConfigurationBuilder( | ||
String::class.java, Set::class.java, | ||
ResourcePoolsBuilder.heap(1) | ||
) | ||
) | ||
) | ||
|
||
cacheManager.createCache( | ||
RELAY_LOCATIONS_PER_DAY, | ||
Eh107Configuration.fromEhcacheCacheConfiguration( | ||
CacheConfigurationBuilder.newCacheConfigurationBuilder( | ||
String::class.java, List::class.java, | ||
ResourcePoolsBuilder.heap(40) // 1 entry ~= 2.5 MB of memory -> 40 entries ~= 100 MB of memory | ||
) | ||
) | ||
) | ||
return cacheManager | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
backend/src/main/kotlin/org/tormap/service/CacheService.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,85 @@ | ||
package org.tormap.service | ||
|
||
import org.springframework.cache.CacheManager | ||
import org.springframework.scheduling.annotation.Async | ||
import org.springframework.stereotype.Service | ||
import org.tormap.config.CacheConfig | ||
import org.tormap.database.repository.RelayLocationRepositoryImpl | ||
import org.tormap.util.logger | ||
import java.time.LocalDate | ||
import java.time.YearMonth | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.locks.Lock | ||
import java.util.concurrent.locks.ReentrantLock | ||
|
||
|
||
@Service | ||
class CacheService( | ||
private val cacheManager: CacheManager, | ||
private val relayLocationRepositoryImpl: RelayLocationRepositoryImpl, | ||
) { | ||
private val logger = logger() | ||
private val lockRelayLocationDistinctDays: Lock = ReentrantLock() | ||
private val lockRelayLocationsPerDay: Lock = ReentrantLock() | ||
|
||
@Async | ||
fun cacheRelayLocationDistinctDays(): CompletableFuture<Void> { | ||
if (lockRelayLocationDistinctDays.tryLock()) { | ||
try { | ||
logger.info("Caching distinct relay location days") | ||
cacheManager.getCache(CacheConfig.RELAY_LOCATION_DISTINCT_DAYS)?.put( | ||
CacheConfig.RELAY_LOCATION_DISTINCT_DAYS_KEY, | ||
relayLocationRepositoryImpl.findDistinctDays() | ||
) | ||
} finally { | ||
lockRelayLocationDistinctDays.unlock() | ||
} | ||
} else { | ||
logger.debug("Cache update of relay location distinct days already in progress. Waiting 1 second...") | ||
Thread.sleep(1000) | ||
cacheRelayLocationDistinctDays() | ||
} | ||
return CompletableFuture.completedFuture(null) | ||
} | ||
|
||
@Async | ||
fun cacheRelayLocationsPerDay(months: Set<String>): CompletableFuture<Void> { | ||
if (lockRelayLocationsPerDay.tryLock()) { | ||
try { | ||
logger.info("Caching relay locations for each day of months: ${months.joinToString(", ")}") | ||
months.forEach { month -> | ||
val yearMonth = YearMonth.parse(month) | ||
yearMonth.atDay(1).datesUntil(yearMonth.plusMonths(1).atDay(1)).forEach { | ||
val day = it.toString() | ||
val relayLocations = relayLocationRepositoryImpl.findAllUsingDay(LocalDate.parse(day)) | ||
if (relayLocations.isNotEmpty()) { | ||
cacheManager.getCache(CacheConfig.RELAY_LOCATIONS_PER_DAY)?.put( | ||
day, | ||
relayLocations | ||
) | ||
} | ||
} | ||
} | ||
} finally { | ||
lockRelayLocationsPerDay.unlock() | ||
} | ||
} else { | ||
logger.debug("Cache update of relay location per day already in progress. Waiting 1 second...") | ||
Thread.sleep(1000) | ||
cacheRelayLocationsPerDay(months) | ||
} | ||
return CompletableFuture.completedFuture(null) | ||
} | ||
|
||
@Async | ||
fun evictRelayLocationsPerDay(months: Set<String>): CompletableFuture<Void> { | ||
logger.info("Evicting cache of relay locations per day for months: ${months.joinToString(", ")}") | ||
months.forEach { month -> | ||
val yearMonth = YearMonth.parse(month) | ||
yearMonth.atDay(1).datesUntil(yearMonth.plusMonths(1).atDay(1)).forEach { | ||
cacheManager.getCache(CacheConfig.RELAY_LOCATIONS_PER_DAY)?.evict(it.toString()) | ||
} | ||
} | ||
return CompletableFuture.completedFuture(null) | ||
} | ||
} |
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
Oops, something went wrong.