Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Recipes

Mike Nakhimovich edited this page Jan 19, 2017 · 20 revisions

Welcome to the Store Recipes!

Cached then fresh data

First call get and concat the result with fetch. Next add a distinct operator which will only allow unique emissions. If cache and network return same value or if the first get call actually needs to go to network, only 1 item will be emitted.

store.get(barCode)
                .concatWith(store.fetch(barCode))
                .distinct()
                .subscribe()

MultiParser

Sometimes you want to use a middleware parser to go from json to pojo and then use another parser to unwrap your data.

       Parser<BufferedSource, RedditData> sourceParser = GsonParserFactory.createSourceParser(provideGson(), RedditData.class);
       Parser<RedditData, Data> envelopeParser = redditData -> redditData.data();

       ParsingStoreBuilder.<BufferedSource,RedditData>builder()
               .fetcher(this::fetcher)
               .persister(persister)
               .parser(new MultiParser<>(Arrays.asList(sourceParser,envelopeParser)))
               .open();

####Top Level JSON Array In some cases you may need to parse a top level JSONArray, in which case you can provide a TypeToken.

Store<List<Article>> Store = ParsingStoreBuilder.<BufferedSource, List<Article>>builder()
                .nonObservableFetcher(this::getResponse)
                .parser(GsonParserFactory.createSourceParser(gson, new TypeToken<List<Article>>() {}))
                .open();

###Custom Memory Caching Policy By default stores will cache up to 100 items for 24 hours using an expireAfterAccess policy. You can pass in your own Cache instance to override the duration and type of caching policy. The below example will cache 1 item forever allowing you to keep the memory footprint of the store lower

ParsingStoreBuilder.<BufferedSource,RedditData>builder()
                .fetcher(this::fetcher)
                .persister(persister)
                .parser(GsonParserFactory.createSourceParser(provideGson(),RedditData.class))
                .memory(CacheBuilder.newBuilder()
                        .maximumSize(1)
                        .expireAfterWrite(Long.MAX_VALUE, TimeUnit.SECONDS)
                        .build())
                .open();
Clone this wiki locally