-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes for AdvancedLRU with naive implementtion, LRUCache, and AnyCall…
…back (#144)
- Loading branch information
Showing
12 changed files
with
362 additions
and
193 deletions.
There are no files selected for viewing
53 changes: 10 additions & 43 deletions
53
src/test/kotlin/com/igorwojda/cache/advancedlru/Challenge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,19 @@ | ||
package com.igorwojda.cache.advancedlru | ||
|
||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.jupiter.api.Test | ||
import java.util.* | ||
import java.time.Clock | ||
import java.time.Duration | ||
|
||
class AdvancedLRUCache(private val capacity: Int) { | ||
fun put(key: String, value: Int, priority: Int, expiryTime: Long) { | ||
TODO("Add your solution here") | ||
} | ||
|
||
fun get(key: String): Int? { | ||
TODO("Add your solution here") | ||
} | ||
|
||
// Returns fixed system time in milliseconds | ||
private fun getSystemTimeForExpiry() = 1000 | ||
interface LRUCache<K: Any, V: Any> { | ||
fun put(key: K, value: V, priority: Int, ttl: Duration) | ||
fun get(key: K): V? | ||
} | ||
|
||
private class Test { | ||
@Test | ||
fun `add and get`() { | ||
val cache = AdvancedLRUCache(2) | ||
cache.put("A", 1, 5, 5000) | ||
|
||
cache.get("A") shouldBeEqualTo 1 | ||
} | ||
|
||
@Test | ||
fun `evict by priority`() { | ||
val cache = AdvancedLRUCache(2) | ||
cache.put("A", 1, 1, 3000) | ||
cache.put("B", 2, 3, 4000) | ||
cache.put("C", 3, 4, 5000) | ||
|
||
// This should be null because "A" was evicted due to lower priority. | ||
cache.get("A") shouldBeEqualTo null | ||
cache.get("B") shouldBeEqualTo 2 | ||
cache.get("C") shouldBeEqualTo 3 | ||
class AdvancedLRUCache<K: Any, V: Any>(private val capacity: Int, private val clock: Clock = Clock.systemDefaultZone()): LRUCache<K, V> { | ||
override fun put(key: K, value: V, priority: Int, ttl: Duration) { | ||
TODO("Add your solution here") | ||
} | ||
|
||
@Test | ||
fun `evict by expiry`() { | ||
val cache = AdvancedLRUCache(2) | ||
cache.put("A", 1, 1, 500) | ||
cache.put("B", 2, 3, 700) | ||
|
||
// This should be null because "A" was evicted due to expiry. | ||
cache.get("A") shouldBeEqualTo null | ||
cache.get("B") shouldBeEqualTo null | ||
override fun get(key: K): V? { | ||
TODO("Add your solution here") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 69 additions & 43 deletions
112
src/test/kotlin/com/igorwojda/cache/advancedlru/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/test/kotlin/com/igorwojda/cache/advancedlru/Tests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.igorwojda.cache.advancedlru | ||
|
||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.jupiter.api.Test | ||
import java.time.Clock | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
|
||
class Tests { | ||
// Easily switch between a known solution and Challenge code | ||
private val classUnderTest: (capacity: Int, clock: Clock)->LRUCache<String, String> = ::AdvancedLRUCache // or SolutionN::AdvancedLRUCache | ||
|
||
private val testClock = object : Clock() { | ||
private var testTime = Instant.now() | ||
override fun instant(): Instant { | ||
return testTime | ||
} | ||
|
||
fun incTime(duration: Duration) { | ||
testTime += duration | ||
} | ||
|
||
override fun withZone(zone: ZoneId?): Clock = TODO("Not yet implemented") | ||
override fun getZone(): ZoneId = systemDefaultZone().zone | ||
} | ||
|
||
@Test | ||
fun `add and get immediately`() { | ||
val cache = classUnderTest(2, testClock) | ||
|
||
cache.put("A", "apple", 0, Duration.ofMinutes(15)) | ||
cache.get("A") shouldBeEqualTo "apple" | ||
|
||
cache.put("B", "bee", 0, Duration.ofMinutes(15)) | ||
cache.get("B") shouldBeEqualTo "bee" | ||
|
||
cache.put("C", "cat", 0, Duration.ofMinutes(15)) | ||
cache.get("C") shouldBeEqualTo "cat" | ||
|
||
cache.put("E", "echo", 0, Duration.ofMinutes(15)) | ||
cache.get("E") shouldBeEqualTo "echo" | ||
} | ||
|
||
@Test | ||
fun `evict by priority`() { | ||
val cache = classUnderTest(4, testClock) | ||
|
||
// all have the same expiry | ||
cache.put("B", "bee", 3, Duration.ofMinutes(15)) | ||
cache.put("A", "apple", 1, Duration.ofMinutes(15)) // lowest priority | ||
cache.put("C", "cat", 5, Duration.ofMinutes(15)) | ||
cache.put("D", "door", 7, Duration.ofMinutes(15)) | ||
cache.put("E", "echo", 9, Duration.ofMinutes(15)) // causes eviction | ||
|
||
// This should be null because "A" was evicted due to lower priority and no items have reached expiry time | ||
cache.get("A") shouldBeEqualTo null | ||
cache.get("B") shouldBeEqualTo "bee" | ||
cache.get("C") shouldBeEqualTo "cat" | ||
cache.get("D") shouldBeEqualTo "door" | ||
cache.get("E") shouldBeEqualTo "echo" | ||
} | ||
|
||
@Test | ||
fun `evict by priority and last used`() { | ||
val cache = classUnderTest(4, testClock) | ||
|
||
// some have the same priority | ||
cache.put("C", "cat", 1, Duration.ofMinutes(12)) // priority 1 | ||
cache.put("A", "apple", 1, Duration.ofMinutes(20)) // priority 1 | ||
cache.put("B", "bee", 1, Duration.ofMinutes(10)) // priority 1 | ||
cache.put("D", "door", 7, Duration.ofMinutes(5)) | ||
|
||
// but are accessed most recently in a different order... | ||
testClock.incTime(Duration.ofSeconds(1)) | ||
cache.get("A") | ||
testClock.incTime(Duration.ofSeconds(1)) | ||
cache.get("C") | ||
testClock.incTime(Duration.ofSeconds(1)) | ||
cache.get("B") | ||
testClock.incTime(Duration.ofSeconds(1)) | ||
|
||
cache.put("E", "echo", 9, Duration.ofMinutes(15)) // causes eviction | ||
|
||
// This should be null because "A" was evicted due to lower priority. | ||
println(cache) | ||
cache.get("A") shouldBeEqualTo null | ||
cache.get("B") shouldBeEqualTo "bee" | ||
cache.get("C") shouldBeEqualTo "cat" | ||
cache.get("D") shouldBeEqualTo "door" | ||
cache.get("E") shouldBeEqualTo "echo" | ||
} | ||
|
||
@Test | ||
fun `evict by expiry time`() { | ||
val cache = classUnderTest(100, testClock) | ||
|
||
cache.put("A", "apple", 1, Duration.ofMinutes(15)) | ||
cache.put("B", "bee", 3, Duration.ofMinutes(20)) | ||
|
||
testClock.incTime(Duration.ofMinutes(16)) | ||
|
||
// This should be null because "A" was evicted due to expiry. | ||
cache.get("A") shouldBeEqualTo null | ||
|
||
testClock.incTime(Duration.ofMinutes(20)) | ||
|
||
cache.put("C", "cat", 5, Duration.ofMinutes(15)) // causes eviction | ||
cache.put("D", "door", 5, Duration.ofMinutes(15)) // causes eviction | ||
|
||
// this should be null because "B" was evicted due to expiry and later inserts | ||
cache.get("B") shouldBeEqualTo null | ||
|
||
testClock.incTime(Duration.ofMinutes(14)) | ||
|
||
// still here as clock has not moved past expiry | ||
cache.get("C") shouldBeEqualTo "cat" | ||
cache.get("D") shouldBeEqualTo "door" | ||
} | ||
} |
Oops, something went wrong.