Skip to main content

RedisClient

Struct RedisClient 

Source
pub struct RedisClient { /* private fields */ }
Expand description

Shared Redis client wrapping a deadpool_redis::Pool.

Provides typed helpers for common operations used across Pyra services: get/set with optional TTL, JSON serialization, MGET, SCAN, sets, hashes, streams, and distributed locks (SET NX).

Implementations§

Source§

impl RedisClient

Source

pub fn new(pool: Pool) -> Self

Source

pub fn pool(&self) -> &Pool

Get a reference to the underlying pool.

Source

pub async fn get(&self, key: &str) -> RedisResult<Option<String>>

Source

pub async fn set( &self, key: &str, value: &str, ttl_seconds: Option<u64>, ) -> RedisResult<()>

Source

pub async fn set_nx( &self, key: &str, value: &str, ttl_seconds: u64, ) -> RedisResult<bool>

SET key value NX EX ttl — returns true if the key was set (lock acquired).

Source

pub async fn delete(&self, key: &str) -> RedisResult<bool>

Source

pub async fn exists(&self, key: &str) -> RedisResult<bool>

Source

pub async fn expire(&self, key: &str, ttl_seconds: i64) -> RedisResult<bool>

Source

pub async fn increment(&self, key: &str) -> RedisResult<i64>

Source

pub async fn increment_by(&self, key: &str, amount: i64) -> RedisResult<i64>

INCRBY — increment key by a specific amount.

Source

pub async fn decrement_by(&self, key: &str, amount: i64) -> RedisResult<i64>

DECRBY — decrement key by a specific amount.

Source

pub async fn ttl(&self, key: &str) -> RedisResult<i64>

TTL — get remaining time-to-live in seconds. Returns -1 if no expiry, -2 if key doesn’t exist.

Source

pub async fn set_json<T: Serialize>( &self, key: &str, value: &T, ttl_seconds: Option<u64>, ) -> RedisResult<()>

Source

pub async fn get_json<T: DeserializeOwned>( &self, key: &str, ) -> RedisResult<Option<T>>

Source

pub async fn set_multiple(&self, pairs: &[(String, String)]) -> RedisResult<()>

MSET — set multiple key-value pairs in a single round-trip.

Source

pub async fn mget(&self, keys: &[String]) -> RedisResult<Vec<Option<String>>>

MGET — fetch multiple keys in a single round-trip.

Source

pub async fn scan_keys(&self, pattern: &str) -> RedisResult<Vec<String>>

SCAN with a glob pattern. Returns deduplicated keys.

Source

pub async fn set_add(&self, key: &str, members: &[String]) -> RedisResult<usize>

Source

pub async fn set_members(&self, key: &str) -> RedisResult<Vec<String>>

Source

pub async fn set_is_member(&self, key: &str, member: &str) -> RedisResult<bool>

Source

pub async fn hash_set( &self, key: &str, field: &str, value: &str, ) -> RedisResult<()>

Source

pub async fn hash_get( &self, key: &str, field: &str, ) -> RedisResult<Option<String>>

Source

pub async fn hash_get_all( &self, key: &str, ) -> RedisResult<HashMap<String, String>>

Source

pub async fn xadd( &self, key: &str, max_len: usize, fields: &[(&str, &str)], ) -> RedisResult<String>

XADD with approximate MAXLEN trimming.

Source

pub async fn list_push(&self, key: &str, values: &[String]) -> RedisResult<i64>

RPUSH — append one or more values to a list.

Source

pub async fn list_pop(&self, key: &str) -> RedisResult<Option<String>>

Source

pub async fn list_length(&self, key: &str) -> RedisResult<i64>

Source

pub async fn eval<T: FromRedisValue>( &self, script: &str, keys: &[&str], args: &[&str], ) -> RedisResult<T>

Execute a Lua script via EVAL.

Source

pub async fn ping(&self) -> RedisResult<bool>

Source

pub async fn health_check(&self) -> bool

Health check — attempts a GET and returns false on error.

Source

pub fn pool_size(&self) -> usize

Total number of connections in the pool.

Source

pub fn available_connections(&self) -> usize

Number of idle connections available for use.

Source

pub fn waiting_connections(&self) -> usize

Number of tasks waiting for a connection.

Source§

impl RedisClient

Source

pub async fn fetch_vault_position_data( &self, vault_address: &str, asset_ids: &[AssetId], ) -> RedisResult<VaultPositionData>

Fetch DriftUser, SpotMarkets, and prices for a vault via a single MGET.

Builds keys as: [drift_user, spot_market_0..N, price_0..N] and parses the results into typed structures.

Returns NotFound if the DriftUser key is missing.

Source

pub async fn fetch_all_drift_positions( &self, asset_ids: &[AssetId], include_vault_owners: bool, ) -> RedisResult<AllDriftPositionsData>

Fetch ALL drift user positions from Redis via SCAN + MGET.

Scans every account:drift:user:* key and fetches all position data in a single MGET round-trip.

When include_vault_owners is true, vault accounts are also fetched to map vault_address → owner solana address.

Source§

impl RedisClient

Source

pub async fn fetch_drift_user( &self, authority: &Pubkey, ) -> RedisResult<Option<DriftUser>>

Fetch a Drift user account by the vault (authority) address.

Source

pub async fn fetch_spot_market( &self, asset_id: AssetId, ) -> RedisResult<Option<SpotMarket>>

Fetch a single Drift spot market by asset ID.

Source

pub async fn fetch_spot_markets( &self, asset_ids: &[AssetId], ) -> RedisResult<HashMap<AssetId, SpotMarket>>

Fetch multiple spot markets by their asset IDs in a single MGET round-trip.

Returns only the markets that were found and successfully deserialized.

Source

pub async fn fetch_all_spot_markets( &self, ) -> RedisResult<HashMap<AssetId, SpotMarket>>

Fetch all spot markets by scanning account:drift:spot_market:* keys.

Uses SCAN + MGET for efficiency. Returns asset_id -> SpotMarket. Only includes tokens known to pyra-tokens.

Source§

impl RedisClient

Source

pub async fn fetch_vault_kamino_position_data( &self, vault_address: &Pubkey, lending_market: &Pubkey, reserve_pubkeys: &[Pubkey], ) -> RedisResult<VaultKaminoPositionData>

Fetch KaminoObligation, reserves, and prices for a vault via MGET.

Builds keys as: [obligation, reserve_0..N] and extracts prices from reserve.liquidity.market_price_sf.

Returns NotFound if the obligation key is missing.

Source

pub async fn fetch_all_kamino_positions( &self, reserve_pubkeys: &[Pubkey], ) -> RedisResult<AllKaminoPositionsData>

Fetch ALL Kamino obligations from Redis via SCAN + MGET.

Scans every account:kamino:obligation:* key and fetches all obligation data in a single MGET round-trip.

Source§

impl RedisClient

Source

pub async fn fetch_kamino_obligation( &self, vault_address: &Pubkey, lending_market: &Pubkey, ) -> RedisResult<Option<KaminoObligation>>

Fetch a Kamino obligation by the vault (authority) address and lending market.

Source

pub async fn fetch_kamino_obligation_by_pubkey( &self, obligation_pubkey: &Pubkey, ) -> RedisResult<Pubkey>

Reverse lookup: Kamino Obligation account pubkey → vault authority (owner).

Source

pub async fn fetch_kamino_reserve( &self, reserve_pubkey: &Pubkey, ) -> RedisResult<Option<KaminoReserve>>

Fetch a single Kamino reserve by its pubkey.

Source

pub async fn fetch_kamino_reserves( &self, pubkeys: &[Pubkey], ) -> RedisResult<HashMap<Pubkey, KaminoReserve>>

Fetch multiple Kamino reserves by pubkey in a single MGET round-trip.

Returns only the reserves that were found and successfully deserialized.

Source

pub async fn fetch_all_kamino_reserves( &self, ) -> RedisResult<HashMap<Pubkey, KaminoReserve>>

Fetch all Kamino reserves by scanning account:kamino:reserve:* keys.

Uses SCAN + MGET for efficiency. Returns reserve_pubkey -> KaminoReserve.

Source§

impl RedisClient

Source

pub async fn fetch_vault(&self, vault: &Pubkey) -> RedisResult<Option<Vault>>

Fetch a Pyra vault account by its PDA address.

Source

pub async fn fetch_withdraw_order( &self, order: &Pubkey, ) -> RedisResult<Option<WithdrawOrderAccount>>

Fetch a withdraw order account by its PDA address.

Source

pub async fn fetch_spend_limits_order( &self, order: &Pubkey, ) -> RedisResult<Option<SpendLimitsOrderAccount>>

Fetch a spend limits order account by its PDA address.

Source

pub async fn fetch_price(&self, asset_id: AssetId) -> RedisResult<Option<f64>>

Fetch the oracle price for an asset (stored as a plain f64 by the indexer).

Source

pub async fn fetch_prices( &self, asset_ids: &[AssetId], ) -> RedisResult<HashMap<AssetId, f64>>

Fetch prices for multiple assets in a single MGET round-trip.

Returns only assets that have a cached price.

Trait Implementations§

Source§

impl Clone for RedisClient

Source§

fn clone(&self) -> RedisClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.