Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): allow specifying an explicit expiration when storing #23

Merged
merged 1 commit into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::PathBuf;
use std::time::Instant;

Check warning on line 3 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `std::time::Instant`

Check warning on line 3 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unused import: `std::time::Instant`

Check warning on line 3 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

unused import: `std::time::Instant`

Check warning on line 3 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

unused import: `std::time::Instant`

Check warning on line 3 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `std::time::Instant`

warning: unused import: `std::time::Instant` --> src/lib.rs:3:5 | 3 | use std::time::Instant; | ^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

use chrono::{DateTime, Duration, Utc};
use rusqlite::Connection;
Expand Down Expand Up @@ -90,6 +91,19 @@
}

pub fn store<K, T>(&self, key: K, value: T) -> Result<(), Error>
where
K: Hash,
T: ToSql + FromSql,
{
self.store_with_expiration(key, value, Utc::now() + self.ttl)
}

pub fn store_with_expiration<K, T>(
&self,
key: K,
value: T,
expiration: DateTime<Utc>,
) -> Result<(), Error>
where
K: Hash,
T: ToSql + FromSql,
Expand All @@ -103,7 +117,7 @@
let value = CacheEntry {
key: hash,
value,
expiration: Utc::now() + self.ttl,
expiration,
};

let db = Connection::open(self.path.as_path())?;
Expand Down
Loading