Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved TimedCache to korlibs.time package & add IntTimedCache + tests #1975

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions korge-core/src/common/korlibs/io/lang/TimedCache.kt

This file was deleted.

45 changes: 45 additions & 0 deletions korge-foundation/src/common/korlibs/time/TimedCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package korlibs.time

import kotlin.reflect.*

class TimedCache<T : Any>(var validTime: TimeSpan, val timeProvider: TimeProvider = TimeProvider, val gen: () -> T) {
private var cachedTime: DateTime = DateTime.EPOCH
private lateinit var _value: T

var value: T
get() = getValue(Unit, null)
set(value) { setValue(Unit, null, value) }

operator fun getValue(obj: Any, prop: KProperty<*>?): T {
val now = timeProvider.now()
if (cachedTime == DateTime.EPOCH || (now - cachedTime) >= validTime) {
cachedTime = now
this._value = gen()
}
return _value
}

operator fun setValue(obj: Any, prop: KProperty<*>?, value: T) {
this._value = value
}
}

class IntTimedCache(val ttl: TimeSpan, val timeProvider: TimeProvider = TimeProvider, val gen: () -> Int) {
@PublishedApi internal var cachedTime = DateTime.EPOCH
@PublishedApi internal var _value: Int = 0

var value: Int
get() = get()
set(value) { _value = value }

inline fun get(): Int {
val now = timeProvider.now()
if (cachedTime == DateTime.EPOCH || (now - cachedTime >= ttl)) {
cachedTime = now
_value = gen()
}
return _value
}

operator fun getValue(obj: Any?, property: KProperty<*>): Int = get()
}
37 changes: 37 additions & 0 deletions korge-foundation/test/common/korlibs/time/TimedCacheTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package korlibs.time

import kotlin.test.*

class TimedCacheTest {
@Test
fun test() {
var now = DateTime.fromUnixMillis(1000L)
val provider = TimeProvider { now }
var value = 0
val cache = TimedCache<Int>(1.seconds, provider) { value++ }
assertEquals(0, cache.value)
assertEquals(0, cache.value)
assertEquals(0, cache.value)
now = DateTime.fromUnixMillis(2000L)
assertEquals(1, cache.value)
assertEquals(1, cache.value)
cache.value = 100
assertEquals(100, cache.value)
}

@Test
fun testInt() {
var now = DateTime.fromUnixMillis(1000L)
val provider = TimeProvider { now }
var value = 0
val cache = IntTimedCache(1.seconds, provider) { value++ }
assertEquals(0, cache.value)
assertEquals(0, cache.value)
assertEquals(0, cache.value)
now = DateTime.fromUnixMillis(2000L)
assertEquals(1, cache.value)
assertEquals(1, cache.value)
cache.value = 100
assertEquals(100, cache.value)
}
}
1 change: 0 additions & 1 deletion korge/src/common/korlibs/render/GameWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import korlibs.logger.*
import korlibs.math.geom.*
import korlibs.memory.*
import korlibs.render.GameWindow.Quality.*
import korlibs.render.internal.*
import korlibs.time.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
Expand Down
39 changes: 0 additions & 39 deletions korge/src/common/korlibs/render/internal/TimedCache.kt

This file was deleted.

Loading