Struct actix_storage_redis::RedisBackend
source · pub struct RedisBackend { /* private fields */ }
Expand description
An implementation of ExpiryStore
based on redis
using redis-rs async runtime
Example
use actix_storage::Storage;
use actix_storage_redis::{RedisBackend, ConnectionInfo,RedisConnectionInfo, ConnectionAddr};
use actix_web::{App, HttpServer};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
const THREADS_NUMBER: usize = 4;
let store = RedisBackend::connect_default();
// OR
let connection_info = ConnectionInfo {
addr: ConnectionAddr::Tcp("127.0.0.1".to_string(), 1234).into(),
redis: RedisConnectionInfo{
db: 0,
username: Some("god".to_string()),
password: Some("bless".to_string()),
}
};
let store = RedisBackend::connect(connection_info).await.expect("Redis connection failed");
let storage = Storage::build().expiry_store(store).finish();
let server = HttpServer::new(move || {
App::new()
.data(storage.clone())
});
server.bind("localhost:5000")?.run().await
}
requires [“actor”] feature
Implementations§
source§impl RedisBackend
impl RedisBackend
sourcepub async fn connect(connection_info: ConnectionInfo) -> RedisResult<Self>
pub async fn connect(connection_info: ConnectionInfo) -> RedisResult<Self>
Connect using the provided connection info
sourcepub async fn connect_default() -> RedisResult<Self>
pub async fn connect_default() -> RedisResult<Self>
Connect using the default redis port on local machine
Trait Implementations§
source§impl Clone for RedisBackend
impl Clone for RedisBackend
source§fn clone(&self) -> RedisBackend
fn clone(&self) -> RedisBackend
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Expiry for RedisBackend
impl Expiry for RedisBackend
source§fn persist<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn persist<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Remove all expiry requests from a key and make it persistent,
the persistenty can be overwriten by calling expire on the key.
source§fn expiry<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<Option<Duration>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn expiry<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<Option<Duration>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Gets expiry for a key, returning None means it doesn’t have an expiry,
if the provider can’t return an expiry, it should return an error instead.
The result of this function can have some error, but it should be documented.
source§fn expire<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
expire_in: Duration
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn expire<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
expire_in: Duration
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Sets an expiry for a key, the key may or may not be removed based on
implementation, but it should be guaranteed that it won’t appear in
get based methods or contains checks after the period specified.
source§fn extend<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
expire_in: Duration
) -> Pin<Box<dyn Future<Output = Result<(), StorageError>> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
Self: 'async_trait,
fn extend<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
expire_in: Duration
) -> Pin<Box<dyn Future<Output = Result<(), StorageError>> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
Self: 'async_trait,
Extend expiry for a key for another duration of time.
If the key doesn’t have an expiry, it should be equivalent of calling expire.
source§fn set_called<'life0, 'async_trait>(
&'life0 self,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
Self: 'async_trait,
fn set_called<'life0, 'async_trait>(
&'life0 self,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
Self: 'async_trait,
A notification that should be implemented if expiry is a different entity that
store itself to remove expiry when set is called for a key.
source§impl ExpiryStore for RedisBackend
impl ExpiryStore for RedisBackend
source§fn set_expiring<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
value: Arc<[u8]>,
expire_in: Duration
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_expiring<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
value: Arc<[u8]>,
expire_in: Duration
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Set a key-value for a duration of time, if the key already exists, it should overwrite
both the value and the expiry for that key.
source§fn get_expiring<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<Option<(Arc<[u8]>, Option<Duration>)>, StorageError>> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
Self: 'async_trait,
fn get_expiring<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<Option<(Arc<[u8]>, Option<Duration>)>, StorageError>> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
Self: 'async_trait,
Get the value and expiry for a key, it is possible to return None if the key doesn’t exist,
or return None for the expiry if the key is persistent.
source§impl Store for RedisBackend
impl Store for RedisBackend
source§fn set<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
value: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>,
value: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Set a key-value pair, if the key already exist, value should be overwritten
source§fn get<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<Option<Arc<[u8]>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get<'life0, 'async_trait>(
&'life0 self,
scope: Arc<[u8]>,
key: Arc<[u8]>
) -> Pin<Box<dyn Future<Output = Result<Option<Arc<[u8]>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Get a value for specified key, it should result in None if the value does not exist