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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*!
[](https://crates.io/crates/kash)

[](https://crates.io/crates/kash)
[](https://docs.rs/kash)

[](https://deps.rs/crate/kash)
Function and method cache and memoization library for Rust, using [`#[kash]`](kash) macro.
```rust
use kash::kash;
/// Defines a function named `fib` that uses a cache implicitly named `FIB`.
/// By default, the cache will be the function's name in all caps.
#[kash]
fn fib(n: u64) -> u64 {
if n == 0 || n == 1 { return n }
fib(n-1) + fib(n-2)
}
```
Or if you want to limit the size and time-to-live:
```rust
use kash::kash;
const TTL: u64 = 1000;
#[kash(size = "100", ttl = "TTL")]
fn fib(n: u64) -> u64 {
if n == 0 || n == 1 { return n }
fib(n-1) + fib(n-2)
}
```
## Features
- `default`: Includes `ahash` feature.
- `ahash`: Enable `ahash` hasher as default hashing algorithm.
- `async`: Include support for async functions.
- `redis_store`: Include Redis cache store.
- `redis_tokio`: Include async Redis support using `tokio` and `tokio` tls support, implies `redis_store` and `async`.
- `redis_connection_manager`: Enable the optional `connection-manager` feature of `redis`. Any async redis caches created
will use a connection manager instead of a `MultiplexedConnection`.
- `redis_ahash`: Enable the optional `ahash` feature of `redis`.
- `disk_store`: Include disk cache store.
----
```rust
use std::thread::sleep;
use std::time::Duration;
use kash::kash;
/// Use an explicit cache-type with a custom creation block and custom cache-key generating block
#[kash(
size = "100",
key(ty = "String", expr = r#"{ format!("{}{}", a, b) }"#)
)]
fn keyed(a: &str, b: &str) -> usize {
let size = a.len() + b.len();
sleep(Duration::new(size as u64, 0));
size
}
# pub fn main() { }
```
----
```rust
use kash::{kash, RedisCacheError};
use kash::AsyncRedisCache;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Clone)]
enum ExampleError {
#[error("error with redis cache `{0}`")]
RedisError(String),
}
impl From<RedisCacheError> for ExampleError {
fn from(e: RedisCacheError) -> Self {
ExampleError::RedisError(format!("{:?}", e))
}
}
/// Cache the results of an async function in redis. Cache
/// keys will be prefixed with `cache_redis_prefix`.
#[kash(redis)]
async fn async_kash_sleep_secs(secs: u64) -> Result<String, ExampleError> {
std::thread::sleep(std::time::Duration::from_secs(secs));
Ok(secs.to_string())
}
```
----
```rust
use kash::{kash, DiskCacheError};
use kash::DiskCache;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Clone)]
enum ExampleError {
#[error("error with disk cache `{0}`")]
DiskError(String),
}
impl From<DiskCacheError> for ExampleError {
fn from(e: DiskCacheError) -> Self {
ExampleError::DiskError(format!("{:?}", e))
}
}
/// Cache the results of a function on disk.
/// Cache files will be stored under the system cache dir
/// unless otherwise specified with `dir` or the `create` argument.
#[kash(disk)]
fn kash_sleep_secs(secs: u64) -> Result<String, ExampleError> {
std::thread::sleep(std::time::Duration::from_secs(secs));
Ok(secs.to_string())
}
```
Functions defined via macros will have their result, cached using the
function's arguments as a key by default.
When a macro-defined function is called, the function's cache is first checked for an already
computed (and still valid) value before evaluating the function body.
See [`examples`](https://github.com/omid/kash/tree/master/examples) directory for more examples.
*/
pub use moka;
pub use once_cell;
use async_trait;
pub use kash;
pub use AsyncRedisCache;
pub use ;
pub use ;
pub use instant;
/// Cache operations on an io-connected store