[][src]Struct actix_storage::Storage

pub struct Storage { /* fields omitted */ }

Takes the underlying backend and provides common methods for it

It can be stored in actix_web's Data and be used from handlers without specifying the backend itself and provides all the common methods from underlying store and expiry. The backend this struct holds should implement ExpiryStore either directly, or by depending on the default polyfill. Look StorageBuilder for more details.

Example

use actix_storage::Storage;
use actix_web::*;

async fn index(storage: web::Data<Storage>) -> Result<String, Error>{
    storage.set_bytes("key", "value").await;
    let val = storage.get_bytes("key").await?.unwrap_or_default();
    Ok(std::str::from_utf8(&val)
        .map_err(|err| error::ErrorInternalServerError("Storage error"))?.to_string())
}

It is also possible to set and get values directly using serde by enabling with-serde feature flag.

Implementations

impl Storage[src]

pub fn build() -> StorageBuilder[src]

Returns the storage builder struct

pub async fn set<V, '_, '_>(
    &'_ self,
    key: impl AsRef<[u8]>,
    value: &'_ V
) -> Result<()> where
    V: Serialize
[src]

Stores a generic serializable value on storage using serde

Calling set operations twice on the same key, overwrites it's value and clear the expiry on that key(if it exist).

Example

storage.set("age", &60_u8).await;

Errors

Beside the normal errors caused by the storage itself, it will result in error if serialization fails.

Note: it required the value to be Sized as some of the serde extensions currently has the same requirement, this restriction may be lifted in future.

requires "with-serde" feature and one of the format features to work ex. "serde-json"

pub async fn set_expiring<V, '_, '_>(
    &'_ self,
    key: impl AsRef<[u8]>,
    value: &'_ V,
    expires_in: Duration
) -> Result<()> where
    V: Serialize
[src]

Stores a generic serializable value on storage using serde and sets expiry on the key It should be prefered over explicity setting a value and putting an expiry on it as providers may provide a more optimized way to do both operations at once.

Calling set operations twice on the same key, overwrites it's value and clear the expiry on that key(if it exist).

Example

storage.set_expiring("age", &60_u8, Duration::from_secs(10)).await;

Errors

Beside the normal errors caused by the storage itself, it will result in error if expiry provider is not set or serialization fails.

Note: it required the value to be Sized as some of the serde extensions currently has the same requirement, this restriction may be lifted in future.

requires "with-serde" feature and one of the format features to work ex. "serde-json"

pub async fn set_bytes<'_>(
    &'_ self,
    key: impl AsRef<[u8]>,
    value: impl AsRef<[u8]>
) -> Result<()>
[src]

Stores a sequence of bytes on storage

Calling set operations twice on the same key, overwrites it's value and clear the expiry on that key(if it exist).

Example

storage.set_bytes("age", vec![10]).await;
storage.set_bytes("name", "Violet".as_bytes()).await;

pub async fn set_expiring_bytes<'_>(
    &'_ self,
    key: impl AsRef<[u8]>,
    value: impl AsRef<[u8]>,
    expires_in: Duration
) -> Result<()>
[src]

Stores a sequence of bytes on storage and sets expiry on the key It should be prefered over calling set and expire as providers may define a more optimized way to do both operations at once.

Calling set operations twice on the same key, overwrites it's value and clear the expiry on that key(if it exist).

Example

storage.set_expiring_bytes("name", "Violet".as_bytes(), Duration::from_secs(10)).await;

Errors

Beside the normal errors caused by the storage itself, it will result in error if expiry provider is not set.

pub async fn get<K, V, '_>(&'_ self, key: K) -> Result<Option<V>> where
    K: AsRef<[u8]>,
    V: DeserializeOwned
[src]

Gets a generic deserializable value from backend using serde

Example

let val: Option<String> = storage.get("key").await?;

Errors

Beside the normal errors caused by the storage itself, it will result in error if deserialization fails.

requires "with-serde" feature and one of the format features to work ex. "serde-json"

pub async fn get_expiring<K, V, '_>(
    &'_ self,
    key: K
) -> Result<Option<(V, Option<Duration>)>> where
    K: AsRef<[u8]>,
    V: DeserializeOwned
[src]

Gets a generic deserializable value from backend using serde together with its expiry It should be prefered over calling get and expiry as providers may define a more optimized way to do the both operations at once.

Example

let val: Option<(String, _)> = storage.get_expiring("key").await?;

Errors

Beside the normal errors caused by the storage itself, it will result in error if expiry provider is not set or deserialization fails.

requires "with-serde" and one of the format features to work ex. "serde-json"

pub async fn get_bytes<'_>(
    &'_ self,
    key: impl AsRef<[u8]>
) -> Result<Option<Vec<u8>>>
[src]

Gets a sequence of bytes from backend, resulting in an owned vector

Example

let val = storage.get_bytes("key").await?;

pub async fn get_expiring_bytes<'_>(
    &'_ self,
    key: impl AsRef<[u8]>
) -> Result<Option<(Vec<u8>, Option<Duration>)>>
[src]

Same as get_bytes but it also gets expiry.

Example

let val = storage.get_expiring_bytes("key").await?;

pub async fn get_bytes_ref<'_>(
    &'_ self,
    key: impl AsRef<[u8]>
) -> Result<Option<Arc<[u8]>>>
[src]

Gets a sequence of bytes from backend, resulting in an arc

Example

let val = storage.get_bytes_ref("key").await?;

pub async fn get_expiring_bytes_ref<'_>(
    &'_ self,
    key: impl AsRef<[u8]>
) -> Result<Option<(Arc<[u8]>, Option<Duration>)>>
[src]

Same as get_bytes_ref but it also gets expiry.

Example

let val = storage.get_expiring_bytes_ref("key").await?;

pub async fn delete<'_>(&'_ self, key: impl AsRef<[u8]>) -> Result<()>[src]

Deletes/Removes a key value pair from storage.

Example

storage.delete("key").await?;

pub async fn contains_key<'_>(&'_ self, key: impl AsRef<[u8]>) -> Result<bool>[src]

Checks if storage contains a key.

Example

let exist = storage.contains_key("key").await?;

pub async fn expire<'_>(
    &'_ self,
    key: impl AsRef<[u8]>,
    expire_in: Duration
) -> Result<()>
[src]

Sets expiry on a key, it won't result in error if the key doesn't exist.

Calling set methods twice or calling persist will result in expiry being erased from the key, calling expire itself twice will overwrite the expiry for key.

Example

storage.expire("key", Duration::from_secs(10)).await?;

pub async fn expiry<'_>(
    &'_ self,
    key: impl AsRef<[u8]>
) -> Result<Option<Duration>>
[src]

Gets expiry for the provided key, it will give none if there is no expiry set.

The result of this method is not guaranteed to be exact and may be inaccurate depending on sotrage implementation.

Example

let exp = storage.expiry("key").await?;
if let Some(exp) = exp{
    println!("Key will expire in {} seconds", exp.as_secs());
} else {
    println!("Long live the key");
}

pub async fn extend<'_>(
    &'_ self,
    key: impl AsRef<[u8]>,
    expire_in: Duration
) -> Result<()>
[src]

Extends expiry for a key, it won't result in error if the key doesn't exist.

If the provided key doesn't have an expiry set, it will set the expiry on that key.

Example

storage.expire("key", Duration::from_secs(5)).await?;
storage.extend("key", Duration::from_secs(5)).await?; // ket will expire in ~10 seconds

pub async fn persist<'_>(&'_ self, key: impl AsRef<[u8]>) -> Result<()>[src]

Clears expiry from the provided key, making it persistant.

Calling expire will overwrite persist.

Example

storage.persist("key").await?;

Trait Implementations

impl Clone for Storage[src]

Auto Trait Implementations

impl !RefUnwindSafe for Storage

impl Send for Storage

impl Sync for Storage

impl Unpin for Storage

impl !UnwindSafe for Storage

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,