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

com.box.sdk.InMemoryLRUAccessTokenCache Maven / Gradle / Ivy

There is a newer version: 4.11.1
Show newest version
package com.box.sdk;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Use this class to create an in-memory LRU (least recently used) access token cache to be
 * passed to BoxDeveloperEditionAPIConnection.
 */
public class InMemoryLRUAccessTokenCache implements IAccessTokenCache {

    private final Map cache;

    /**
     * Creates an in-memory LRU access token cache.
     * @param maxEntries    maximum number of entries to store.
     */
    public InMemoryLRUAccessTokenCache(final int maxEntries) {
        this.cache = new LinkedHashMap(maxEntries, 0.75F, true) {
            private static final long serialVersionUID = -187234623489L;
            @Override
            protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
                return size() > maxEntries;
            }
        };
    }

    /**
     * Add an entry to the cache.
     * @param key       key to use.
     * @param value     access token information to store.
     */
    public void put(String key, String value) {
        synchronized (this.cache) {
            this.cache.put(key, value);
        }
    }

    /**
     * Get an entry from the cache.
     * @param key       key to look for.
     * @return          access token information.
     */
    public String get(String key) {
        synchronized (this.cache) {
            return this.cache.get(key);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy