Store icon indicating copy to clipboard operation
Store copied to clipboard

[BUG] After memory cache expiration I receive fetched data several times

Open kaliturin opened this issue 2 years ago • 3 comments

Suppose I have simple store fetching random number with in memory cache that lives 10 seconds:

 @OptIn(ExperimentalTime::class)
    private val store by lazy {
        StoreBuilder
            .from(Fetcher.of { Random.nextInt() % 100 })
            .cachePolicy(
                MemoryPolicy.builder<Any, Int>()
                    .setExpireAfterWrite(10.seconds)
                    .build()
            )
            .build()
    }

And simple request data function that I can call several times hoping to receive data from cache if it's not expired or from fetcher else:

 private fun requestData() {
        Timber.d("requestData() called")
        lifecycleScope.launch {
            store.stream(StoreRequest.cached(0, false))
                .collect { response ->
                    if (response is StoreResponse.Data) {
                        when (response.origin) {
                            ResponseOrigin.Fetcher -> Timber.d("Fetcher: ${response.value}")
                            ResponseOrigin.Cache -> Timber.d("Cache: ${response.value}")
                            else -> {}
                        }
                    }
                }
        }
    }

I hope to receive one response on every call requestData(). And it works as supposed when cache isn't expired:

00:57:14.062 D requestData() called 00:57:14.110 D Fetcher: -81 00:57:19.162 D requestData() called 00:57:19.163 D Cache: -81 00:57:21.293 D requestData() called 00:57:21.296 D Cache: -81

But after cache expiration - I receive as many responses from fetcher as many calls I made before the expiration:

00:57:27.627 D requestData() called 00:57:27.634 D Fetcher: 39 00:57:27.635 D Fetcher: 39 00:57:27.635 D Fetcher: 39 00:57:27.637 D Fetcher: 39

Device: various OS: 11 Store Version: [5.0.0-alpha03]

kaliturin avatar Feb 05 '23 22:02 kaliturin