Skip to content

Instantly share code, notes, and snippets.

@dave08
dave08 / Cacheable.kt
Last active October 17, 2023 12:24
Cacheable
import io.lettuce.core.api.StatefulRedisConnection
import kotlinx.coroutines.future.await
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import kotlin.time.Duration
enum class ExpiryType {
none, after_write, after_access
Tue Jun 25 16:58:14 UTC 2019
fun JsonReader.skipNameAndValue() {
skipName()
skipValue()
}
fun JsonReader.nextLongOrNull() =
if (peek() == JsonReader.Token.NULL) {
nextNull<Long>()
null
} else
val Connection.asSuspendable get(): SuspendableConnection = SuspendableConnectionImpl(this)
interface SuspendableConnection {
/**
*
* Disconnects this object. You should discard this object after calling this method. No more queries
* will be accepted.
*
* @return
import io.ktor.application.*
import io.ktor.pipeline.*
import io.ktor.routing.*
import io.ktor.util.*
import io.micrometer.core.instrument.*
import io.micrometer.core.instrument.binder.MeterBinder
import io.micrometer.core.instrument.binder.jvm.*
import io.micrometer.core.instrument.binder.system.*
import java.util.concurrent.atomic.AtomicInteger
@ContextDsl
fun Route.coGet(path: String, block: suspend ApplicationCall.() -> ServerResponse): Route = get("/") {
val result = call.block()
when (result) {
is ServerResponse.Empty ->
call.respondText { "" }
is ServerResponse.Json ->
call.respondText(result.jsonString, ContentType.Application.Json)
fun Route.coHandler(controller: suspend RoutingContext.() -> ServerResponse): Route =
handler {
launch(it.vertx().dispatcher()) {
val response = it.controller()
when (response) {
is ServerResponse.Empty ->
it.response().end()
is ServerResponse.Json ->
import android.content.Context
import com.gun0912.tedpermission.PermissionListener
import com.gun0912.tedpermission.TedPermission
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
import timber.log.Timber
import java.util.ArrayList
fun <T> Context.withPermission(permission: String, block: () -> T) = async(start = CoroutineStart.LAZY) {
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.*
import org.junit.Test
import kotlin.coroutines.experimental.CoroutineContext
data class Intent(val action: String)
class BroadcastSender() {
}
import io.vertx.core.json.JsonArray
import io.vertx.ext.sql.ResultSet
import io.vertx.ext.sql.SQLConnection
import io.vertx.ext.sql.UpdateResult
import io.vertx.kotlin.core.json.JsonArray
import io.vertx.kotlin.core.json.get
import io.vertx.kotlin.coroutines.awaitResult
import io.vertx.ext.sql.SQLClient as VertxSQLClient
// ---- Db access