summaryrefslogtreecommitdiff
path: root/ext/cache/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/cache/lib.rs')
-rw-r--r--ext/cache/lib.rs49
1 files changed, 13 insertions, 36 deletions
diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs
index 845a6ad86..cee5b7e56 100644
--- a/ext/cache/lib.rs
+++ b/ext/cache/lib.rs
@@ -29,7 +29,6 @@ deno_core::extension!(deno_cache,
op_cache_storage_has<CA>,
op_cache_storage_delete<CA>,
op_cache_put<CA>,
- op_cache_put_finish<CA>,
op_cache_match<CA>,
op_cache_delete<CA>,
],
@@ -55,9 +54,9 @@ pub struct CachePutRequest {
pub request_url: String,
pub request_headers: Vec<(ByteString, ByteString)>,
pub response_headers: Vec<(ByteString, ByteString)>,
- pub response_has_body: bool,
pub response_status: u16,
pub response_status_text: String,
+ pub response_rid: Option<ResourceId>,
}
#[derive(Deserialize, Serialize, Debug)]
@@ -90,27 +89,24 @@ pub struct CacheDeleteRequest {
#[async_trait(?Send)]
pub trait Cache: Clone + 'static {
- type CachePutResourceType: Resource;
+ type CacheMatchResourceType: Resource;
async fn storage_open(&self, cache_name: String) -> Result<i64, AnyError>;
async fn storage_has(&self, cache_name: String) -> Result<bool, AnyError>;
async fn storage_delete(&self, cache_name: String) -> Result<bool, AnyError>;
- /// Create a put request.
- async fn put_create(
+ /// Put a resource into the cache.
+ async fn put(
&self,
request_response: CachePutRequest,
- ) -> Result<Option<Rc<Self::CachePutResourceType>>, AnyError>;
- /// Complete a put request.
- async fn put_finish(
- &self,
- resource: Rc<Self::CachePutResourceType>,
+ resource: Option<Rc<dyn Resource>>,
) -> Result<(), AnyError>;
+
async fn r#match(
&self,
request: CacheMatchRequest,
) -> Result<
- Option<(CacheMatchResponseMeta, Option<Rc<dyn Resource>>)>,
+ Option<(CacheMatchResponseMeta, Option<Self::CacheMatchResourceType>)>,
AnyError,
>;
async fn delete(&self, request: CacheDeleteRequest)
@@ -155,38 +151,19 @@ where
}
#[op2(async)]
-#[smi]
pub async fn op_cache_put<CA>(
state: Rc<RefCell<OpState>>,
#[serde] request_response: CachePutRequest,
-) -> Result<Option<ResourceId>, AnyError>
-where
- CA: Cache,
-{
- let cache = get_cache::<CA>(&state)?;
- match cache.put_create(request_response).await? {
- Some(resource) => {
- let rid = state.borrow_mut().resource_table.add_rc_dyn(resource);
- Ok(Some(rid))
- }
- None => Ok(None),
- }
-}
-
-#[op2(async)]
-pub async fn op_cache_put_finish<CA>(
- state: Rc<RefCell<OpState>>,
- #[smi] rid: ResourceId,
) -> Result<(), AnyError>
where
CA: Cache,
{
let cache = get_cache::<CA>(&state)?;
- let resource = state
- .borrow_mut()
- .resource_table
- .get::<CA::CachePutResourceType>(rid)?;
- cache.put_finish(resource).await
+ let resource = match request_response.response_rid {
+ Some(rid) => Some(state.borrow_mut().resource_table.take_any(rid)?),
+ None => None,
+ };
+ cache.put(request_response, resource).await
}
#[op2(async)]
@@ -202,7 +179,7 @@ where
match cache.r#match(request).await? {
Some((meta, None)) => Ok(Some(CacheMatchResponse(meta, None))),
Some((meta, Some(resource))) => {
- let rid = state.borrow_mut().resource_table.add_rc_dyn(resource);
+ let rid = state.borrow_mut().resource_table.add(resource);
Ok(Some(CacheMatchResponse(meta, Some(rid))))
}
None => Ok(None),