From ea4d87816bf7dc1bfd68b80c138c4a9557258fb8 Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Tue, 6 Aug 2024 14:59:57 +0200 Subject: [PATCH] cache: add bindings for `SharedCache` This is a simple, cache implementation which can be accessed from multiple processes. It also supports storing a range of historical values. Signed-off-by: Lukas Wagner [wb: also update pmg-rs/Cargo.toml and both d/control files] Signed-off-by: Wolfgang Bumiller --- common/pkg/Makefile | 1 + common/src/mod.rs | 1 + common/src/shared_cache.rs | 67 ++++++++++++++++++++++++++++++++++++++ pmg-rs/Cargo.toml | 1 + pmg-rs/debian/control | 1 + pve-rs/Cargo.toml | 1 + pve-rs/debian/control | 1 + 7 files changed, 73 insertions(+) create mode 100644 common/src/shared_cache.rs diff --git a/common/pkg/Makefile b/common/pkg/Makefile index 78f2d64..76c8ebd 100644 --- a/common/pkg/Makefile +++ b/common/pkg/Makefile @@ -24,6 +24,7 @@ PERLMOD_PACKAGES := \ Proxmox::RS::APT::Repositories \ Proxmox::RS::CalendarEvent \ Proxmox::RS::Notify \ + Proxmox::RS::SharedCache \ Proxmox::RS::Subscription PERLMOD_PACKAGE_FILES := $(addsuffix .pm,$(subst ::,/,$(PERLMOD_PACKAGES))) diff --git a/common/src/mod.rs b/common/src/mod.rs index c3574f4..badfc98 100644 --- a/common/src/mod.rs +++ b/common/src/mod.rs @@ -2,4 +2,5 @@ pub mod apt; mod calendar_event; pub mod logger; pub mod notify; +pub mod shared_cache; mod subscription; diff --git a/common/src/shared_cache.rs b/common/src/shared_cache.rs new file mode 100644 index 0000000..ae688f6 --- /dev/null +++ b/common/src/shared_cache.rs @@ -0,0 +1,67 @@ +#[perlmod::package(name = "Proxmox::RS::SharedCache")] +mod export { + use std::time::Duration; + + use anyhow::Error; + use nix::sys::stat::Mode; + use perlmod::Value; + use serde::Deserialize; + use serde_json::Value as JSONValue; + + use proxmox_shared_cache::SharedCache; + use proxmox_sys::fs::CreateOptions; + + pub struct CacheWrapper(SharedCache); + + perlmod::declare_magic!(Box : &CacheWrapper as "Proxmox::RS::SharedCache"); + + #[derive(Deserialize)] + struct Params { + path: String, + owner: u32, + group: u32, + entry_mode: u32, + keep_old: u32, + } + + #[export(raw_return)] + fn new(#[raw] class: Value, params: Params) -> Result { + let options = CreateOptions::new() + .owner(params.owner.into()) + .group(params.group.into()) + .perm(Mode::from_bits_truncate(params.entry_mode)); + + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new( + CacheWrapper ( + SharedCache::new(params.path, options, params.keep_old)? + ) + ))) + } + + #[export] + fn set( + #[try_from_ref] this: &CacheWrapper, + value: JSONValue, + lock_timeout: u64, + ) -> Result<(), Error> { + this.0.set(&value, Duration::from_secs(lock_timeout)) + } + + #[export] + fn get(#[try_from_ref] this: &CacheWrapper) -> Result, Error> { + this.0.get() + } + + #[export] + fn get_last( + #[try_from_ref] this: &CacheWrapper, + number_of_old_entries: u32, + ) -> Result, Error> { + this.0.get_last(number_of_old_entries) + } + + #[export] + fn delete(#[try_from_ref] this: &CacheWrapper, lock_timeout: u64) -> Result<(), Error> { + this.0.delete(Duration::from_secs(lock_timeout)) + } +} diff --git a/pmg-rs/Cargo.toml b/pmg-rs/Cargo.toml index 2af92ee..8ed128e 100644 --- a/pmg-rs/Cargo.toml +++ b/pmg-rs/Cargo.toml @@ -37,6 +37,7 @@ proxmox-http = { version = "0.9", features = ["client-sync", "client-trait"] } proxmox-http-error = "0.1.0" proxmox-log = "0.2" proxmox-notify = "0.4" +proxmox-shared-cache = "0.1.0" proxmox-subscription = "0.4" proxmox-sys = "0.6" proxmox-tfa = { version = "5", features = ["api"] } diff --git a/pmg-rs/debian/control b/pmg-rs/debian/control index 2904b55..0e1d1ac 100644 --- a/pmg-rs/debian/control +++ b/pmg-rs/debian/control @@ -27,6 +27,7 @@ Build-Depends: cargo:native , librust-proxmox-http-error-0.1+default-dev, librust-proxmox-log-0.2+default-dev, librust-proxmox-notify-0.4+default-dev, + librust-proxmox-shared-cache-0.1+default-dev, librust-proxmox-subscription-0.4+default-dev, librust-proxmox-sys-0.6+default-dev, librust-proxmox-tfa-5+api-dev, diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index b6abf6c..04cbcee 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -40,6 +40,7 @@ proxmox-log = "0.2" proxmox-notify = { version = "0.4", features = ["pve-context"] } proxmox-openid = "0.10" proxmox-resource-scheduling = "0.3.0" +proxmox-shared-cache = "0.1.0" proxmox-subscription = "0.4" proxmox-sys = "0.6" proxmox-tfa = { version = "5", features = ["api"] } diff --git a/pve-rs/debian/control b/pve-rs/debian/control index 6191fdb..cdac4ec 100644 --- a/pve-rs/debian/control +++ b/pve-rs/debian/control @@ -28,6 +28,7 @@ Build-Depends: cargo:native , librust-proxmox-notify-0.4+pve-context-dev, librust-proxmox-openid-0.10+default-dev, librust-proxmox-resource-scheduling-0.3+default-dev, + librust-proxmox-shared-cache-0.1+default-dev, librust-proxmox-subscription-0.4+default-dev, librust-proxmox-sys-0.6+default-dev, librust-proxmox-tfa-5+api-dev,