Hi :wave: I'm currently playing with the Zipline c...
# squarelibraries
m
Hi 👋 I'm currently playing with the Zipline cache and I'm not able to achieve what I would : • at Zipline start, the loader fetches the server and returns the response if possible • if the fetch is on error Zipline returns the cached value • if no cache Zipline throws How could I achieve that ? Thank you 🙏
j
We don’t have a stale-if-error mechanism, but I’m gonna add one 'cause it’s a good policy to have
You can fake it by calling
loadOnce()
twice, with different
FreshnessChecker
arguments on each call
m
Hi @jessewilson 👋 Thanks for your quick answer ! I tried your idea but I'm facing an another issue 😅 It looks like the
freshAtEpochMs
param in the callback
FreshnessChecker.isFresh
has always the same value until I update the Kotlin/JS code on the server 🤔 I thought the cache will be updated at each successful network fetch. Please find the code below. I execute it at app startup. I observe in the logs that even if the network fetch succeeded, the next isFresh call (at next app startup) will be executed with a non-updated freshAtEpochMs value.
Copy code
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
            }
        }
    }
Do you want me to create an issue on GH ?
j
Oooh that’s unexpected
Sure, please open an issue!
m
Ok thanks 👌