Skip to content

Commit

Permalink
Add random jitter to cache
Browse files Browse the repository at this point in the history
This will help smooth out some of the cyclical load
  • Loading branch information
NovaFox161 committed Apr 7, 2024
1 parent bfdf9b2 commit 84fa396
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
Expand Up @@ -2,10 +2,13 @@ package org.dreamexposure.discal.core.cache

import discord4j.common.util.Snowflake
import java.time.Duration
import kotlin.random.Random

interface CacheRepository<K, V> {
val ttl: Duration
get() = Duration.ofMinutes(60)
val ttlWithJitter: Duration
get() = ttl.plusSeconds(Random.Default.nextLong(-333,333))

// Write
suspend fun put(guildId: Snowflake? = null, key: K, value: V)
Expand Down
Expand Up @@ -19,14 +19,14 @@ class JdkCacheRepository<K : Any, V>(override val ttl: Duration) : CacheReposito
override suspend fun put(guildId: Snowflake?, key: K, value: V) {
val guildedMap = getGuildedCache(guildId)

guildedMap[key] = Pair(Instant.now().plus(ttl), value)
guildedMap[key] = Pair(Instant.now().plus(ttlWithJitter), value)
cache[guildId] = guildedMap
}

override suspend fun putMany(guildId: Snowflake?, values: Map<K, V>) {
val guildedMap = getGuildedCache(guildId)

guildedMap.putAll(values.mapValues { Pair(Instant.now().plus(ttl), it.value) })
guildedMap.putAll(values.mapValues { Pair(Instant.now().plus(ttlWithJitter), it.value) })
cache[guildId] = guildedMap
}

Expand Down
Expand Up @@ -25,12 +25,12 @@ class RedisStringCacheRepository<K, V>(
}

override suspend fun put(guildId: Snowflake?, key: K, value: V) {
valueOps.setAndAwait(formatKey(guildId, key), objectMapper.writeValueAsString(value), ttl)
valueOps.setAndAwait(formatKey(guildId, key), objectMapper.writeValueAsString(value), ttlWithJitter)
}

override suspend fun putMany(guildId: Snowflake?, values: Map<K, V>) {
values.forEach { (key, value) ->
valueOps.setAndAwait(formatKey(guildId, key), objectMapper.writeValueAsString(value), ttl)
valueOps.setAndAwait(formatKey(guildId, key), objectMapper.writeValueAsString(value), ttlWithJitter)
}
}

Expand Down

0 comments on commit 84fa396

Please sign in to comment.