aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/cache.rs
blob: 15fc99af8af49ac727ee8b521580e52181022556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// -*- coding: utf-8 -*-
//
// Simple CMS
//
// Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![allow(dead_code)] //TODO

use cms_ident::Ident;
use lru::LruCache;
use tokio::sync::Mutex;

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
enum CacheKey {
    //TODO
    Page(Ident),
}

#[derive(Debug)]
enum CacheValue {
    //TODO
    Blob(Vec<u8>),
}

pub struct CmsCache {
    cache: Option<Mutex<LruCache<CacheKey, CacheValue>>>,
}

impl CmsCache {
    pub fn new(cache_size: usize) -> Self {
        let cache = if cache_size == 0 {
            None
        } else {
            let cache_size = cache_size.try_into().unwrap();
            Some(Mutex::new(LruCache::new(cache_size)))
        };
        Self { cache }
    }

    pub async fn clear(&self) {
        if let Some(cache) = &self.cache {
            let mut cache = cache.lock().await;
            if !cache.is_empty() {
                cache.clear();
                println!("Backend cache cleared.");
            }
        }
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface