cashapp / zipline

Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs
Apache License 2.0
1.98k stars 151 forks source link

Cache freshness is not updated on successful network fetch #1338

Open mariolourobert opened 3 weeks ago

mariolourobert commented 3 weeks ago

On Zipline v1.10.1

Running the code below, it looks like the cache freshness is not updated even through successful fetches, until the Kotlin/JS code exposed by the server is actually updated.

To reproduce:

suspend fun launchZipline(dispatcher: CoroutineDispatcher): Zipline? {
        val manifestUrl = "http://10.0.2.2:8080/manifest.zipline.json"
        val ziplineCache = ZiplineCache(
            applicationContext,
            FileSystem.SYSTEM,
            applicationContext.cacheDir.toOkioPath(),
            10 * 1024 * 1024, // 10MB
        )
        val loader = ZiplineLoader(
            dispatcher,
            ManifestVerifier.NO_SIGNATURE_CHECKS,
            OkHttpClient(),
        ).withCache(ziplineCache)

        val cacheDuration = Duration.ofMinutes(1L)

        val freshnessChecker = object : FreshnessChecker {
            override fun isFresh(manifest: ZiplineManifest, freshAtEpochMs: Long): Boolean =
                (System.currentTimeMillis() - freshAtEpochMs < cacheDuration.toMillis())
                    .also {
                        Log.d("Zipline", "freshAtEpochMs: $freshAtEpochMs")
                        Log.d("Zipline", "Is the cache fresh? $it")
                    }
        }

        return when (
            val result = loader.loadOnce(AppZiplineInterfaceName, freshnessChecker, manifestUrl)
        ) {
            is LoadResult.Success -> {
                Log.d("Zipline", "Zipline loaded freshAtEpochMs: ${result.freshAtEpochMs}")
                result.zipline
            }
            is LoadResult.Failure -> {
                Log.e("Zipline", "Failed to load Zipline \n ${result.exception.message}")
                null
            }
        }
    }