
Redis Cache
10/30/24About 1 min
This file is a Redis-based cache implementation that provides complete Redis operation functionality for Rust applications.
File location: [redis.rs](src\cache edis.rs)
Core Structure
RedisCache Structure contains two main components:
pool: Redis connection pool, used for managing database connectionsnamespace: Namespace, supporting logical grouping of key-values
Main Functional Modules
1. Basic Connection Management
- Asynchronous connection pool initialization
- Namespace support, all key operations will automatically add a namespace prefix
- Connection pool cloning and sharing
2. String Operations
set_string/get_string: Basic string storage and retrievalset_string_ex: String setting with expiration timeget_one_use: One-time retrieval (delete after retrieval)
3. Serialized Object Operations
set_value/get_value: Support storage and retrieval of any serializable objectsset_value_ex: Object storage with expiration timeget_oneuse_value: One-time object retrieval
4. Key Management
remove: Delete specified keyscontains_key: Check if a key existsttl: Get the remaining time-to-live for a key
5. Advanced Redis Data Structure Operations
- Sorted Sets (ZSet):
zadd,zrange,zrangebyscore_limit,zrem,zadd_ch - Sets:
sadd - Lists:
lpush,brpop(blocking pop) - Atomic Operations:
set_nx_ex(set if not exists)
6. Cache Management and Queries
get_all: Get all cache itemsget_all_paginated: Paginated query of cache items, supports search filteringrecycling: Cache recycling interface
7. Namespace Management
with_namespace: Create an instance with a new namespaceset_namespace: Dynamically modify the namespacenamespaced_key/namespaced_keys: Key name conversion tools
Technical Features
Asynchronous Support: All operations are asynchronous, using the async/await pattern Type Safety: Ensures type safety through generics and Serde serialization Error Handling: Unified error handling mechanism Connection Pool: Uses bb8 connection pool to improve performance Namespace Isolation: Supports data isolation between multi-tenant or modules
This implementation provides a feature-complete, type-safe, and high-performance Redis cache abstraction layer for applications.