Skip to content

domgew/kop

Repository files navigation

KOP - Kotlin Object Pool

Maven Central Latest Tag Publish Test Kotlin Licence: MIT

KOP is a Kotlin Multiplatform object pool.

Installation

dependencies {
    // ...

    implementation("io.github.domgew:kop:<current_version>")

    // OR just for JVM:
    implementation("io.github.domgew:kop-jvm:<current_version>")

    // ...
}
repositories {
    mavenCentral()

    // ...
}

Documentation

See Dokka-generated docs.

Quick Start

typealias ObjectType = Any

suspend fun createObjectInstance(): ObjectType =
    TODO()

val objectPool = KotlinObjectPool(
    config = KotlinObjectPoolConfig(
        maxSize = 4,
        keepAliveFor = 1.minutes,
        strategy = KotlinObjectPoolStrategy.LIFO,
    ),
    // ...
) {
    createObjectInstance()
}

// OR

val objectPool = KotlinObjectPool.build {
    maxSize(4)
    keepAliveFor(1.minutes)
    strategy(KotlinObjectPoolStrategy.LIFO)
    // ...
    createInstance {
        createObjectInstance()
    }
}

suspend fun callObject() =
    objectPool.withObject { instance ->
        instance.call()
    }

Targets

Supported Targets:

  • JVM
  • JS: Browser
  • JS: NodeJS
  • wasmJS: Browser
  • wasmJS: NodeJS
  • Native: Android ARM64
  • Native: Android ARM32
  • Native: Android X64
  • Native: Android X86
  • Native: Linux X64
  • Native: Linux ARM64
  • Native: macOS X64
  • Native: macOS ARM64
  • Native: mingw X64
  • Native: iOS X64
  • Native: iOS ARM64
  • Native: watchOS X64
  • Native: watchOS ARM64
  • Native: tvOS X64
  • Native: tvOS ARM64

Examples

Kedis - Kotlin Multiplatform Redis Cache

val kedisConfiguration: KedisConfiguration = TODO()

suspend fun KedisClient.getFromCache(): String =
    TODO()

val objectPool = KotlinObjectPool(
    KotlinObjectPoolConfig(
        maxSize = 4,
        keepAliveFor = 1.minutes,
        strategy = KotlinObjectPoolStrategy.LIFO,
    ),
) {
    KedisClient(kedisConfiguration)
}

suspend fun getValueWithCache() =
    objectPool.withObject { kedisClient: KedisClient ->
        kedisClient.getFromCache()
    }

About

Kotlin Multiplatform Object Pool

Resources

License

Stars

Watchers

Forks

Contributors

Languages