001/*
002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob;
020
021import java.io.IOException;
022import java.nio.file.Path;
023import java.util.Map;
024
025import org.nuxeo.common.utils.SizeUtils;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * Configuration for a cache.
030 *
031 * @since 11.1
032 */
033public class CachingConfiguration extends PropertyBasedConfiguration {
034
035    public static final String CACHE_SIZE_PROPERTY = "cachesize";
036
037    public static final String CACHE_COUNT_PROPERTY = "cachecount";
038
039    public static final String CACHE_MIN_AGE_PROPERTY = "cacheminage";
040
041    public static final String DEFAULT_CACHE_SIZE = "100 mb";
042
043    public static final String DEFAULT_CACHE_COUNT = "10000";
044
045    public static final String DEFAULT_CACHE_MIN_AGE = "3600"; // 1h
046
047    public final Path dir;
048
049    public final long maxSize;
050
051    public final long maxCount;
052
053    public final long minAge;
054
055    public CachingConfiguration(String systemPropertyPrefix, Map<String, String> properties) throws IOException {
056        super(systemPropertyPrefix, properties);
057        dir = Framework.createTempDirectory("nxbincache.");
058        String maxSizeProp = getProperty(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
059        String maxCountProp = getProperty(CACHE_COUNT_PROPERTY, DEFAULT_CACHE_COUNT);
060        String minAgeProp = getProperty(CACHE_MIN_AGE_PROPERTY, DEFAULT_CACHE_MIN_AGE);
061        maxSize = SizeUtils.parseSizeInBytes(maxSizeProp);
062        maxCount = Long.parseLong(maxCountProp);
063        minAge = Long.parseLong(minAgeProp);
064    }
065
066    public CachingConfiguration(Path dir, long maxSize, long maxCount, long minAge) {
067        super(null, null);
068        this.dir = dir;
069        this.maxSize = maxSize;
070        this.maxCount = maxCount;
071        this.minAge = minAge;
072    }
073
074}