larastvel-core 0.2.2

Larastvel framework core - inspired by Laravel
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use async_trait::async_trait;
use std::sync::Arc;
use std::time::Duration;

use super::{prefixed_key, CacheError, CacheStore, LockStore};

/// Atomically release a lock only when the caller still owns it.
const RELEASE_LUA: &str = r#"
if redis.call('get', KEYS[1]) == ARGV[1] then
    return redis.call('del', KEYS[1])
end
return 0
"#;

/// Atomically extend a lock's TTL only when the caller still owns it.
const REFRESH_LUA: &str = r#"
if redis.call('get', KEYS[1]) == ARGV[1] then
    return redis.call('pexpire', KEYS[1], ARGV[2])
end
return 0
"#;

/// Redis-backed cache store (Laravel's `redis` cache driver).
///
/// Values are stored as plain strings. TTLs use Redis' native `EX` so the
/// server performs expiration; `touch` uses `EXPIRE` without re-fetching.
#[derive(Debug, Clone)]
pub struct RedisStore {
    name: String,
    client: redis::Client,
    prefix: String,
}

impl RedisStore {
    /// Create a new Redis cache store.
    ///
    /// - `name`: The store name (e.g. "redis").
    /// - `url`: A Redis connection URL (e.g. `redis://127.0.0.1:6379`).
    /// - `prefix`: An optional key prefix.
    pub fn new(name: &str, url: &str, prefix: &str) -> Result<Self, CacheError> {
        let client = redis::Client::open(url)
            .map_err(|e| CacheError::Store(format!("Invalid Redis URL: {}", e)))?;
        Ok(Self {
            name: name.to_string(),
            client,
            prefix: prefix.to_string(),
        })
    }

    fn key(&self, key: &str) -> String {
        prefixed_key(&self.prefix, key)
    }

    async fn connection(&self) -> Result<redis::aio::MultiplexedConnection, CacheError> {
        self.client
            .get_multiplexed_async_connection()
            .await
            .map_err(|e| CacheError::Store(format!("Redis connection error: {}", e)))
    }
}

#[async_trait]
impl CacheStore for RedisStore {
    async fn get(&self, key: &str) -> Result<Option<String>, CacheError> {
        let mut conn = self.connection().await?;
        let value: Option<String> = redis::cmd("GET")
            .arg(self.key(key))
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis GET failed: {}", e)))?;
        Ok(value)
    }

    async fn set(
        &self,
        key: &str,
        value: &str,
        ttl_seconds: Option<u64>,
    ) -> Result<(), CacheError> {
        let mut conn = self.connection().await?;
        let mut cmd = redis::cmd("SET");
        cmd.arg(self.key(key)).arg(value);
        if let Some(ttl) = ttl_seconds {
            cmd.arg("EX").arg(ttl);
        }
        cmd.query_async::<()>(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis SET failed: {}", e)))?;
        Ok(())
    }

    async fn delete(&self, key: &str) -> Result<bool, CacheError> {
        let mut conn = self.connection().await?;
        let removed: i64 = redis::cmd("DEL")
            .arg(self.key(key))
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis DEL failed: {}", e)))?;
        Ok(removed > 0)
    }

    async fn clear(&self) -> Result<(), CacheError> {
        let mut conn = self.connection().await?;
        // Only remove keys under this store's prefix (SCAN + DEL, chunked).
        let pattern = format!("{}*", self.prefix);
        let mut cursor = 0u64;
        loop {
            let (next, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                .arg(cursor)
                .arg("MATCH")
                .arg(&pattern)
                .arg("COUNT")
                .arg(100)
                .query_async(&mut conn)
                .await
                .map_err(|e| CacheError::Store(format!("Redis SCAN failed: {}", e)))?;
            if !keys.is_empty() {
                let mut del = redis::cmd("DEL");
                for k in &keys {
                    del.arg(k);
                }
                let _: i64 = del
                    .query_async(&mut conn)
                    .await
                    .map_err(|e| CacheError::Store(format!("Redis DEL failed: {}", e)))?;
            }
            if next == 0 {
                break;
            }
            cursor = next;
        }
        Ok(())
    }

    async fn increment(&self, key: &str, by: i64) -> Result<i64, CacheError> {
        let mut conn = self.connection().await?;
        let value: i64 = redis::cmd("INCRBY")
            .arg(self.key(key))
            .arg(by)
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis INCRBY failed: {}", e)))?;
        Ok(value)
    }

    async fn touch(&self, key: &str, ttl_seconds: u64) -> Result<bool, CacheError> {
        let mut conn = self.connection().await?;
        let touched: i64 = redis::cmd("EXPIRE")
            .arg(self.key(key))
            .arg(ttl_seconds)
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis EXPIRE failed: {}", e)))?;
        Ok(touched > 0)
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn as_lock_store(&self) -> Option<Arc<dyn LockStore>> {
        Some(Arc::new(self.clone()))
    }
}

#[async_trait]
impl LockStore for RedisStore {
    async fn acquire(&self, key: &str, owner: &str, ttl: Duration) -> Result<bool, CacheError> {
        let mut conn = self.connection().await?;
        let acquired: bool = redis::cmd("SET")
            .arg(self.key(key))
            .arg(owner)
            .arg("NX")
            .arg("PX")
            .arg(ttl.as_millis() as u64)
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis SET NX failed: {}", e)))?;
        Ok(acquired)
    }

    async fn release(&self, key: &str, owner: &str) -> Result<bool, CacheError> {
        let mut conn = self.connection().await?;
        let released: i64 = redis::cmd("EVAL")
            .arg(RELEASE_LUA)
            .arg(1)
            .arg(self.key(key))
            .arg(owner)
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis lock release failed: {}", e)))?;
        Ok(released > 0)
    }

    async fn force_release(&self, key: &str) -> Result<bool, CacheError> {
        let mut conn = self.connection().await?;
        let removed: i64 = redis::cmd("DEL")
            .arg(self.key(key))
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis DEL failed: {}", e)))?;
        Ok(removed > 0)
    }

    async fn refresh(&self, key: &str, owner: &str, ttl: Duration) -> Result<bool, CacheError> {
        let mut conn = self.connection().await?;
        let refreshed: i64 = redis::cmd("EVAL")
            .arg(REFRESH_LUA)
            .arg(1)
            .arg(self.key(key))
            .arg(owner)
            .arg(ttl.as_millis() as u64)
            .query_async(&mut conn)
            .await
            .map_err(|e| CacheError::Store(format!("Redis lock refresh failed: {}", e)))?;
        Ok(refreshed > 0)
    }
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use once_cell::sync::Lazy;

    static REDIS_URL: Lazy<String> = Lazy::new(|| {
        std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string())
    });

    fn redis_url() -> &'static str {
        &REDIS_URL
    }

    /// Connect if a Redis server is reachable; otherwise skip the test body.
    /// The connect is wrapped in a short timeout so a missing server fails
    /// fast instead of blocking the test suite.
    async fn maybe_store() -> Option<RedisStore> {
        let store = match RedisStore::new("redis", redis_url(), "larastvel_test:") {
            Ok(s) => s,
            Err(_) => return None,
        };
        let connected =
            tokio::time::timeout(std::time::Duration::from_millis(300), store.connection()).await;
        match connected {
            Ok(Ok(_)) => Some(store),
            _ => {
                eprintln!("skipping Redis tests: no server at {}", redis_url());
                None
            }
        }
    }

    #[tokio::test]
    async fn test_redis_store_get_set_delete() {
        let Some(store) = maybe_store().await else {
            return;
        };
        assert!(store.get("key").await.unwrap().is_none());
        store.set("key", "value", Some(60)).await.unwrap();
        assert_eq!(store.get("key").await.unwrap(), Some("value".to_string()));
        assert!(store.delete("key").await.unwrap());
        assert!(!store.delete("key").await.unwrap());
    }

    #[tokio::test]
    async fn test_redis_store_overwrite() {
        let Some(store) = maybe_store().await else {
            return;
        };
        store.set("key", "old", Some(60)).await.unwrap();
        store.set("key", "new", Some(60)).await.unwrap();
        assert_eq!(store.get("key").await.unwrap(), Some("new".to_string()));
        let _ = store.delete("key").await;
    }

    #[tokio::test]
    async fn test_redis_store_ttl_expiry() {
        let Some(store) = maybe_store().await else {
            return;
        };
        store.set("temp", "gone", Some(1)).await.unwrap();
        assert!(store.has("temp").await.unwrap());
        tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
        assert!(!store.has("temp").await.unwrap());
    }

    #[tokio::test]
    async fn test_redis_store_forever_has_no_ttl() {
        let Some(store) = maybe_store().await else {
            return;
        };
        store.forever("perm", "always").await.unwrap();
        assert_eq!(store.get("perm").await.unwrap(), Some("always".to_string()));
        let _ = store.delete("perm").await;
    }

    #[tokio::test]
    async fn test_redis_store_clear_only_prefixed() {
        let Some(store) = maybe_store().await else {
            return;
        };
        // An unrelated key (different prefix) must survive clear().
        let other = RedisStore::new("redis", redis_url(), "other:").unwrap();
        other.set("keep", "safe", Some(60)).await.unwrap();
        store.set("a", "1", Some(60)).await.unwrap();
        store.set("b", "2", Some(60)).await.unwrap();
        store.clear().await.unwrap();
        assert!(!store.has("a").await.unwrap());
        assert!(!store.has("b").await.unwrap());
        assert!(other.has("keep").await.unwrap());
        let _ = other.delete("keep").await;
    }

    #[tokio::test]
    async fn test_redis_store_increment_decrement() {
        let Some(store) = maybe_store().await else {
            return;
        };
        assert_eq!(store.increment("counter", 1).await.unwrap(), 1);
        assert_eq!(store.increment("counter", 5).await.unwrap(), 6);
        assert_eq!(store.decrement("counter", 2).await.unwrap(), 4);
        let _ = store.delete("counter").await;
    }

    #[tokio::test]
    async fn test_redis_store_pull_and_many() {
        let Some(store) = maybe_store().await else {
            return;
        };
        store.set("p", "val", Some(60)).await.unwrap();
        assert_eq!(store.pull("p").await.unwrap(), Some("val".to_string()));
        assert!(!store.has("p").await.unwrap());

        store.set("a", "1", Some(60)).await.unwrap();
        store.set("b", "2", Some(60)).await.unwrap();
        let results = store.many(&["a", "b", "c"]).await.unwrap();
        assert_eq!(results.get("a").unwrap(), &Some("1".to_string()));
        assert_eq!(results.get("c").unwrap(), &None);
        let _ = store.delete("a").await;
        let _ = store.delete("b").await;
    }

    #[tokio::test]
    async fn test_redis_store_remember() {
        let Some(store) = maybe_store().await else {
            return;
        };
        let result = store
            .remember(
                "computed",
                60,
                Box::new(|| Box::pin(async { "expensive".to_string() })),
            )
            .await
            .unwrap();
        assert_eq!(result, "expensive");
        assert_eq!(
            store.get("computed").await.unwrap(),
            Some("expensive".to_string())
        );
        let _ = store.delete("computed").await;
    }

    #[tokio::test]
    async fn test_redis_store_touch() {
        let Some(store) = maybe_store().await else {
            return;
        };
        assert!(!store.touch("missing", 60).await.unwrap());
        store.set("t", "v", Some(60)).await.unwrap();
        assert!(store.touch("t", 120).await.unwrap());
        assert_eq!(store.get("t").await.unwrap(), Some("v".to_string()));
        let _ = store.delete("t").await;
    }

    #[tokio::test]
    async fn test_redis_store_name_and_bad_url() {
        let Some(store) = maybe_store().await else {
            return;
        };
        assert_eq!(store.name(), "redis");
        assert!(RedisStore::new("redis", "not-a-url", "").is_err());
    }

    #[tokio::test]
    async fn test_redis_locks() {
        let Some(store) = maybe_store().await else {
            return;
        };
        let lock_store = store.as_lock_store().unwrap();
        let owner_a = "owner-a";
        let owner_b = "owner-b";

        assert!(lock_store
            .acquire("lock1", owner_a, Duration::from_secs(60))
            .await
            .unwrap());
        // Second owner is blocked, and only the holder may release.
        assert!(!lock_store
            .acquire("lock1", owner_b, Duration::from_secs(60))
            .await
            .unwrap());
        assert!(!lock_store.release("lock1", owner_b).await.unwrap());
        assert!(lock_store.release("lock1", owner_a).await.unwrap());
        assert!(lock_store
            .acquire("lock1", owner_b, Duration::from_secs(60))
            .await
            .unwrap());

        // refresh keeps the lock alive for the holder only.
        assert!(lock_store
            .refresh("lock1", owner_b, Duration::from_secs(120))
            .await
            .unwrap());
        assert!(!lock_store
            .refresh("lock1", owner_a, Duration::from_secs(120))
            .await
            .unwrap());

        // force_release ignores the owner.
        assert!(lock_store.force_release("lock1").await.unwrap());
        assert!(!lock_store.force_release("lock1").await.unwrap());

        // Expiry: a 100ms lock must be free after 300ms.
        assert!(lock_store
            .acquire("short", owner_a, Duration::from_millis(100))
            .await
            .unwrap());
        tokio::time::sleep(Duration::from_millis(300)).await;
        assert!(lock_store
            .acquire("short", owner_b, Duration::from_secs(60))
            .await
            .unwrap());
        let _ = lock_store.force_release("short").await;
    }
}