Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.6 KB
Raw Blame
//! Generic cache system for PostHog services
//!
//! This crate provides a generic read-through cache implementation that can be used
//! across all PostHog services. It supports:
//!
//! - Generic key-value caching with Redis
//! - Configurable TTL and cache prefixes
//! - Optional negative caching with Moka
//! - Rich return types indicating cache source for observability
//! - Function-based fallback API (no trait implementations required)
//! - User-defined error types
//!
//! # Example
//!
//! ```rust,ignore
//! use common_cache::{ReadThroughCache, CacheConfig, CacheSource};
//!
//! let cache = ReadThroughCache::new(
//!     redis_reader,
//!     redis_writer,
//!     CacheConfig::with_ttl("my_data:", 300),
//!     None, // negative_cache
//! );
//!
//! let result = cache
//!     .get_or_load(&key, |key| async {
//!         load_from_source(key).await
//!     })
//!     .await?;
//!
//! match result.source {
//!     CacheSource::PositiveCache => println!("Cache hit!"),
//!     CacheSource::LoaderCacheMiss => println!("Cache miss, loaded from source"),
//!     CacheSource::NegativeCache => println!("Known to not exist"),
//!     _ => println!("Other source: {:?}", result.source),
//! }
//!
//! if let Some(value) = result.value {
//!     // Use the value
//! } else {
//!     // Item doesn't exist
//! }
//! ```

pub mod metrics;
pub mod negative_cache;
pub mod read_through;
pub mod types;

pub use metrics::ReadThroughCacheWithMetrics;
pub use negative_cache::NegativeCache;
pub use read_through::ReadThroughCache;
pub use types::{CacheConfig, CacheResult, CacheSource};