Struct dtypes::redis::Guard

source ·
pub struct Guard<'a, T> { /* private fields */ }
Expand description

The guard struct for the Mutex. It is used to access the value and not for you to initialize it by your own.

Implementations§

source§

impl<'a, T> Guard<'a, T>where T: Serialize + DeserializeOwned,

source

pub fn expand(&mut self)

Expands the lock time by 2000ms from the point on its called. This is useful if you need to access the value for a longer time.

But use it with caution, because it can lead to deadlocks. To avoid deadlocks, we only allow one extension per lock.

source

pub fn store(&mut self, value: T) -> Result<(), LockError>where T: Serialize,

Stores the value in Redis. This function blocks until the value is stored. Disables the store operation of the guarded value.

source

pub fn acquire(&mut self) -> &T

Loads the value from Redis. This function blocks until the value is loaded. Shadows the load operation of the guarded value.

Methods from Deref<Target = Generic<T>>§

source

pub fn store(&mut self, value: T)

The store method sets the value of the type.

Examples found in repository?
examples/2services.rs (line 15)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    thread::scope(|s| {
        let client = redis::Client::open("redis://localhost:6379").unwrap();
        let client2 = client.clone();

        let t1 = s.spawn(move || {
            let mut string = String::with_value("Hello".to_string(), "test", client);
            println!("Thread1: {}", string.cached().unwrap());
            assert_eq!(string, "Hello");
            sleep(std::time::Duration::from_secs(1));
            string.store("World".to_string());
            println!("Thread1: {}", string.cached().unwrap());
            assert_eq!(string, "World");
        });

        let t2 = s.spawn(move || {
            sleep(std::time::Duration::from_micros(100));
            let mut string = String::with_load("test", client2);
            println!("Thread2: {}", string.cached().unwrap());
            assert_eq!(string, "Hello");
            sleep(std::time::Duration::from_secs(2));
            string.acquire();
            println!("Thread2: {}", string.cached().unwrap());
            assert_eq!(string, "World");
        });
        t1.join().expect("Failed to join thread1");
        t2.join().expect("Failed to join thread2");
    });
}
source

pub fn acquire(&mut self) -> &T

The acquire method returns a reference to the value stored in the type. Loads it from the redis directly.

Example
use dtypes::redis::Di32 as i32;

let client = redis::Client::open("redis://localhost:6379").unwrap();
let mut i32 = i32::with_value(1, "test_add", client.clone());
i32 = i32 + i32::with_value(2, "test_add2", client);
assert_eq!(i32.acquire(), &3);
Examples found in repository?
examples/2services.rs (line 26)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    thread::scope(|s| {
        let client = redis::Client::open("redis://localhost:6379").unwrap();
        let client2 = client.clone();

        let t1 = s.spawn(move || {
            let mut string = String::with_value("Hello".to_string(), "test", client);
            println!("Thread1: {}", string.cached().unwrap());
            assert_eq!(string, "Hello");
            sleep(std::time::Duration::from_secs(1));
            string.store("World".to_string());
            println!("Thread1: {}", string.cached().unwrap());
            assert_eq!(string, "World");
        });

        let t2 = s.spawn(move || {
            sleep(std::time::Duration::from_micros(100));
            let mut string = String::with_load("test", client2);
            println!("Thread2: {}", string.cached().unwrap());
            assert_eq!(string, "Hello");
            sleep(std::time::Duration::from_secs(2));
            string.acquire();
            println!("Thread2: {}", string.cached().unwrap());
            assert_eq!(string, "World");
        });
        t1.join().expect("Failed to join thread1");
        t2.join().expect("Failed to join thread2");
    });
}
source

pub fn cached(&self) -> Option<&T>

The get method returns a reference to the value stored in the type.

Examples found in repository?
examples/2services.rs (line 12)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    thread::scope(|s| {
        let client = redis::Client::open("redis://localhost:6379").unwrap();
        let client2 = client.clone();

        let t1 = s.spawn(move || {
            let mut string = String::with_value("Hello".to_string(), "test", client);
            println!("Thread1: {}", string.cached().unwrap());
            assert_eq!(string, "Hello");
            sleep(std::time::Duration::from_secs(1));
            string.store("World".to_string());
            println!("Thread1: {}", string.cached().unwrap());
            assert_eq!(string, "World");
        });

        let t2 = s.spawn(move || {
            sleep(std::time::Duration::from_micros(100));
            let mut string = String::with_load("test", client2);
            println!("Thread2: {}", string.cached().unwrap());
            assert_eq!(string, "Hello");
            sleep(std::time::Duration::from_secs(2));
            string.acquire();
            println!("Thread2: {}", string.cached().unwrap());
            assert_eq!(string, "World");
        });
        t1.join().expect("Failed to join thread1");
        t2.join().expect("Failed to join thread2");
    });
}

Trait Implementations§

source§

impl<T> Deref for Guard<'_, T>where T: DeserializeOwned + Serialize,

§

type Target = Generic<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for Guard<'_, T>where T: DeserializeOwned + Serialize,

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T> Drop for Guard<'_, T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for Guard<'a, T>

§

impl<'a, T> Send for Guard<'a, T>where T: Send,

§

impl<'a, T> Sync for Guard<'a, T>where T: Sync,

§

impl<'a, T> Unpin for Guard<'a, T>

§

impl<'a, T> !UnwindSafe for Guard<'a, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.