Slide 18
Slide 18 text
R2dbcCachedRepository
interface R2dbcCacheRepository, ID: Any> {
val cacheName: String
val entityTable: IdTable
suspend fun ResultRow.toEntity(): T
val cache: RMap
suspend fun exists(id: ID): Boolean = cache.containsKeyAsync(id).coAwait()
suspend fun findFreshById(id: ID): T? =
entityTable.selectAll().where { entityTable.id eq id }.singleOrNull()?.toEntity()
suspend fun findFreshAll(vararg ids: ID): List =
entityTable.selectAll().where { entityTable.id inList ids.toList() }.map { it.toEntity() }.toList()
suspend fun findFreshAll(ids: Collection): List =
entityTable.selectAll().where { entityTable.id inList ids }.map { it.toEntity() }.toList()
suspend fun findAll(limit: Int? = null, offset: Long? = null, sortBy: Expression<*> = entityTable.id,
sortOrder: SortOrder = SortOrder.ASC,
where: SqlExpressionBuilder.() -> Op = { Op.TRUE },
): List
suspend fun get(id: ID): T? = cache.getAsync(id).coAwait()
suspend fun getAll(ids: Collection, batchSize: Int = DefaultBatchSize): List
suspend fun put(entity: T): Boolean? = cache.fastPutAsync(entity.id, entity).coAwait()
suspend fun putAll(entities: Collection, batchSize: Int = DefaultBatchSize) {
cache.putAllAsync(entities.associateBy { it.id }, batchSize).coAwait()
}
suspend fun invalidate(vararg ids: ID): Long = cache.fastRemoveAsync(*ids).coAwait()
suspend fun invalidateAll(): Boolean = cache.clearAsync().coAwait()
suspend fun invalidateByPattern(patterns: String, count: Int = DefaultBatchSize): Long {
val keys = cache.keySet(patterns, count)
return cache.fastRemoveAsync(*keys.toTypedArray()).coAwait()
}
}
R2dbcCacheRepository