cache

Adaptable cache with query-like operations

License

License

MIT
Categories

Categories

Discord API Business Logic Libraries
GroupId

GroupId

dev.kord.cache
ArtifactId

ArtifactId

cache-tck
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

cache
Adaptable cache with query-like operations
Project Organization

Project Organization

Kord
Source Code Management

Source Code Management

https://github.com/kordlib/cache

Download cache-tck

Dependencies

compile (3)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.10
org.jetbrains.kotlinx : kotlinx-coroutines-core-jvm jar 1.4.0
dev.kord.cache : cache-api jar 0.3.0

runtime (4)

Group / Artifact Type Version
org.jetbrains.kotlinx : atomicfu-jvm jar 0.14.4
io.github.microutils : kotlin-logging-jvm jar 2.0.3
org.junit.jupiter : junit-jupiter-api jar 5.5.1
org.jetbrains.kotlinx : kotlinx-serialization-core-jvm jar 1.0.0

Project Modules

There are no modules declared in this project.

DataCache

An adaptable cache that allows query-like operations.

class Person(val id: Int, val age: Int, val name: String)
/**
 * Generates an object that tells caches how to handle this type.
 * Here we indicated that instances of MyType should be indexed on their `id` field.
 */
val description = description(Person::id) 

val cache = MapDataCache() //A `DataCache` that's backed by a ConcurrentMap. Every `DataCache` can support an arbitrary number of types.
 
cache.register(description)  //Types need to be registered before a cache can use them, this provides an initial setup.

cache.put(Person(500, 24, "Test Dummy"))

val entry = cache.find<Person> { Person::id eq 500 }.single()
cache.find<Person>().update { it.copy(age = it.age + 1) }
cache.find<Person>().remove()   

Cascading

DataCache supports cascading of entities linked by properties.

class User(val id: Int, val name: String)
class Message(val id: Int, val userId: Int, val content: String)

val userDescription = description(User::id) {
    link(User::id to Message::userId) //will remove messages with the same userId as the removed User's id.
}
val messageDescription = description(Message::id)

val cache = MapDataCache {
    forType<Message> { lruHashMap(100) } //only keep the latest 100 messages
}

cache.register(userDescription, messageDescription)
cache.put(User(500, "Test Dummy"))
cache.put(Message(400, 500, "Hello world"))

cache.find<User> { User::id eq 500 }.remove()
val messages = cache.find<Message>().count()

assert(messages == 0)

Annotation processor

DataDescriptions can be automatically generated with the annotation-processor module.

//creates `userDescription` and `messageDescription`

class User(
    @Identity
    @Link(Message::class, "userId")
    val id: Int, 
    val name: String
)

class Message(
    @Identity
    val id: Int, 
    val userId: Int, 
    val content: String
)

Note that there is currently an issue with kapt relating to repeatable annotations. Because of that entities with multiple @Link annotations on the same property will not generate those links.

dev.kord.cache

Kord

Versions

Version
0.3.0