Skip to content

Commit

Permalink
feat(api): refresh feed cache periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 20, 2024
1 parent d831d3e commit 66d4d3b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
14 changes: 14 additions & 0 deletions crates/synd_api/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub struct Args {
pub tls: TlsOptions,
#[command(flatten)]
pub o11y: ObservabilityOptions,
#[command(flatten)]
pub cache: CacheOptions,
}

#[derive(clap::Args, Debug)]
Expand Down Expand Up @@ -88,6 +90,18 @@ pub struct ObservabilityOptions {
pub trace_sampler_ratio: f64,
}

#[derive(clap::Args, Debug)]
#[command(next_help_heading = "Cache options")]
pub struct CacheOptions {
/// Max feed cache size in MiB
#[arg(long, default_value_t = config::cache::DEFAULT_FEED_CACHE_SIZE_MB, env = env_key!("FEED_CACHE_SIZE") )]
pub feed_cache_size_mb: u64,
#[arg(long, default_value_t = config::cache::DEFAULT_FEED_CACHE_TTL_MINUTES, env = env_key!("FEED_CACHE_TTL_MINUTES"))]
pub feed_cache_ttl_minutes: u64,
#[arg(long, default_value_t = config::cache::DEFAULT_FEED_CACHE_REFRESH_INTERVAL_MINUTES, env = env_key!("FEED_CACHE_REFRESH_INTERVAL_MINUTES"))]
pub feed_cache_refresh_interval_minutes: u64,
}

#[must_use]
pub fn parse() -> Args {
Args::parse()
Expand Down
6 changes: 6 additions & 0 deletions crates/synd_api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ pub mod metrics {

pub const MONITOR_INTERVAL: Duration = Duration::from_secs(60);
}

pub mod cache {
pub const DEFAULT_FEED_CACHE_SIZE_MB: u64 = 100;
pub const DEFAULT_FEED_CACHE_TTL_MINUTES: u64 = 180;
pub const DEFAULT_FEED_CACHE_REFRESH_INTERVAL_MINUTES: u64 = 120;
}
63 changes: 41 additions & 22 deletions crates/synd_api/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use synd_feed::feed::{
};

use crate::{
args::{self, KvsdOptions, TlsOptions},
args::{self, CacheOptions, KvsdOptions, TlsOptions},
config,
monitor::Monitors,
repository::kvsd::KvsdClient,
Expand All @@ -29,29 +29,48 @@ impl Dependency {
kvsd: KvsdOptions,
tls: TlsOptions,
serve_options: args::ServeOptions,
cache: CacheOptions,
) -> anyhow::Result<Self> {
let KvsdOptions {
kvsd_host,
kvsd_port,
kvsd_username,
kvsd_password,
} = kvsd;
let kvsd = KvsdClient::connect(
kvsd_host,
kvsd_port,
kvsd_username,
kvsd_password,
Duration::from_secs(10),
)
.await?;
let kvsd = {
let KvsdOptions {
kvsd_host,
kvsd_port,
kvsd_username,
kvsd_password,
} = kvsd;
KvsdClient::connect(
kvsd_host,
kvsd_port,
kvsd_username,
kvsd_password,
Duration::from_secs(10),
)
.await?
};

let cache_feed_service = {
let CacheOptions {
feed_cache_size_mb,
feed_cache_ttl_minutes,
feed_cache_refresh_interval_minutes,
} = cache;
let feed_service = FeedService::new(config::USER_AGENT, 10 * 1024 * 1024);
let cache_feed_service = CacheLayer::with(
feed_service,
CacheConfig::default()
.with_max_cache_size(feed_cache_size_mb * 1024 * 1024)
.with_time_to_live(Duration::from_secs(feed_cache_ttl_minutes * 60)),
);
let periodic_refresher = cache_feed_service
.periodic_refresher()
.with_emit_metrics(true);

let feed_service = FeedService::new(config::USER_AGENT, 10 * 1024 * 1024);
let cache_feed_service = CacheLayer::with(
feed_service,
CacheConfig::default()
.with_max_cache_size(100 * 1024 * 1024)
.with_time_to_live(Duration::from_secs(60 * 60 * 3)),
);
tokio::spawn(periodic_refresher.run(Duration::from_secs(
feed_cache_refresh_interval_minutes * 60,
)));

cache_feed_service
};

let make_usecase = MakeUsecase {
subscription_repo: Arc::new(kvsd),
Expand Down
3 changes: 2 additions & 1 deletion crates/synd_api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ async fn run(
serve,
tls,
o11y,
cache,
}: Args,
shutdown: Shutdown,
) -> anyhow::Result<()> {
let dep = Dependency::new(kvsd, tls, serve).await?;
let dep = Dependency::new(kvsd, tls, serve, cache).await?;

info!(
version = config::VERSION,
Expand Down

0 comments on commit 66d4d3b

Please sign in to comment.