pub struct RiCacheManager { /* private fields */ }Expand description
Cache manager that coordinates different cache backends with consistency support
This struct provides a unified interface for cache operations while ensuring cache consistency across multiple instances through event-driven architecture. It wraps any backend implementing the RiCache trait and adds consistency guarantees.
Implementations§
Source§impl RiCacheManager
impl RiCacheManager
Sourcepub fn new(backend: Arc<dyn RiCache + Send + Sync>) -> Self
pub fn new(backend: Arc<dyn RiCache + Send + Sync>) -> Self
Create a new cache manager with the specified backend
Parameters:
backend: The underlying cache backend implementation
Returns:
- A new instance of
RiCacheManager
Sourcepub async fn start_consistency_listener(&mut self) -> JoinHandle<()>
pub async fn start_consistency_listener(&mut self) -> JoinHandle<()>
Start the cache consistency event listener
This method starts a background task that listens for cache consistency events and applies them to the underlying cache backend. This ensures that all cache instances remain consistent across the system.
Returns:
- A
JoinHandlefor the background task
Sourcepub fn subscribe(&self) -> Receiver<RiCacheEvent>
pub fn subscribe(&self) -> Receiver<RiCacheEvent>
Subscribe to cache consistency events
This method allows external components to subscribe to cache consistency events, enabling them to react to cache changes in real-time.
Returns:
- A broadcast receiver for cache events
Sourcepub fn publish_event(&self, event: RiCacheEvent)
pub fn publish_event(&self, event: RiCacheEvent)
Publish a cache consistency event
This method publishes a cache event to all subscribers, ensuring cache consistency across all instances.
Parameters:
event: The cache event to publish
Sourcepub async fn get<T: DeserializeOwned>(&self, key: &str) -> RiResult<Option<T>>
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> RiResult<Option<T>>
Get a value from cache
This method retrieves a value from the cache using the specified key. If the key
exists and the value is valid, it is deserialized and returned. Otherwise, None
is returned.
Parameters:
key: The cache key to retrieve
Returns:
Ok(Some(T))if the key exists and the value is validOk(None)if the key does not existErr(RiError)if an error occurs during retrieval or deserialization
Sourcepub async fn set<T: Serialize>(
&self,
key: &str,
value: &T,
ttl_seconds: Option<u64>,
) -> RiResult<()>
pub async fn set<T: Serialize>( &self, key: &str, value: &T, ttl_seconds: Option<u64>, ) -> RiResult<()>
Set a value in cache with optional TTL
This method stores a value in the cache with the specified key and optional TTL. It also publishes an invalidate event to ensure cache consistency across all instances.
Parameters:
key: The cache key to setvalue: The value to store (must implement Serialize)ttl_seconds: Optional time-to-live in seconds
Returns:
Ok(())if the value was successfully storedErr(RiError)if an error occurs during serialization or storage
Sourcepub async fn delete(&self, key: &str) -> RiResult<bool>
pub async fn delete(&self, key: &str) -> RiResult<bool>
Delete a value from cache
This method deletes a value from the cache using the specified key. It also publishes an invalidate event to ensure cache consistency across all instances.
Parameters:
key: The cache key to delete
Returns:
Ok(true)if the key was found and deletedOk(false)if the key didn’t existErr(RiError)if an error occurs during deletion
Sourcepub async fn exists(&self, key: &str) -> bool
pub async fn exists(&self, key: &str) -> bool
Check if a key exists in cache
This method checks if the specified key exists in the cache.
Parameters:
key: The cache key to check
Returns:
trueif the key exists,falseotherwise
Sourcepub async fn clear(&self) -> RiResult<()>
pub async fn clear(&self) -> RiResult<()>
Clear all cache entries
This method clears all entries from the cache. It also publishes a clear event to ensure cache consistency across all instances.
Returns:
Ok(())if the cache was successfully clearedErr(RiError)if an error occurs during clearing
Sourcepub async fn invalidate_pattern(&self, pattern: &str) -> RiResult<()>
pub async fn invalidate_pattern(&self, pattern: &str) -> RiResult<()>
Invalidate cache entries matching a pattern
This method invalidates all cache entries matching the specified pattern. It publishes an invalidate pattern event to ensure cache consistency across all instances.
Parameters:
pattern: The pattern to match cache keys (supports wildcards depending on backend)
Returns:
Ok(())if the invalidation event was successfully published
Sourcepub async fn stats(&self) -> RiCacheStats
pub async fn stats(&self) -> RiCacheStats
Get cache statistics
This method retrieves statistics about the cache, including hit rate, miss rate, and the number of entries.
Returns:
- A
RiCacheStatsstruct containing the cache statistics
Sourcepub async fn cleanup_expired(&self) -> RiResult<usize>
pub async fn cleanup_expired(&self) -> RiResult<usize>
Cleanup expired cache entries
This method removes all expired entries from the cache.
Returns:
Ok(usize)with the number of expired entries cleaned upErr(RiError)if an error occurs during cleanup
Sourcepub async fn get_or_set<T, F>(
&self,
key: &str,
ttl_seconds: Option<u64>,
factory: F,
) -> RiResult<T>
pub async fn get_or_set<T, F>( &self, key: &str, ttl_seconds: Option<u64>, factory: F, ) -> RiResult<T>
Get a value from cache or set it if it doesn’t exist
This method retrieves a value from the cache using the specified key. If the key exists and the value is valid, it is returned. Otherwise, the provided factory function is called to generate the value, which is then stored in the cache and returned.
Parameters:
key: The cache key to retrieve or setttl_seconds: Optional time-to-live in seconds for the new valuefactory: A function that generates the value if it doesn’t exist in cache
Returns:
Ok(T)with the retrieved or generated valueErr(RiError)if an error occurs during retrieval, generation, or storage
Trait Implementations§
Source§impl<'py> IntoPyObject<'py> for RiCacheManager
impl<'py> IntoPyObject<'py> for RiCacheManager
Source§type Target = RiCacheManager
type Target = RiCacheManager
Source§type Output = Bound<'py, <RiCacheManager as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <RiCacheManager as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for RiCacheManager
impl PyClass for RiCacheManager
Source§impl PyClassImpl for RiCacheManager
impl PyClassImpl for RiCacheManager
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// any backend implementing the RiCache trait and adds consistency guarantees.
const RAW_DOC: &'static CStr = /// any backend implementing the RiCache trait and adds consistency guarantees.
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<RiCacheManager>
type ThreadChecker = SendablePyClass<RiCacheManager>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyClassNewTextSignature for RiCacheManager
impl PyClassNewTextSignature for RiCacheManager
const TEXT_SIGNATURE: &'static str = "()"
Source§impl PyMethods<RiCacheManager> for PyClassImplCollector<RiCacheManager>
impl PyMethods<RiCacheManager> for PyClassImplCollector<RiCacheManager>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for RiCacheManager
impl PyTypeInfo for RiCacheManager
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl DerefToPyAny for RiCacheManager
impl ExtractPyClassWithClone for RiCacheManager
Auto Trait Implementations§
impl Freeze for RiCacheManager
impl !RefUnwindSafe for RiCacheManager
impl Send for RiCacheManager
impl Sync for RiCacheManager
impl Unpin for RiCacheManager
impl UnsafeUnpin for RiCacheManager
impl !UnwindSafe for RiCacheManager
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more