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

com.astamuse.asta4d.util.MemorySafeResourceCache Maven / Gradle / Ivy

Go to download

core functionalities of asta4d framework, including template and snippt implemention

There is a newer version: 1.2-M2
Show newest version
package com.astamuse.asta4d.util;

import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * We want to cache the result of not found resources but a potential DDOS attack can be performed against this wish. Thus we cache the not
 * found results in a SoftRreferenced map.
 * 
 * @author e-ryu
 * 
 * @param 
 * @param 
 */
public class MemorySafeResourceCache {

    public static class ResouceHolder {
        private T resource;

        ResouceHolder(T _res) {
            resource = _res;
        }

        public T get() {
            return resource;
        }

        public boolean exists() {
            return resource != null;
        }
    }

    private Map> existingResourceMap;

    private SoftReference>> notExistingResourceMapRef = null;

    private final ResouceHolder notExistingHolder = new ResouceHolder<>(null);

    public MemorySafeResourceCache() {
        // a copy on write map would be better
        existingResourceMap = new ConcurrentHashMap<>();
    }

    /**
     * 
     * @param key
     *            throw NullPointerException when key is null
     * @param resource
     *            null means the resource of the given key is not existing
     */
    public void put(K key, V resource) {
        if (key == null) {
            throw new NullPointerException();
        }
        if (resource == null) {
            getNotExistingResourceMap().put(key, notExistingHolder);
        } else {
            existingResourceMap.put(key, new ResouceHolder<>(resource));
        }
    }

    private Map> getNotExistingResourceMap() {
        Map> map = null;
        if (notExistingResourceMapRef == null) {
            map = new ConcurrentHashMap<>();
            notExistingResourceMapRef = new SoftReference<>(map);
        } else {
            map = notExistingResourceMapRef.get();
            if (map == null) {
                map = new ConcurrentHashMap<>();
                notExistingResourceMapRef = new SoftReference<>(map);
            }
        }
        return map;
    }

    public ResouceHolder get(K key) {
        ResouceHolder holder = existingResourceMap.get(key);
        if (holder == null) {
            holder = getNotExistingResourceMap().get(key);
        }
        return holder;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy