Skip to content

Commit

Permalink
Update Entry to handle cachedString
Browse files Browse the repository at this point in the history
  • Loading branch information
novalisdenahi committed Nov 14, 2024
1 parent d175cb9 commit 554a75c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
8 changes: 2 additions & 6 deletions src/commonMain/kotlin/com/configcat/ConfigService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ internal class ConfigService(
private val fetchJob: AtomicRef<Deferred<Pair<Entry, String?>>?> = atomic(null)
private var pollingJob: Job? = null
private var cachedEntry = Entry.empty
private var cachedJsonString = ""
private var fetching = false

val isOffline: Boolean get() = offline.value
Expand Down Expand Up @@ -247,8 +246,7 @@ internal class ConfigService(
private suspend fun readCache(): Entry {
return try {
val cached = options.configCache?.read(cacheKey) ?: ""
if (cached.isEmpty() || cached == cachedJsonString) return Entry.empty
cachedJsonString = cached
if (cached.isEmpty() || cached == cachedEntry.cacheString) return Entry.empty
Entry.fromString(cached)
} catch (e: Exception) {
logger.error(2200, ConfigCatLogMessages.CONFIG_SERVICE_CACHE_READ_ERROR, e)
Expand All @@ -259,9 +257,7 @@ internal class ConfigService(
private suspend fun writeCache(entry: Entry) {
options.configCache?.let { cache ->
try {
val json = entry.serialize()
cachedJsonString = json
cache.write(cacheKey, json)
cache.write(cacheKey, entry.cacheString)
} catch (e: Exception) {
logger.error(2201, ConfigCatLogMessages.CONFIG_SERVICE_CACHE_WRITE_ERROR, e)
}
Expand Down
13 changes: 12 additions & 1 deletion src/commonMain/kotlin/com/configcat/model/Entry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ internal data class Entry(
val configJson: String,
val fetchTime: DateTime,
) {
var cacheString: String = ""
private set

init {
cacheString = serialize(fetchTime, eTag, configJson)
}

fun isEmpty(): Boolean = this === empty

fun isExpired(threshold: DateTime): Boolean {
Expand Down Expand Up @@ -43,7 +50,11 @@ internal data class Entry(
}
}

fun serialize(): String {
private fun serialize(
fetchTime: DateTime,
eTag: String,
configJson: String,
): String {
return "${fetchTime.unixMillis.toLong()}\n${eTag}\n$configJson"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class EntrySerializationTests {
val fetchTimeNow = DateTime.now()
val entry = Entry(config, "fakeTag", json, fetchTimeNow)

val serializedString = entry.serialize()

val fetchTimeNowUnixSecond = fetchTimeNow.unixMillis.toLong()
val expected = "$fetchTimeNowUnixSecond\nfakeTag\n$json"
assertEquals(expected, serializedString)
assertEquals(expected, entry.cacheString)
}

@Test
Expand All @@ -39,9 +39,8 @@ class EntrySerializationTests {
val config: Config = Constants.json.decodeFromString(payloadTestConfigJson)

val entry = Entry(config, "test-etag", payloadTestConfigJson, DateTime(1686756435844L))
val serializedString = entry.serialize()

assertEquals("1686756435844\ntest-etag\n$payloadTestConfigJson", serializedString)
assertEquals("1686756435844\ntest-etag\n$payloadTestConfigJson", entry.cacheString)
}

@Test
Expand Down

0 comments on commit 554a75c

Please sign in to comment.