Slide 20
Slide 20 text
ExposedCachedRepository
interface ExposedCacheRepository, ID: Any> {
val cacheName: String
val entityTable: IdTable
fun ResultRow.toEntity(): T
val cache: RMap
fun exists(id: ID): Boolean = cache.containsKey(id)
fun findFreshById(id: ID): T? =
entityTable.selectAll().where { entityTable.id eq id }.singleOrNull()?.toEntity()
fun findFreshAll(vararg ids: ID): List =
entityTable.selectAll().where { entityTable.id inList ids.toList() }.map { it.toEntity() }
fun findFreshAll(ids: Collection): List =
entityTable.selectAll().where { entityTable.id inList ids }.map { it.toEntity() }
fun get(id: ID): T? = cache[id]
fun findAll(
limit: Int? = null,
offset: Long? = null,
sortBy: Expression<*> = entityTable.id,
sortOrder: SortOrder = SortOrder.ASC,
where: SqlExpressionBuilder.() -> Op = { Op.TRUE },
): List
fun getAll(ids: Collection, batchSize: Int = 100): List
fun put(entity: T) = cache.fastPut(entity.id, entity)
fun putAll(entities: Collection, batchSize: Int = 100) {
cache.putAll(entities.associateBy { it.id }, batchSize)
}
fun invalidate(vararg ids: ID): Long = cache.fastRemove(*ids)
fun invalidateAll() = cache.clear()
fun invalidateByPattern(patterns: String, count: Int = 100): Long {
val keys = cache.keySet(patterns, count)
return cache.fastRemove(*keys.toVarargArray())
}
}