1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-30 11:16:38 -05:00
denoland-deno/cli/cache/http_cache/mod.rs

78 lines
2.1 KiB
Rust
Raw Normal View History

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError;
use deno_core::serde::Deserialize;
use deno_core::serde::Serialize;
use deno_core::url::Url;
use std::path::PathBuf;
use std::time::SystemTime;
use crate::http_util::HeadersMap;
mod common;
mod global;
mod local;
pub use global::url_to_filename;
pub use global::GlobalHttpCache;
pub use local::LocalHttpCache;
pub use local::LocalLspHttpCache;
/// Cached metadata about a url.
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct CachedUrlMetadata {
pub headers: HeadersMap,
pub url: String,
#[serde(default = "SystemTime::now", rename = "now")]
pub time: SystemTime,
}
impl CachedUrlMetadata {
pub fn is_redirect(&self) -> bool {
self.headers.contains_key("location")
}
}
/// Computed cache key, which can help reduce the work of computing the cache key multiple times.
pub struct HttpCacheItemKey<'a> {
// The key is specific to the implementation of HttpCache,
// so keep these private to the module. For example, the
// fact that these may be stored in a file is an implementation
// detail.
#[cfg(debug_assertions)]
pub(super) is_local_key: bool,
pub(super) url: &'a Url,
/// This will be set all the time for the global cache, but it
/// won't ever be set for the local cache because that also needs
/// header information to determine the final path.
pub(super) file_path: Option<PathBuf>,
}
pub trait HttpCache: Send + Sync + std::fmt::Debug {
/// A pre-computed key for looking up items in the cache.
fn cache_item_key<'a>(
&self,
url: &'a Url,
) -> Result<HttpCacheItemKey<'a>, AnyError>;
fn contains(&self, url: &Url) -> bool;
fn set(
&self,
url: &Url,
headers: HeadersMap,
content: &[u8],
) -> Result<(), AnyError>;
fn read_modified_time(
&self,
key: &HttpCacheItemKey,
) -> Result<Option<SystemTime>, AnyError>;
fn read_file_bytes(
&self,
key: &HttpCacheItemKey,
) -> Result<Option<Vec<u8>>, AnyError>;
fn read_metadata(
&self,
key: &HttpCacheItemKey,
) -> Result<Option<CachedUrlMetadata>, AnyError>;
}