From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9575585D5 for ; Mon, 21 Aug 2023 15:44:55 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 60BFB1724 for ; Mon, 21 Aug 2023 15:44:55 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 21 Aug 2023 15:44:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 7531042DA8 for ; Mon, 21 Aug 2023 15:44:54 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Mon, 21 Aug 2023 15:44:43 +0200 Message-Id: <20230821134444.620021-7-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230821134444.620021-1-l.wagner@proxmox.com> References: <20230821134444.620021-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.038 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [RFC proxmox-perl-rs 6/7] cache: add bindings for `SharedCache` from `proxmox-cache` X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Aug 2023 13:44:55 -0000 Signed-off-by: Lukas Wagner --- 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 as "Proxmox::RS::SharedCache"); + + #[export(raw_return)] + fn new(#[raw] class: Value, base_dir: &str) -> Result { + 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, + ) { + 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 { + 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