Skip to content

Commit

Permalink
caching wasn't actually happening, fix this
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Jul 15, 2018
1 parent 013b27d commit c668427
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/main/kotlin/io/kweb/shoebox/stores/DirectoryStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.kweb.shoebox.stores

import com.fatboyindustrial.gsonjavatime.Converters
import com.google.common.cache.*
import com.google.common.cache.CacheLoader.InvalidCacheLoadException
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import io.kweb.shoebox.*
Expand Down Expand Up @@ -34,7 +35,18 @@ class DirectoryStore<T : Any>(val directory : Path, private val kc : KClass<T>)
internal val cache: LoadingCache<String, CachedValueWithTime<T>> = CacheBuilder.newBuilder().build(
object : CacheLoader<String, CachedValueWithTime<T>>() {
override fun load(key: String): CachedValueWithTime<T>? {
return this@DirectoryStore.load(key)
val filePath = this@DirectoryStore.toPath(key)
return if (Files.exists(filePath)) {
if (Files.isDirectory(filePath)) {
throw IllegalStateException("File $filePath is a directory, not a file")
}
val o = filePath.newBufferedReader().use {
gson.fromJson(it, kc.javaObjectType)
}
CachedValueWithTime(o, Files.getLastModifiedTime(filePath).toInstant())
} else {
null
}
}
}
)
Expand Down Expand Up @@ -94,7 +106,7 @@ class DirectoryStore<T : Any>(val directory : Path, private val kc : KClass<T>)
if (cachedValue != null) {
cache.invalidate(key)
}
val filePath = directory.resolve(key)
val filePath = this.toPath(key)
if (Files.exists(filePath)) {
val oldValue = cachedValue ?: load(key)?.value
return if (oldValue != null) {
Expand Down Expand Up @@ -130,19 +142,15 @@ class DirectoryStore<T : Any>(val directory : Path, private val kc : KClass<T>)

private fun load(key: String): CachedValueWithTime<T>? {
val filePath = toPath(key)
cache.get(key).let { cached ->
if (cached != null && !cached.time.isAfter(Files.getLastModifiedTime(filePath).toInstant())) return cached
}
return if (Files.exists(filePath)) {
if (Files.isDirectory(filePath)) {
throw IllegalStateException("File $filePath is a directory, not a file")
}
val o = filePath.newBufferedReader().use {
gson.fromJson(it, kc.javaObjectType)
val cached = try {cache.get(key) } catch (e : InvalidCacheLoadException) { null }
return if (cached != null) {
val cacheIsOutOfDate = !cached.time.isAfter(Files.getLastModifiedTime(filePath).toInstant())
if (cacheIsOutOfDate) {
cache.invalidate(key)
cache.get(key)
} else {
cached
}
val cachedValueWithTime = CachedValueWithTime(o, Files.getLastModifiedTime(filePath).toInstant())
cache.put(key, cachedValueWithTime)
cachedValueWithTime
} else {
null
}
Expand Down

0 comments on commit c668427

Please sign in to comment.