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

com.mercadopago.core.MPCache Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.mercadopago.core;

import java.lang.ref.SoftReference;
import java.util.HashMap;

/**
 * Mercado Pago SDK
 * MPCache class
 *
 * Created by Eduardo Paoletta on 12/12/16.
 */
class MPCache {

    private static SoftReference> cache = null;

    /**
     * Auxiliar method. It returns a Map with cached responses from the soft references variable.
     * If the map does not exists, its instantiated and then returned.
     *
     * @return      HashMap object
     */
    private static HashMap getMapCache() {
        if (cache == null || cache.get() == null) {
            cache = new SoftReference(new HashMap());
        }
        return cache.get();
    }

    /**
     * Inserts an entry to the cache.
     *
     * @param key           String with cache entry key
     * @param response      MPApiResponse object to be cached
     */
    static void addToCache(String key, MPApiResponse response) {
        HashMap mapCache = getMapCache();
        mapCache.put(key, response);
    }

    /**
     * Retrieves an entry from the cache.
     *
     * @param key           String with cache entry key
     * @return              MPApiResponse cached object, null if it does not exists
     */
    static MPApiResponse getFromCache(String key) {
        HashMap mapCache = getMapCache();
        MPApiResponse response = null;
        try {
            response = mapCache.get(key).clone();
        } catch (Exception ex) {
            // Do nothing
        }
        if (response != null) {
            response.fromCache = Boolean.TRUE;
        }
        return response;
    }

    /**
     * Removes an entry from the cache.
     *
     * @param key           String with cache entry key
     */
    static void removeFromCache(String key) {
        HashMap mapCache = getMapCache();
        mapCache.remove(key);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy