mirror of
https://github.com/denoland/deno.git
synced 2024-12-27 01:29:14 -05:00
1cefa831fd
Closes #15633
76 lines
2.1 KiB
Rust
76 lines
2.1 KiB
Rust
// 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;
|
|
|
|
/// 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>;
|
|
}
|