mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
07.50.2019 219ec32448572da39d629dfcd9c37ec362378ffd
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
package de.micromata.borgbutler.cache;
 
import de.micromata.borgbutler.config.ConfigurationHandler;
import lombok.Getter;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class JCSCache {
    private static Logger log = LoggerFactory.getLogger(JCSCache.class);
    private static JCSCache instance = new JCSCache();
    private static final String CONFIG_FILE = "jcs-basic-config.properties";
    public static final String CACHE_DIR_NAME = "cache";
 
    public static JCSCache getInstance() {
        return instance;
    }
 
    @Getter
    private File cacheDir;
 
    public <K, V> CacheAccess<K, V> getJCSCache(String region) {
        return JCS.getInstance(region);
    }
 
    private JCSCache() {
        cacheDir = new File(ConfigurationHandler.getInstance().getWorkingDir(), CACHE_DIR_NAME);
        if (!cacheDir.exists()) {
            log.info("Creating cache dir: " + cacheDir.getAbsolutePath());
            cacheDir.mkdir();
        }
 
        Properties props = new Properties();
        try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(CONFIG_FILE)) {
            props.load(inputStream);
        } catch (IOException ex) {
            log.error("Error while loading jcs config file '" + CONFIG_FILE + "': " + ex.getMessage(), ex);
        }
        props.setProperty("jcs.auxiliary.DC.attributes.DiskPath", cacheDir.getAbsolutePath());
        //props.setProperty("jcs.auxiliary.DC2.attributes.DiskPath", cacheDir.getAbsolutePath());
        //int cacheMaxDiscSizeMB = configuration.getCacheMaxDiscSizeMB();
        //log.info("Using cache size for archive contents: " + cacheMaxDiscSizeMB + "MB.");
        //props.setProperty("jcs.auxiliary.DC2.attributes.MaxKeySize", String.valueOf(cacheMaxDiscSizeMB * 1000));
        JCS.setConfigProperties(props);
    }
}