opendal/services/cacache/
core.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use crate::*;
21
22#[derive(Clone)]
23pub struct CacacheCore {
24    pub path: String,
25}
26
27impl Debug for CacacheCore {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        f.debug_struct("CacacheCore")
30            .field("path", &self.path)
31            .finish()
32    }
33}
34
35impl CacacheCore {
36    pub async fn get(&self, key: &str) -> Result<Option<bytes::Bytes>> {
37        let cache_path = self.path.clone();
38        let cache_key = key.to_string();
39
40        let data = cacache::read(&cache_path, &cache_key).await;
41
42        match data {
43            Ok(bs) => Ok(Some(bytes::Bytes::from(bs))),
44            Err(cacache::Error::EntryNotFound(_, _)) => Ok(None),
45            Err(err) => Err(Error::new(ErrorKind::Unexpected, "cacache get failed")
46                .with_operation("CacacheCore::get")
47                .set_source(err)),
48        }
49    }
50
51    pub async fn set(&self, key: &str, value: bytes::Bytes) -> Result<()> {
52        let cache_path = self.path.clone();
53        let cache_key = key.to_string();
54
55        cacache::write(&cache_path, &cache_key, value.to_vec())
56            .await
57            .map_err(|err| {
58                Error::new(ErrorKind::Unexpected, "cacache set failed")
59                    .with_operation("CacacheCore::set")
60                    .set_source(err)
61            })?;
62
63        Ok(())
64    }
65
66    pub async fn delete(&self, key: &str) -> Result<()> {
67        let cache_path = self.path.clone();
68        let cache_key = key.to_string();
69
70        cacache::remove(&cache_path, &cache_key)
71            .await
72            .map_err(|err| {
73                Error::new(ErrorKind::Unexpected, "cacache delete failed")
74                    .with_operation("CacacheCore::delete")
75                    .set_source(err)
76            })?;
77
78        Ok(())
79    }
80
81    pub async fn metadata(&self, key: &str) -> Result<Option<cacache::Metadata>> {
82        let cache_path = self.path.clone();
83        let cache_key = key.to_string();
84
85        let metadata = cacache::metadata(&cache_path, &cache_key).await;
86
87        match metadata {
88            Ok(Some(md)) => Ok(Some(md)),
89            Ok(None) => Ok(None),
90            Err(cacache::Error::EntryNotFound(_, _)) => Ok(None),
91            Err(err) => Err(Error::new(ErrorKind::Unexpected, "cacache metadata failed")
92                .with_operation("CacacheCore::metadata")
93                .set_source(err)),
94        }
95    }
96}