|
| 1 | +//! Redis storage and message publishing. |
| 2 | +//! |
| 3 | +//! To receive Redis messages, use the Redis trigger. |
| 4 | +//! |
| 5 | +//! # Examples |
| 6 | +//! |
| 7 | +//! Get a value from the Redis database. |
| 8 | +//! |
| 9 | +//! ```no_run |
| 10 | +//! use spin_sdk::redis::Connection; |
| 11 | +//! |
| 12 | +//! # fn main() -> anyhow::Result<()> { |
| 13 | +//! let conn = Connection::open("redis://127.0.0.1:6379")?; |
| 14 | +//! let payload = conn.get("archimedes-data")?; |
| 15 | +//! if let Some(data) = payload { |
| 16 | +//! println!("{}", String::from_utf8_lossy(&data)); |
| 17 | +//! } |
| 18 | +//! # Ok(()) |
| 19 | +//! # } |
| 20 | +//! ``` |
| 21 | +//! |
| 22 | +//! See the [`Connection`] type for further examples. |
| 23 | +
|
| 24 | +use std::hash::{Hash, Hasher}; |
| 25 | + |
| 26 | +/// An open connection to a Redis server. |
| 27 | +/// |
| 28 | +/// # Examples |
| 29 | +/// |
| 30 | +/// Get a value from the Redis database. |
| 31 | +/// |
| 32 | +/// ```no_run |
| 33 | +/// use spin_sdk::redis::Connection; |
| 34 | +/// |
| 35 | +/// # fn main() -> anyhow::Result<()> { |
| 36 | +/// let conn = Connection::open("redis://127.0.0.1:6379")?; |
| 37 | +/// let payload = conn.get("archimedes-data")?; |
| 38 | +/// if let Some(data) = payload { |
| 39 | +/// println!("{}", String::from_utf8_lossy(&data)); |
| 40 | +/// } |
| 41 | +/// # Ok(()) |
| 42 | +/// # } |
| 43 | +/// ``` |
| 44 | +/// |
| 45 | +/// Set a value in the Redis database. |
| 46 | +/// |
| 47 | +/// ```no_run |
| 48 | +/// use spin_sdk::redis::Connection; |
| 49 | +/// |
| 50 | +/// # fn main() -> anyhow::Result<()> { |
| 51 | +/// let conn = Connection::open("redis://127.0.0.1:6379")?; |
| 52 | +/// let payload = "Eureka!".to_owned().into_bytes(); |
| 53 | +/// conn.set("archimedes-data", &payload)?; |
| 54 | +/// # Ok(()) |
| 55 | +/// # } |
| 56 | +/// ``` |
| 57 | +/// |
| 58 | +/// Delete a value from the Redis database. |
| 59 | +/// |
| 60 | +/// ```no_run |
| 61 | +/// use spin_sdk::redis::Connection; |
| 62 | +/// |
| 63 | +/// # fn main() -> anyhow::Result<()> { |
| 64 | +/// let conn = Connection::open("redis://127.0.0.1:6379")?; |
| 65 | +/// conn.del(&["archimedes-data".to_owned()])?; |
| 66 | +/// # Ok(()) |
| 67 | +/// # } |
| 68 | +/// ``` |
| 69 | +/// |
| 70 | +/// Publish a message to a Redis channel. |
| 71 | +/// |
| 72 | +/// ```no_run |
| 73 | +/// use spin_sdk::redis::Connection; |
| 74 | +/// |
| 75 | +/// # fn ensure_pet_picture(_: &[u8]) -> anyhow::Result<()> { Ok(()) } |
| 76 | +/// # fn use_redis(request: spin_sdk::http::Request) -> anyhow::Result<()> { |
| 77 | +/// let conn = Connection::open("redis://127.0.0.1:6379")?; |
| 78 | +/// |
| 79 | +/// let payload = request.body().to_vec(); |
| 80 | +/// ensure_pet_picture(&payload)?; |
| 81 | +/// |
| 82 | +/// conn.publish("pet-pictures", &payload)?; |
| 83 | +/// # Ok(()) |
| 84 | +/// # } |
| 85 | +/// ``` |
| 86 | +pub use super::wit::v2::redis::Connection; |
| 87 | + |
| 88 | +pub use super::wit::v2::redis::{Error, Payload, RedisParameter, RedisResult}; |
| 89 | + |
| 90 | +impl PartialEq for RedisResult { |
| 91 | + fn eq(&self, other: &Self) -> bool { |
| 92 | + use RedisResult::*; |
| 93 | + match (self, other) { |
| 94 | + (Nil, Nil) => true, |
| 95 | + (Status(a), Status(b)) => a == b, |
| 96 | + (Int64(a), Int64(b)) => a == b, |
| 97 | + (Binary(a), Binary(b)) => a == b, |
| 98 | + _ => false, |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl Eq for RedisResult {} |
| 104 | + |
| 105 | +impl Hash for RedisResult { |
| 106 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 107 | + use RedisResult::*; |
| 108 | + |
| 109 | + match self { |
| 110 | + Nil => (), |
| 111 | + Status(s) => s.hash(state), |
| 112 | + Int64(v) => v.hash(state), |
| 113 | + Binary(v) => v.hash(state), |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments