Skip to content

Commit

Permalink
fix(cache): load new caches every refresh (#135)
Browse files Browse the repository at this point in the history
* fix(cache): load new caches every refresh
* cache interval configurable
  • Loading branch information
emjburns authored Nov 1, 2018
1 parent 6154d35 commit d26883a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ class EddaEndpointProvider(
@Component
class EddaEndpointCache(
eddaEndpointProvider: EddaEndpointProvider
) : InMemoryCache<EddaEndpoint>(eddaEndpointProvider.load())
) : InMemoryCache<EddaEndpoint>(eddaEndpointProvider::load)
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ open class EddaImagesUsedByInstancesProvider(
@Component
open class EddaImagesUsedByInstancesCache(
eddaImagesUsedByInstancesProvider: EddaImagesUsedByInstancesProvider
) : InMemorySingletonCache<AmazonImagesUsedByInstancesCache>(eddaImagesUsedByInstancesProvider.load())
) : InMemorySingletonCache<AmazonImagesUsedByInstancesCache>(eddaImagesUsedByInstancesProvider::load)

data class AmazonImagesUsedByInstancesCache(
private val refdAmisByRegion: Map<String, Set<String>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ open class EddaLaunchConfigurationCacheProvider(
@Component
open class EddaLaunchConfigurationCache(
eddaLaunchConfigurationCacheProvider: EddaLaunchConfigurationCacheProvider
) : InMemorySingletonCache<AmazonLaunchConfigurationCache>(eddaLaunchConfigurationCacheProvider.load())
) : InMemorySingletonCache<AmazonLaunchConfigurationCache>(eddaLaunchConfigurationCacheProvider::load)

data class AmazonLaunchConfigurationCache(
private val configsByRegion: Map<String, Set<AmazonLaunchConfiguration>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BaseImageLabelsExclusionSupplier(
class BaseImageLabelsCache(
gateService: DynamicPropertyService
) : InMemoryCache<DynamicProperty>(
gateService.getProperties("bakery").propertiesList.toSet()
gateService.getProperties("bakery")::getPropertiesList
)

//TODO: make configurable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ class ClouddriverAccountProvider(
@Component
class ClouddriverAccountCache(
cloudDriverService: CloudDriverService
) : InMemoryCache<SpinnakerAccount>(cloudDriverService.getAccounts())
) : InMemoryCache<SpinnakerAccount>(cloudDriverService::getAccounts)
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,29 @@ interface SingletonCache<out T> {
}

open class InMemoryCache<out T : Cacheable>(
private val source: Set<T>
private val sourceProvider: () -> Set<T>
) : Cache<T> {

override fun contains(key: String?): Boolean {
if (key == null) return false
return cache.get().find { it.name == key } != null
}

private val cache = AtomicReference<Set<T>>()

@Scheduled(fixedDelay = 15 * 60 * 1000L) //TODO: make configurable
@Scheduled(initialDelay = 0L, fixedDelayString = "\${cache.updateIntervalMillis:900000}")
private fun refresh() {
try {
log.info("Refreshing cache ${javaClass.name}")
cache.set(source)
cache.set(sourceProvider.invoke())
} catch (e: Exception) {
log.error("Error refreshing cache ${javaClass.name}", e)
}
}

override fun get(): Set<T> {
if (cache.get() == null) {
cache.set(source)
cache.set(sourceProvider.invoke())
}

return cache.get()
Expand All @@ -65,25 +66,25 @@ open class InMemoryCache<out T : Cacheable>(
}

open class InMemorySingletonCache<out T : Cacheable>(
private val source: T
private val sourceProvider: () -> T
) : SingletonCache<T> {
val log: Logger = LoggerFactory.getLogger(javaClass)

private val cache = AtomicReference<T>()

@Scheduled(fixedDelay = 15 * 60 * 1000L) //TODO: make configurable
@Scheduled(initialDelay = 0L, fixedDelayString = "\${cache.updateIntervalMillis:900000}")
private fun refresh() {
try {
log.info("Refreshing cache ${javaClass.name}")
cache.set(source)
cache.set(sourceProvider.invoke())
} catch (e: Exception) {
log.error("Error refreshing cache ${javaClass.name}", e)
}
}

override fun get(): T {
if (cache.get() == null) {
cache.set(source)
cache.set(sourceProvider.invoke())
}
return cache.get()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ interface DynamicPropertyService {

data class PropertyResponse(
val propertiesList: List<DynamicProperty>
)
) {
fun getPropertiesList(): Set<DynamicProperty> {
return propertiesList.toSet()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ import com.netflix.spinnaker.swabbie.model.Application
import org.springframework.stereotype.Component

@Component
class Front50ApplicationCache(front50Service: Front50Service) : InMemoryCache<Application>(front50Service.getApplications())
class Front50ApplicationCache(front50Service: Front50Service) : InMemoryCache<Application>(front50Service::getApplications)

0 comments on commit d26883a

Please sign in to comment.