MobileNativeFoundation / MobileNativeFoundation/Store

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

Open
#513 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Kotlin
Stars
3.4k
Forks
217
Avg merge
18m
Merged PRs (30d)
4

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with the StoreBuilder, MemoryPolicy, and StoreRequest.cached example, then trace how repeated stream calls are handled when the 10-second cache expires. Check the fetcher and response flow around expiration. Done means a final request produces one fetcher response rather than repeating it for earlier calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.