java - Cache filter on collection with Guava -
i'm developing program performs operations on automatas. automata made of states (aka node) , transitions (aka edges) , need filter them retrieve set particular properties. operation easy implement, performed several times , write little cache on it.
in fragment of code below there implementation , i'd know if correct way filter , memoize observable transitions.
public class automata { private state initial; private set <state> states; private set <transition> transitions; private supplier <set <transition>> observables; // ... public automata() { this.initial = new state(); this.states = new hashset <> (); this.transitions = new hashset <> (); this.observables = suppliers.memoize(() -> transitions.stream().filter((t) -> (t.isobservable() == true)).collect(collectors.toset())); } public getobservables() { return observables.get(); } }
questions:
- is correct?
- if transition changes observability information propagate supplier?
i'm sorry poor english, hope enough clear.
- yes, it's correct.
no, changes in transitions won't automatically propagated. , case supplier afaik not suitable. either need overwrite manually here:
public void invalidate(){ memorized = suppliers.memoize(supplier); }
also
memoizewithexpiration
work if know update not frequent , don't need reliable reads.or need use
cache
, example:cacheloader<key, graph> loader = new cacheloader<key, graph>() { public graph load(key key) throws anyexception { return createexpensivegraph(key); } }; loadingcache<key, graph> cache = cachebuilder.newbuilder().build(loader);
Comments
Post a Comment