pub struct Generic<T> { /* private fields */ }Expand description
The generic type is used to implement the common methods for all types.
The generic type is not meant to be used directly. Instead use one of the aliases.
Mostly you will interact with the methods Generic::store, Generic::acquire and Generic::into_inner.
Implementations§
Source§impl<T> Generic<T>where
T: Serialize + DeserializeOwned,
impl<T> Generic<T>where
T: Serialize + DeserializeOwned,
Sourcepub fn new(field_name: &str, client: Client) -> Generic<T>
pub fn new(field_name: &str, client: Client) -> Generic<T>
The new method creates a new instance of the type. It does not load or store any value in Redis. It only creates the instance.
§Example
use dtypes::redis::types::Di32 as i32;
let client = redis::Client::open("redis://localhost:6379").unwrap();
let mut i32 = i32::new("test_add", client.clone());
i32.store(1);
let i32 = i32 + i32::with_value(2, "test_add2", client);
assert_eq!(i32, 3);Sourcepub fn with_value(value: T, field_name: &str, client: Client) -> Generic<T>
pub fn with_value(value: T, field_name: &str, client: Client) -> Generic<T>
The with_value method creates a new instance of the type. If a value is already stored in Redis, it will be overwritten.
Examples found in repository?
5fn main() {
6 thread::scope(|s| {
7 let client = redis::Client::open("redis://localhost:6379").unwrap();
8 let client2 = client.clone();
9
10 let t1 = s.spawn(move || {
11 let mut string = String::with_value("Hello".to_string(), "test", client);
12 println!("Thread1: {}", string.cached().unwrap());
13 assert_eq!(string, "Hello");
14 sleep(std::time::Duration::from_secs(1));
15 string.store("World".to_string());
16 println!("Thread1: {}", string.cached().unwrap());
17 assert_eq!(string, "World");
18 });
19
20 let t2 = s.spawn(move || {
21 sleep(std::time::Duration::from_micros(100));
22 let mut string = String::with_load("test", client2);
23 println!("Thread2: {}", string.cached().unwrap());
24 assert_eq!(string, "Hello");
25 sleep(std::time::Duration::from_secs(2));
26 string.acquire();
27 println!("Thread2: {}", string.cached().unwrap());
28 assert_eq!(string, "World");
29 });
30 t1.join().expect("Failed to join thread1");
31 t2.join().expect("Failed to join thread2");
32 });
33}Sourcepub fn with_load(field_name: &str, client: Client) -> Generic<T>
pub fn with_load(field_name: &str, client: Client) -> Generic<T>
The with_value_load method creates a new instance of the type. It loads the value from Redis. If there is no value stored in Redis, it stores a None in cache.
Examples found in repository?
5fn main() {
6 thread::scope(|s| {
7 let client = redis::Client::open("redis://localhost:6379").unwrap();
8 let client2 = client.clone();
9
10 let t1 = s.spawn(move || {
11 let mut string = String::with_value("Hello".to_string(), "test", client);
12 println!("Thread1: {}", string.cached().unwrap());
13 assert_eq!(string, "Hello");
14 sleep(std::time::Duration::from_secs(1));
15 string.store("World".to_string());
16 println!("Thread1: {}", string.cached().unwrap());
17 assert_eq!(string, "World");
18 });
19
20 let t2 = s.spawn(move || {
21 sleep(std::time::Duration::from_micros(100));
22 let mut string = String::with_load("test", client2);
23 println!("Thread2: {}", string.cached().unwrap());
24 assert_eq!(string, "Hello");
25 sleep(std::time::Duration::from_secs(2));
26 string.acquire();
27 println!("Thread2: {}", string.cached().unwrap());
28 assert_eq!(string, "World");
29 });
30 t1.join().expect("Failed to join thread1");
31 t2.join().expect("Failed to join thread2");
32 });
33}Sourcepub fn with_value_default(
value: T,
field_name: &str,
client: Client,
) -> Generic<T>
pub fn with_value_default( value: T, field_name: &str, client: Client, ) -> Generic<T>
The with_value_default method creates a new instance of the type. If the value is not already stored in Redis, it will be stored. If the value is already stored in Redis, it will be loaded and your given value will be ignored.
Sourcepub fn store(&mut self, value: T)
pub fn store(&mut self, value: T)
The store method sets the value of the type.
Examples found in repository?
5fn main() {
6 thread::scope(|s| {
7 let client = redis::Client::open("redis://localhost:6379").unwrap();
8 let client2 = client.clone();
9
10 let t1 = s.spawn(move || {
11 let mut string = String::with_value("Hello".to_string(), "test", client);
12 println!("Thread1: {}", string.cached().unwrap());
13 assert_eq!(string, "Hello");
14 sleep(std::time::Duration::from_secs(1));
15 string.store("World".to_string());
16 println!("Thread1: {}", string.cached().unwrap());
17 assert_eq!(string, "World");
18 });
19
20 let t2 = s.spawn(move || {
21 sleep(std::time::Duration::from_micros(100));
22 let mut string = String::with_load("test", client2);
23 println!("Thread2: {}", string.cached().unwrap());
24 assert_eq!(string, "Hello");
25 sleep(std::time::Duration::from_secs(2));
26 string.acquire();
27 println!("Thread2: {}", string.cached().unwrap());
28 assert_eq!(string, "World");
29 });
30 t1.join().expect("Failed to join thread1");
31 t2.join().expect("Failed to join thread2");
32 });
33}Sourcepub fn acquire(&mut self) -> &T
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::types::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?
5fn main() {
6 thread::scope(|s| {
7 let client = redis::Client::open("redis://localhost:6379").unwrap();
8 let client2 = client.clone();
9
10 let t1 = s.spawn(move || {
11 let mut string = String::with_value("Hello".to_string(), "test", client);
12 println!("Thread1: {}", string.cached().unwrap());
13 assert_eq!(string, "Hello");
14 sleep(std::time::Duration::from_secs(1));
15 string.store("World".to_string());
16 println!("Thread1: {}", string.cached().unwrap());
17 assert_eq!(string, "World");
18 });
19
20 let t2 = s.spawn(move || {
21 sleep(std::time::Duration::from_micros(100));
22 let mut string = String::with_load("test", client2);
23 println!("Thread2: {}", string.cached().unwrap());
24 assert_eq!(string, "Hello");
25 sleep(std::time::Duration::from_secs(2));
26 string.acquire();
27 println!("Thread2: {}", string.cached().unwrap());
28 assert_eq!(string, "World");
29 });
30 t1.join().expect("Failed to join thread1");
31 t2.join().expect("Failed to join thread2");
32 });
33}Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
The into_inner method returns the inner value of the type. This method consumes the type and drops everything.
§Example
use dtypes::redis::types::Di32 as i32;
let client = redis::Client::open("redis://localhost:6379").unwrap();
let i32 = i32::with_value(3, "test_add", client.clone());
let i32_inner = i32.into_inner();
assert_eq!(i32_inner, 3);Sourcepub fn cached(&self) -> Option<&T>
pub fn cached(&self) -> Option<&T>
The get method returns a reference to the value stored in the type.
Examples found in repository?
5fn main() {
6 thread::scope(|s| {
7 let client = redis::Client::open("redis://localhost:6379").unwrap();
8 let client2 = client.clone();
9
10 let t1 = s.spawn(move || {
11 let mut string = String::with_value("Hello".to_string(), "test", client);
12 println!("Thread1: {}", string.cached().unwrap());
13 assert_eq!(string, "Hello");
14 sleep(std::time::Duration::from_secs(1));
15 string.store("World".to_string());
16 println!("Thread1: {}", string.cached().unwrap());
17 assert_eq!(string, "World");
18 });
19
20 let t2 = s.spawn(move || {
21 sleep(std::time::Duration::from_micros(100));
22 let mut string = String::with_load("test", client2);
23 println!("Thread2: {}", string.cached().unwrap());
24 assert_eq!(string, "Hello");
25 sleep(std::time::Duration::from_secs(2));
26 string.acquire();
27 println!("Thread2: {}", string.cached().unwrap());
28 assert_eq!(string, "World");
29 });
30 t1.join().expect("Failed to join thread1");
31 t2.join().expect("Failed to join thread2");
32 });
33}Trait Implementations§
Source§impl<T> AddAssign<T> for Generic<T>
impl<T> AddAssign<T> for Generic<T>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moreSource§impl<T> AddAssign for Generic<T>
impl<T> AddAssign for Generic<T>
Source§fn add_assign(&mut self, rhs: Generic<T>)
fn add_assign(&mut self, rhs: Generic<T>)
+= operation. Read more