Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kaliturin opened this issue Feb 5, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@kaliturin
Copy link

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 kaliturin added the bug Something isn't working label Feb 5, 2023
@matt-ramotar
Copy link
Collaborator

Hey there - Sorry to be slow. Thanks for raising. Where is requestData() being called?

@kaliturin
Copy link
Author

Hey there - Sorry to be slow. Thanks for raising. Where is requestData() being called?

I called from a button's listener in a Fragment. But I suppose the result would be the same in any view which implements LifecycleOwner.

@matt-ramotar
Copy link
Collaborator

Hmm I wanted to rule out recomposition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Investigating
Development

No branches or pull requests

2 participants