Skip to content
Merged
Show file tree
Hide file tree
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
97 changes: 97 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ path = "src/lib.rs"

[features]
default = ["cache", "tokenizer", "colored-output"]
# Translation cache with sled DB
cache = ["dep:sled", "dep:sha2", "dep:hex"]
# Translation cache with sled DB + postcard binary serialization
cache = ["dep:sled", "dep:sha2", "dep:hex", "dep:postcard"]
# Claude tokenizer for precise token counting
tokenizer = ["dep:claude-tokenizer"]
# Colored terminal output
Expand All @@ -46,6 +46,7 @@ fastrand = "2" # Lightweight RNG for retry jitter
sled = { version = "0.34", optional = true }
sha2 = { version = "0.10", optional = true }
hex = { version = "0.4", optional = true }
postcard = { version = "1", features = ["alloc"], optional = true }

# Optional: Colored output
colored = { version = "2", optional = true }
Expand Down
5 changes: 3 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod cache_impl {
/// Get cached translation if available and not expired
pub fn get(&self, key: &str) -> Option<CacheEntry> {
match self.db.get(key) {
Ok(Some(bytes)) => match serde_json::from_slice::<CacheEntry>(&bytes) {
Ok(Some(bytes)) => match postcard::from_bytes::<CacheEntry>(&bytes) {
Ok(entry) => {
let now = Utc::now().timestamp();
let ttl_secs = self.config.ttl_days as i64 * 24 * 60 * 60;
Expand All @@ -190,6 +190,7 @@ mod cache_impl {
}
}
Err(_) => {
let _ = self.db.remove(key);
CACHE_MISSES.fetch_add(1, Ordering::Relaxed);
None
}
Expand All @@ -203,7 +204,7 @@ mod cache_impl {

/// Store translation in cache
pub fn put(&self, key: &str, entry: &CacheEntry) {
if let Ok(bytes) = serde_json::to_vec(entry) {
if let Ok(bytes) = postcard::to_allocvec(entry) {
let entry_size = bytes.len();
let _ = self.db.insert(key, bytes);

Expand Down