1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! This library provides a powerful in-memory caching solution with support for customizable eviction policies
//! and persistence using Append-Only Files (AOF). It allows efficient management of key-value pairs,
//! ensuring both performance optimization and data persistence across application lifecycles.
//!
//! ## Features
//!
//! - **Multiple Eviction Policies**: Choose from FIFO (First-In, First-Out), LRU (Least Recently Used), and LFU
//! (Least Frequently Used) eviction policies to suit different data access patterns.
//!
//! - **Customizable Eviction Strategies**: Implement custom eviction policies by defining types that adhere to the
//! `EvictionPolicy` trait, allowing tailored cache management.
//!
//! - **Asynchronous Support**: `AsyncCache` struct provides `async` methods for operations like `get`, `put`, and
//! `remove`, ensuring efficient handling of concurrent requests in async-await contexts.
//!
//! - **Persistence with Append-Only Files (AOF)**: Optionally persist cache state across restarts using AOF, ensuring
//! data integrity and recovery after crashes.
//!
//! - **Thread Safety**: `AsyncCache` utilizes `tokio::sync::Mutex` to manage concurrent access safely, making it
//! suitable for multi-threaded environments.
//!
//! - **Efficient Memory Management**: Optimizes memory usage with smart pointers and references, reducing redundancy
//! and improving overall performance.
//!
//! - **Configuration Flexibility**: Configure cache size limits, eviction policies, and persistence settings through
//! intuitive configuration structs (`CacheSyncConfig` and `AsyncCacheConfig`).
//!
//! - **Detailed Documentation**: Comprehensive API documentation and examples facilitate easy integration and usage
//! within applications.
//!
//! - **Safety and Reliability**: Built with Rust's strong type system and ownership model, ensuring memory safety and
//! preventing common bugs like null pointer dereferencing and data races.
//!
//! ## Examples
//!
//! ### `Cache` - Synchronous Cache:
//!
//! ```rust
//! use sine_cache::{cache::Cache, config::CacheConfig};
//!
//! fn main() {
//! let capacity = 10; // Maximum number of entries in the cache.
//! let mut cache = Cache::new(sine_cache::config::CacheSyncConfig::LFU(CacheConfig{max_size: capacity}));
//!
//! // Inserting key-value pairs into the cache
//! cache.put(1, "One");
//! cache.put(1, "one"); // Overwrites previous value
//! cache.put(2, "Two");
//!
//! // Retrieving a value from the cache
//! let value = cache.get(&1);
//! assert!(value.is_some_and(|x| x == &"one"));
//! }
//! ```
//!
//! ### `AsyncCache` - Asynchronous Cache:
//!
//! - #### Without `AOF`:
//!
//! ```rust
//! use sine_cache::{cache::AsyncCache, config::{AsyncCacheConfig, EvictionAsyncConfig}};
//!
//! #[tokio::main]
//! async fn main() {
//! let capacity = 10; // Maximum number of entries in the cache.
//! let mut cache = AsyncCache::new(AsyncCacheConfig::LFU(EvictionAsyncConfig {max_size: capacity, aof_config: None})).await;
//!
//! // Inserting key-value pairs into the cache
//! cache.put(1, String::from("One")).await;
//! cache.put(1, String::from("one")).await; // Overwrites previous value
//! cache.put(2, String::from("Two")).await;
//!
//! // Retrieving a value from the cache
//! let value = cache.get(&1).await;
//! assert!(value.is_some_and(|x| x == "one"));
//! }
//! ```
//!
//! - #### With `AOF`:
//!
//! ```rust
//! use sine_cache::{cache::AsyncCache, config::{AsyncCacheConfig, EvictionAsyncConfig, EvictionAOFConfig}};
//!
//! #[tokio::main]
//! async fn main() {
//!
//! let capacity = 10; // Maximum number of entries in the cache.
//! let mut cache = AsyncCache::new(AsyncCacheConfig::LFU(EvictionAsyncConfig {
//! max_size: capacity,
//! aof_config: Some(EvictionAOFConfig {
//! folder: String::from("./data"), //folder in which persistent file should be written.
//! cache_name: String::from("async_lof_cache"), //Unique cache name as with same name file will be created.
//! flush_time: Some(5000) //After every 5000 milliseconds data will be flushed to disk.
//! })
//! })).await;
//!
//! // Inserting key-value pairs into the cache
//! cache.put(1, String::from("One")).await;
//! cache.put(1, String::from("one")).await; // Overwrites previous value
//! cache.put(2, String::from("Two")).await;
//!
//! // Retrieving a value from the cache
//! let value = cache.get(&1).await;
//! assert!(value.is_some_and(|x| x == "one"));
//! }
//! ```
//!
//! ### Custom eviction policy
//! ```rust
//! use sine_cache::eviction_policies::common::EvictionPolicy;
//! use sine_cache::{cache::AsyncCache, config::{AsyncCacheConfig, CustomEvictionAsyncConfig, CustomEvictionAOFConfig}};
//!
//! pub struct CustomEviction<K> {
//! _phantom: std::marker::PhantomData<K>,
//! }
//! impl<K: Eq + std::hash::Hash + Clone> CustomEviction<K> {
//! pub fn new() -> Self{
//! Self{
//! _phantom: std::marker::PhantomData
//! }
//! }
//! }
//!
//! impl<K: Eq + std::hash::Hash + Clone> EvictionPolicy<K> for CustomEviction<K> {
//! fn on_get(&mut self, key: &K) {
//! // nothing to do.
//! }
//!
//! fn on_set(&mut self, key: K) {
//! // nothing to do.
//! }
//!
//! fn evict(&mut self) -> Option<K> {
//! // nothing to do
//! None
//! }
//!
//! fn remove(&mut self, key: K) {
//! //nothing to do
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//!
//! let capacity = 10; // Maximum number of entries in the cache.
//! let mut cache = AsyncCache::new(AsyncCacheConfig::Custom(CustomEvictionAsyncConfig {
//! max_size: capacity,
//! aof_config: Some(CustomEvictionAOFConfig {
//! folder: String::from("./data"), //folder in which persistent file should be written.
//! cache_name: String::from("async_lof_custom_cache"), //Unique cache name as with same name file will be created.
//! flush_time: Some(5000), //After every 5000 milliseconds data will be flushed to disk.
//! persist_read_ops: true //whether to store reads also, true generally.
//! }),
//! policy: Box::new(CustomEviction::new())
//! })).await;
//!
//! // Inserting key-value pairs into the cache
//! cache.put(1, String::from("One")).await;
//! cache.put(1, String::from("one")).await; // Overwrites previous value
//! cache.put(2, String::from("Two")).await;
//!
//! // Retrieving a value from the cache
//! let value = cache.get(&1).await;
//! assert!(value.is_some_and(|x| x == "one"));
//! }
//!
//! ```
//!
//! For detailed API documentation and further customization options, refer to the library's documentation.
//! For more examples, go through test modules on github library
//Contains code of append only files
// Core functionalities for creating and managing in-memory caches
//Event manager which do things upon each event in cache.
// Common types and utilities used throughout the library
// Implementations of different eviction policies for cache management
//Contains different configuration structs and enums.