Skip to content

Introduce default creation method/setting, instead of always requiring lambda at getOrPut #128

@carstenhag

Description

@carstenhag

Is your feature request related to a problem? Please describe.
I want to create a bitmap cache (ie val bitmapCache = InMemoryKache<Int, Bitmap>).
If I run bitmapCache.get(1) I want to get the data. If it doesn't exist, it should automatically run a suspending function first, then return the value.

getOrPut theoretically does this, but I would have to tediously copy over the creationFunction function to all places where I want to get values.

Describe the solution you'd like
The Configuration could accept a factory lambda.

Describe alternatives you've considered
Define a lambda like this once, and reference it in all getOrPut calls:

    val bitmapConstruction = { resId: Int ->
        bitmapHelper.getBitmap(resId, context)
    }
    bitmapCache.getOrPut(R.drawable.xyz, bitmapConstruction)

Additional context
LruCache has a way to override the create method to do just this. But it's written in Java and doesn't support coroutines etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions