clear cache at migration

This commit is contained in:
Namekuji 2023-07-16 13:37:46 -04:00
parent f1f84f9a9a
commit aca0c47012
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
2 changed files with 31 additions and 0 deletions

View file

@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
mod m20230531_180824_drop_reversi; mod m20230531_180824_drop_reversi;
mod m20230627_185451_index_note_url; mod m20230627_185451_index_note_url;
mod m20230709_000510_move_antenna_to_cache; mod m20230709_000510_move_antenna_to_cache;
mod m20230716_164152_clear_user_cache;
pub struct Migrator; pub struct Migrator;
@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
Box::new(m20230531_180824_drop_reversi::Migration), Box::new(m20230531_180824_drop_reversi::Migration),
Box::new(m20230627_185451_index_note_url::Migration), Box::new(m20230627_185451_index_note_url::Migration),
Box::new(m20230709_000510_move_antenna_to_cache::Migration), Box::new(m20230709_000510_move_antenna_to_cache::Migration),
Box::new(m20230716_164152_clear_user_cache::Migration),
] ]
} }
} }

View file

@ -0,0 +1,29 @@
use redis::Commands;
use sea_orm_migration::prelude::*;
use std::env;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
let cache_url = env::var("CACHE_URL").unwrap();
let prefix = env::var("CACHE_PREFIX").unwrap();
let client = redis::Client::open(cache_url).unwrap();
let mut redis_conn = client.get_connection().unwrap();
let keys: Vec<String> = redis_conn
.keys(format!("{prefix}:cache:userById:*")).unwrap();
if !keys.is_empty() {
println!("Deleting {} keys from the cache server.", keys.len());
redis_conn.del::<_, i32>(keys).unwrap();
}
Ok(())
}
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}