January 2019
Intermediate to advanced
520 pages
14h 32m
English
In the previous example in this chapter, we used shared State to provide access to a counter stored as i64, wrapped with RefCell. We reuse this struct, but add a CacheLink field to use connections with a CacheActor to get or set cached values. Add this field:
struct State { counter: RefCell<i64>, cache: CacheLink,}
We derived a Default trait for the State struct before, but now we need a new constructor, because we have to provide a CacheLink instance with the actual address of the caching actor:
impl State { fn new(cache: CacheLink) -> Self { Self { counter: RefCell::default(), cache, } }}
In most cases, caching works this way—it tries to extract a value from a cache; if it exists and hasn't expired, then the value ...
Read now
Unlock full access