934858bebf
- Add repo_path parameter to cached_response and cached_vec_response functions - Implement structured cache key format with namespace, repo_path, and request proto - Replace global cache with Moka in-memory cache using weight-based eviction - Set 256MB memory cap with 10-minute TTL and 2-minute TTI policy - Add metrics collection for cache operations and evictions - Implement efficient repo-scoped invalidation using key structure - Add detailed documentation comments explaining cache architecture - Remove outdated dependencies and update dependency versions - Add error handling for encoding failures in cache operations - Optimize Vec responses with length-delimited encoding and pre-allocation
47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use crate::pb::{PageInfo, Pagination};
|
|
|
|
/// Simple offset-based pagination over an in-memory slice.
|
|
/// The `page_token` is a decimal string encoding the start offset.
|
|
pub fn paginate<T: Clone>(items: &[T], pagination: Option<&Pagination>) -> (Vec<T>, PageInfo) {
|
|
let page_size = pagination
|
|
.map(|p| p.page_size as usize)
|
|
.unwrap_or(items.len().max(1))
|
|
.max(1);
|
|
|
|
let start_offset = pagination
|
|
.and_then(|p| {
|
|
if p.page_token.is_empty() {
|
|
None
|
|
} else {
|
|
p.page_token.parse::<usize>().ok()
|
|
}
|
|
})
|
|
.unwrap_or(0)
|
|
.min(items.len());
|
|
|
|
let end = start_offset.saturating_add(page_size).min(items.len());
|
|
let has_next = end < items.len();
|
|
let next_page_token = if has_next {
|
|
end.to_string()
|
|
} else {
|
|
String::new()
|
|
};
|
|
|
|
(
|
|
items[start_offset..end].to_vec(),
|
|
PageInfo {
|
|
next_page_token,
|
|
has_next_page: has_next,
|
|
total_count: items.len() as u64,
|
|
},
|
|
)
|
|
}
|
|
|
|
/// Apply sort direction. Ascending = no-op (preserves order).
|
|
/// Descending reverses the slice.
|
|
pub fn apply_sort<T>(items: &mut [T], sort_direction: i32) {
|
|
if sort_direction == crate::pb::SortDirection::Desc as i32 {
|
|
items.reverse();
|
|
}
|
|
}
|