[pve-devel] [RFC proxmox-perl-rs 6/7] cache: add bindings for `SharedCache` from `proxmox-cache`

Lukas Wagner l.wagner at proxmox.com
Mon Aug 21 15:44:43 CEST 2023


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 common/pkg/Makefile |  1 +
 common/src/cache.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++
 common/src/mod.rs   |  1 +
 pve-rs/Cargo.toml   |  5 ++++
 pve-rs/src/lib.rs   |  1 +
 5 files changed, 67 insertions(+)
 create mode 100644 common/src/cache.rs

diff --git a/common/pkg/Makefile b/common/pkg/Makefile
index 7bf669f..e714570 100644
--- a/common/pkg/Makefile
+++ b/common/pkg/Makefile
@@ -26,6 +26,7 @@ Proxmox/RS/CalendarEvent.pm:
 	  Proxmox::RS::APT::Repositories \
 	  Proxmox::RS::CalendarEvent \
 	  Proxmox::RS::Notify \
+	  Proxmox::RS::SharedCache \
 	  Proxmox::RS::Subscription
 
 all: Proxmox/RS/CalendarEvent.pm
diff --git a/common/src/cache.rs b/common/src/cache.rs
new file mode 100644
index 0000000..e0ad1ea
--- /dev/null
+++ b/common/src/cache.rs
@@ -0,0 +1,59 @@
+#[perlmod::package(name = "Proxmox::RS::SharedCache")]
+mod export {
+    use anyhow::Error;
+    use nix::sys::stat::Mode;
+    use perlmod::Value;
+    use serde_json::Value as JSONValue;
+
+    use proxmox_cache::{Cache, SharedCache};
+    use proxmox_sys::fs::CreateOptions;
+
+    pub struct CacheWrapper {
+        cache: SharedCache,
+    }
+
+    perlmod::declare_magic!(Box<CacheWrapper> : &CacheWrapper as "Proxmox::RS::SharedCache");
+
+    #[export(raw_return)]
+    fn new(#[raw] class: Value, base_dir: &str) -> Result<Value, Error> {
+        let options = CreateOptions::new()
+            .root_only()
+            .perm(Mode::from_bits_truncate(0o700));
+
+        Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(
+            CacheWrapper {
+                cache: SharedCache::new(base_dir, options)?
+            }
+        )))
+    }
+
+    #[export]
+    fn set(
+        #[try_from_ref] this: &CacheWrapper,
+        key: &str,
+        value: JSONValue,
+        expires_in: Option<i64>,
+    ) {
+        if let Err(err) = this.cache.set(key, value, expires_in) {
+            log::error!("could not set cache entry '{key}': {err}")
+        }
+    }
+
+    #[export]
+    fn get(#[try_from_ref] this: &CacheWrapper, key: &str) -> Option<JSONValue> {
+        match this.cache.get(key) {
+            Ok(val) => val,
+            Err(err) => {
+                log::error!("could not get cache entry '{key}': {err}");
+                None
+            }
+        }
+    }
+
+    #[export]
+    fn delete(#[try_from_ref] this: &CacheWrapper, key: &str) {
+        if let Err(err) = this.cache.delete(key) {
+            log::error!("could not delete cache entry '{key}': {err}");
+        }
+    }
+}
diff --git a/common/src/mod.rs b/common/src/mod.rs
index c3574f4..136f7ae 100644
--- a/common/src/mod.rs
+++ b/common/src/mod.rs
@@ -1,4 +1,5 @@
 pub mod apt;
+pub mod cache;
 mod calendar_event;
 pub mod logger;
 pub mod notify;
diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml
index afd50f4..5b878b9 100644
--- a/pve-rs/Cargo.toml
+++ b/pve-rs/Cargo.toml
@@ -34,6 +34,7 @@ url = "2"
 perlmod = { version = "0.13", features = [ "exporter" ] }
 
 proxmox-apt = "0.10"
+proxmox-cache = "0.1.0"
 proxmox-http = { version = "0.9", features = ["client-sync", "client-trait"] }
 proxmox-http-error = "0.1.0"
 proxmox-notify = "0.2"
@@ -43,3 +44,7 @@ proxmox-subscription = "0.4"
 proxmox-sys = "0.5"
 proxmox-tfa = { version = "4.0.4", features = ["api"] }
 proxmox-time = "1.1.3"
+
+#[patch.crates-io]
+#proxmox-cache = { path = "../../proxmox/proxmox-cache" }
+#proxmox-sys = { path = "../../proxmox/proxmox-sys" }
diff --git a/pve-rs/src/lib.rs b/pve-rs/src/lib.rs
index d1915c9..7e39e85 100644
--- a/pve-rs/src/lib.rs
+++ b/pve-rs/src/lib.rs
@@ -4,6 +4,7 @@
 pub mod common;
 
 pub mod apt;
+pub mod cache;
 pub mod notify_context;
 pub mod openid;
 pub mod resource_scheduling;
-- 
2.39.2






More information about the pve-devel mailing list