
Memory Cache
This file is a memory-based cache implementation that provides complete memory caching functionality for Rust applications, serving as an alternative to Redis cache.
File location: memory.rs
Core Data Structures
MemoryItem Structure
The basic unit of memory cache items, including:
value: Stored string valueexpire_time: Optional expiration timestampcreated_at: Creation timestamp
Supports expiration checking and TTL calculation functionality.
MemoryCache Structure
The main cache manager, containing various data structures:
storage: String key-value storage (implemented using DashMap for concurrent safety)namespace: Namespace supportlists: List data structure storagesets: Set data structure storagesorted_sets: Sorted set data structure storage
Main Functional Modules
1. Lifecycle Management
- Automatic Cleanup Task: Background task that cleans expired keys every minute
- Expiration Check: Automatically checks and removes expired items when accessed
- Manual Recycling: Provides a
recyclingmethod to actively clean expired data
2. Basic String Operations
set_string/get_string: Basic string storage and retrieval operationsset_string_ex: String setting with expiration timeget_one_use: One-time retrieval (delete after retrieval)
3. Serialized Object Operations
set_value/get_value: Supports JSON storage and retrieval for any serializable objectset_value_ex: Object storage with expiration timeget_oneuse_value: One-time object retrieval
4. Key Management Functions
remove: Delete specified key (supports multiple data types)contains_key: Check if a key exists (across all data types)ttl: Get the remaining time-to-live for a key
5. Advanced Data Structure Operations
- Sorted Set Operations:
zadd,zrange,zrangebyscore_limit,zrem,zadd_ch - Set Operations:
sadd(add set members) - List Operations:
lpush(push to left),brpop(pop from right, simplified implementation) - Atomic Operations:
set_nx_ex(set if key does not exist)
6. Cache Query and Management
get_all: Get all cache itemsget_all_paginated: Paginated query of cache items, supports search filtering across multiple data types
7. Namespace Management
with_namespace: Create a cache instance with a new namespaceset_namespace: Dynamically modify the current namespacenamespaced_key/namespaced_keys: Key name namespace conversion tools
Technical Features
Concurrency Safety: Uses DashMap to provide lock-free concurrent access Automatic Expiration: Timestamp-based expiration mechanism, supports automatic background cleanup Multiple Data Types: Simulates various Redis data structures (strings, lists, sets, sorted sets) Memory Efficient: Pure memory implementation, no external dependencies Namespace Isolation: Supports data isolation in multi-tenant scenarios Type Safety: Ensures serialization safety through generics and Serde
Comparison with Redis Implementation
This memory cache implementation provides an API interface basically identical to Redis cache and can serve as a memory alternative to Redis. The main differences are:
- Data is only stored in process memory
- Does not support persistence
- Some complex operations (such as blocking brpop) are simplified
- Higher performance, but data lacks persistence
This implementation provides a feature-complete, high-performance memory cache solution for applications.