Skip to content

Commit

Permalink
feat(bin): no cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard committed Aug 30, 2023
1 parent 880300d commit 4eb7bae
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
18 changes: 16 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub struct Args {
/// Set a custom request timeout in seconds
#[arg(short, long)]
pub timeout: Option<u64>,

/// Do not cache feeds in local file-backed database
#[arg(short, long)]
pub no_cache: bool,
}

/// Application result type.
Expand Down Expand Up @@ -68,7 +72,11 @@ impl App {
let (tx, rx) = mpsc::unbounded_channel::<StorageEvent>();
let mut repo = Repository::init(&config, tx).await?;

let items = repo.get_all_from_db(&config)?;
let items = if config.should_cache() {
repo.get_all_from_db(&config)?
} else {
Vec::new()
};
repo.refresh_all(&config);

Ok(Self {
Expand Down Expand Up @@ -110,11 +118,17 @@ impl App {
self.load_state = LoadState::Loading(counts);
}
Some(StorageEvent::RetrievedAll(feeds)) => {
let _ = self.repo.store_all(&feeds);
if self.config.should_cache() {
self.repo.store_all(&feeds);
}
self.set_feeds(feeds);
self.load_state = LoadState::Done;
}
Some(StorageEvent::RetrievedOne(feed)) => {
if self.config.should_cache() {
self.repo.store_one(&feed);
}

match self
.feeds
.items
Expand Down
42 changes: 39 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ pub struct Config {
dir_path: PathBuf,
feed_urls: Vec<String>,
sort_order: SortOrder,
cache_control: CacheControl,
refresh_interval: u64,
refresh_timeout: u64,
theme: theme::Theme,
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Default, Clone)]
pub enum SortOrder {
#[default]
Az,
Expand All @@ -36,6 +37,23 @@ pub enum SortOrder {
Custom,
}

#[derive(Debug, Default, Clone, PartialEq)]
pub enum CacheControl {
#[default]
Always,
Never,
}

impl From<bool> for CacheControl {
fn from(value: bool) -> Self {
if value {
Self::Always
} else {
Self::Never
}
}
}

#[derive(Debug)]
pub struct SortOrderError;

Expand Down Expand Up @@ -111,6 +129,10 @@ impl Config {
&self.sort_order
}

pub fn should_cache(&self) -> bool {
self.cache_control == CacheControl::Always
}

pub fn refresh_interval(&self) -> u64 {
self.refresh_interval
}
Expand Down Expand Up @@ -187,7 +209,7 @@ impl Config {

let refresh_interval = args
.interval
.or_else(|| {
.or({
preferences.and_then(|prefs| {
prefs.get("refresh_interval").and_then(|i| match i {
Value::Integer(i) => Some(*i as u64),
Expand All @@ -199,7 +221,7 @@ impl Config {

let refresh_timeout = args
.timeout
.or_else(|| {
.or({
preferences.and_then(|prefs| {
prefs.get("refresh_timeout").and_then(|i| match i {
Value::Integer(i) => Some(*i as u64),
Expand All @@ -209,11 +231,25 @@ impl Config {
})
.unwrap_or(DEFAULT_REFRESH_INTERVAL);

let cache_control = if args.no_cache {
CacheControl::Never
} else {
preferences
.and_then(|prefs| {
prefs.get("cache_feeds").and_then(|i| match i {
Value::Boolean(b) => Some(CacheControl::from(*b)),
_ => None,
})
})
.unwrap_or(CacheControl::Always)
};

Ok(Self {
file_path,
dir_path,
feed_urls: feeds,
sort_order,
cache_control,
refresh_interval,
refresh_timeout,
theme,
Expand Down
4 changes: 4 additions & 0 deletions src/config/tabss.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[sources]
# List URLs for Atom or RSS feeds here
feeds = [
"https://bigthink.com/feed/all/",
"https://feeds.feedburner.com/brainpickings/rss",
"https://feeds.simplecast.com/dLRotFGk",
"https://alistapart.com/main/feed/",
Expand Down Expand Up @@ -38,6 +39,9 @@ color_scheme = "default"
# first by unread, then by newest.
sort_feeds = "a-z"

# Whether or not to cache feeds in a local file-backed database
cache_feeds = true

# How often to refresh feeds, in seconds.
# The default is 300, and 0 means refresh will only occur on startup.
refresh_interval = 300
Expand Down
24 changes: 23 additions & 1 deletion src/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ fn sort_feeds(feeds: &mut Vec<Feed>, config: &Config) {

impl Repository {
pub async fn init(config: &Config, app_tx: UnboundedSender<StorageEvent>) -> Result<Self> {
let db = Database::open_file(config.db_path()).expect("could not open db");
let db = if config.should_cache() {
Database::open_file(config.db_path()).expect("could not open db")
} else {
Database::open_memory().expect("could not open db")
};

let (db_tx, db_rx) = mpsc::unbounded_channel::<StorageEvent>();

Expand All @@ -85,6 +89,24 @@ impl Repository {
Ok(feeds)
}

pub fn store_one(&self, feed: &Feed) -> Result<(), polodb_core::Error> {
let collection = self.db.collection::<Feed>("feeds");
let query = doc! { "link": feed.link() };
let update = bson::to_document(feed)?;

match collection.find_one(query.clone()) {
Ok(Some(_)) => {
collection.update_one(query, update)?;
Ok(())
}
Ok(None) => {
collection.insert_one(feed)?;
Ok(())
}
Err(e) => Err(e),
}
}

pub fn store_all(&self, feeds: &Vec<Feed>) -> anyhow::Result<()> {
let collection = self.db.collection::<Feed>("feeds");
for feed in feeds {
Expand Down
13 changes: 8 additions & 5 deletions vhs.tape
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Output meta/vhs.gif
# Set Theme "Elementary"
Set FontSize 14
Set FontSize 16
Set Padding 32
Set Width 1600
Set Height 800

Type "tabss"
Hide
Type " -c ./src/config/tabss.toml"
Show
Sleep 500ms
Sleep 1s
Enter
Sleep 5s
Show
Sleep 2s

Down
Sleep 500ms
Expand All @@ -37,7 +38,7 @@ Sleep 2s

Type "a"
Sleep 500ms
Type "https://shirtloadsofscience.libsyn.com/rss"
Type "https://feeds.feedburner.com/RockPaperShotgun"
Sleep 500ms
Enter
Sleep 2s
Expand All @@ -51,6 +52,8 @@ Sleep 250ms
Down
Sleep 250ms
Down
Sleep 250ms
Down
Sleep 500ms
Right
Sleep 500ms
Expand Down

0 comments on commit 4eb7bae

Please sign in to comment.