Actors

Our actor has to keep an instance of Client. We don't use a connection pool, because we will use multiple actors for handing parallel requests to a database. Look at the following struct:

pub struct CacheActor {
client: Client,
expiration: usize,
}

The struct also contains an expiration field that holds the time-to-live (TTL) period. This defines how long Redis will hold the value.

Add a new method to the implementation that uses a provided address string to create a Client instance, and adds both the client and expiration values to the CacheActor struct, as follows:

impl CacheActor {
pub fn new(addr: &str, expiration: usize) -> Self {
let client = Client::open(addr).unwrap();
Self { client, expiration }
}
}

Also, we have to implement an Actor trait for SyncContext, just as we did when resizing the worker in Chapter 10, Background Tasks and Thread Pools in Microservices:

impl Actor for CacheActor {
type Context = SyncContext<Self>;
}

Now, we can add support for messages to set and get cached values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.140