All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aemds.guide.cache.CacheManager Maven / Gradle / Ivy

/*******************************************************************************
 * ADOBE CONFIDENTIAL
 * ___________________
 * Copyright 2016 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by all applicable intellectual property
 * laws, including trade secret and copyright laws.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 ******************************************************************************/

package com.adobe.aemds.guide.cache;

import com.adobe.aemds.guide.cache.api.CacheConfig;
import com.adobe.aemds.guide.cache.api.CacheStore;
import com.adobe.aemds.guide.cache.api.CacheStoreProvider;
import com.adobe.aemds.guide.service.AdaptiveFormConfigurationService;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component(immediate = true, label = "Adaptive Form Cache Manager",
        description = "Adaptive Form Cache Manager Service")
@Service(CacheManager.class)
public class CacheManager {
    private Map caches = new ConcurrentHashMap();

    private static float LOAD_FACTOR = 0.75f;

    @Reference
    private CacheStoreProvider provider;

    @Reference
    private AdaptiveFormConfigurationService configuration;

    private CacheConfig existingCacheConfig;

    protected void activate() {
        int maximumEntries = configuration.maximumCacheEntries();
        CacheConfig cacheConfig = new CacheConfig((int) (maximumEntries * LOAD_FACTOR), maximumEntries);
        if (existingCacheConfig != null &&
                existingCacheConfig.getMaximumEntries() != cacheConfig.getMaximumEntries()) {
            Set cacheNames = caches.keySet();
            caches.clear();
            for (String cacheName : cacheNames) {
                caches.put(cacheName, getNewCache(existingCacheConfig));
            }
        }
        existingCacheConfig = cacheConfig;
    }

    private Cache getNewCache(CacheConfig cacheConfig) {
        CacheStore cacheStore = provider.newCacheStore(cacheConfig);
        return new Cache(cacheStore);
    }

    public void dumpCacheInfo() {
        //TODO [PERFORMANCE]: Is this intentionally left blank? Can be used for listing the cached items list - useful for
        //testing and checking - before taking performance numbers !!!
    }

    public Cache getOrCreateCache(String cacheName) {
        if (!caches.containsKey(cacheName)) {
            int maximumEntries = configuration.maximumCacheEntries();
            CacheConfig cacheConfig = new CacheConfig((int) (maximumEntries * LOAD_FACTOR), maximumEntries);
            caches.put(cacheName, getNewCache(cacheConfig));
        }
        return caches.get(cacheName);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy