Compare commits
1 Commits
1fbafc25df
...
dae5fce12c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dae5fce12c |
@@ -63,3 +63,12 @@ pub async fn purge_expired(db: &DatabaseConnection) -> DbResult<u64> {
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(result.rows_affected)
|
Ok(result.rows_affected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete all cache entries whose key starts with the given prefix.
|
||||||
|
pub async fn purge_prefix(db: &DatabaseConnection, prefix: &str) -> DbResult<u64> {
|
||||||
|
let result = SearchCache::delete_many()
|
||||||
|
.filter(search_cache::Column::QueryKey.starts_with(prefix))
|
||||||
|
.exec(db)
|
||||||
|
.await?;
|
||||||
|
Ok(result.rows_affected)
|
||||||
|
}
|
||||||
|
|||||||
@@ -274,4 +274,22 @@ async fn test_search_cache_ttl() {
|
|||||||
// Purge expired
|
// Purge expired
|
||||||
let purged = queries::cache::purge_expired(conn).await.unwrap();
|
let purged = queries::cache::purge_expired(conn).await.unwrap();
|
||||||
assert_eq!(purged, 1);
|
assert_eq!(purged, 1);
|
||||||
|
|
||||||
|
// Purge by prefix
|
||||||
|
queries::cache::set(conn, "artist_totals:1", "computed", "[10,5,3]", 3600)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
queries::cache::set(conn, "artist_totals:2", "computed", "[20,10,5]", 3600)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
queries::cache::set(conn, "other_key", "computed", "{}", 3600)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let purged = queries::cache::purge_prefix(conn, "artist_totals:").await.unwrap();
|
||||||
|
assert_eq!(purged, 2);
|
||||||
|
|
||||||
|
// other_key should still exist
|
||||||
|
let result = queries::cache::get(conn, "other_key").await.unwrap();
|
||||||
|
assert!(result.is_some());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user