Store icon indicating copy to clipboard operation
Store copied to clipboard

Shared disk cache

Open fabioCollini opened this issue 7 years ago • 1 comments

I am using many stores with a disk cache, usually I define the PathResolver as a toString of the parameters. Using this pathResolver there is an error when two stores use the same input parameter. This case can happen, for example in my app I have a store to retrieve the weather condition and another to retrieve the forecast, both stores have the city as input. The problem can be reproduced using this Activity:

data class Weather(val weather: String)

data class Forecast(val forecast: String)

class StoreTestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val weatherStore = StoreBuilder.parsedWithKey<String, BufferedSource, Weather>()
                .fetcher { Single.just(Buffer().writeUtf8("""{"weather":"abc"}""")) }
                .persister(FileSystemPersister.create(FileSystemFactory.create(filesDir), { it }))
                .parser(GsonParserFactory.createSourceParser(Gson(), Weather::class.java))
                .open()

        val forecastStore = StoreBuilder.parsedWithKey<String, BufferedSource, Forecast>()
                .fetcher { Single.just(Buffer().writeUtf8("""{"forecast":"def"}""")) }
                .persister(FileSystemPersister.create(FileSystemFactory.create(filesDir), { it }))
                .parser(GsonParserFactory.createSourceParser(Gson(), Forecast::class.java))
                .open()

        val textView = TextView(this)
        setContentView(textView)

        weatherStore.get("myCity")
                .flatMap { weather -> forecastStore.get("myCity").map { forecast -> weather to forecast } }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    textView.text = it.toString()
                }, {
                    it.printStackTrace()
                })
    }
}

Unfortunately I don't get an error but the forecast fields are null (even if are defined as not nullable in the Kotlin class!):

(Weather(weather=abc), Forecast(forecast=null))

The problem can be fixed easily using a prefix in the string returned by PathResolver.

fabioCollini avatar Nov 07 '17 20:11 fabioCollini

Another option here could be to use different flatDir for each different Store. Or, as you proposed, it could be nice to add a newPathResolver subclass that uses the Store name as a prefix (or containing directory, similar to how BarCodeReadAllPathResolver works). Feel free to submit a PR if this is something you're interested in.

ramonaharrison avatar Jan 12 '18 17:01 ramonaharrison