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

com.adobe.aemds.guide.cache.Cache 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.CacheStore;
import com.adobe.aemds.guide.service.GuideException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONObject;

import java.util.Calendar;

public class Cache {

    private CacheStore cacheStore;

    public Cache(CacheStore cacheStore) {
        this.cacheStore = cacheStore;
    }

    /**
     * Put an object in the cache with a key that can be used to retrieve the object later. If the object already exists
     * it will be replaced
     *
     * @param key   Key against which the value is stored in cache
     * @param value value to store in the cache
     * @return Object previously associated with the key in the cache or null if modified time equals LONG.MAX_VALUE
     */
    public Object put(Object key, Object value, Calendar modifiedTime) {
        // if last modified time is not equal to LONG.MAX_VALUE
        // only then cache the object, else it would always return the cached version
        // since next time comparison with cached object would always say the entry to not be stale
        if(modifiedTime == null || modifiedTime.getTimeInMillis() != Long.MAX_VALUE) {
            CacheObject cacheObject = new CacheObject(value, modifiedTime);
            return cacheStore.put(key, cacheObject);
        } else {
            return null;
        }
    }

    /**
     * Get an object from the cache. If not present null is returned
     *
     * @param key
     * @return
     */
    public Object get(Object key) {
        CacheObject cacheObject = cacheStore.get(key);
        if (cacheObject == null) {
            return null;
        }
        return cacheObject.getCacheObject();
    }

    /**
     * Clears all the entries in the cache
     */
    public void clearAll() {
        cacheStore.clearAll();
    }

    /**
     * Removes the value from the cache associated with the key
     *
     * @param key key associated with the object that has to be removed
     */
    public void clear(String key) {
        cacheStore.clear(key);
    }

    /**
     * Returns whether the object in the cache associated with the key is valid given the last update time of the key
     *
     * @param key        Key of the object whose validity has to be checked
     * @param updateTime Calendar Instance of the update time of the
     * @return false if the entry is stale in the cache otherwise true. Also returns true, if key doesn't exist in
     * Cache and  false, if Calendar Instance is null
     */
    public boolean isCacheEntryStale(Object key, Calendar updateTime) {
        if (!cacheStore.entryExists(key)) {
            return true;
        }
        if (updateTime == null) {
            return false;
        }
        CacheObject cacheObject = cacheStore.get(key);
        return cacheObject.getLastModifiedTime().compareTo(updateTime) < 0;
    }

    /**
     * Returns if a value is associated with the key
     *
     * @param key
     * @return true if a value is associated with the key false otherwise
     */
    public boolean entryExists(Object key) {
        return cacheStore.entryExists(key);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy