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

org.yestech.cache.impl.MemcachedAsynchronousCacheManager Maven / Gradle / Ivy

The newest version!
package org.yestech.cache.impl;

import net.spy.memcached.MemcachedClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.yestech.cache.ICacheManager;
import org.yestech.lib.util.Pair;

import java.util.Map;
import java.util.Collection;


/**
 * An implements of an asynchronous cache using Memcached using the
 * Spy Client.
 *
 */
@SuppressWarnings("unchecked")
public class MemcachedAsynchronousCacheManager implements ICacheManager {
    final private static Logger logger = LoggerFactory.getLogger(MemcachedAsynchronousCacheManager.class);

    private int expireTime;

    private MemcachedClient cache;

    public int getExpireTime() {
        return expireTime;
    }

    @Required
    public void setExpireTime(int expireTime) {
        this.expireTime = expireTime;
    }

    public MemcachedClient getCache() {
        return cache;
    }

    @Required
    public void setCache(MemcachedClient cache) {
        this.cache = cache;
    }

    @Override
    public boolean contains(String k) {
        return cache.asyncGet(k) != null;
    }

    @Override
    public void putAll(Map collection) {
        for (Map.Entry entry : collection.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public void put(Pair entry) {
        put(entry.getFirst(), entry.getSecond());
    }

    @Override
    public void put(String k, V v) {
        cache.add(k, expireTime, v);
    }

    @Override
    public V get(String key) {
        long start = System.currentTimeMillis();

        V result = (V) cache.get(key);
        if (logger.isDebugEnabled()) {
            logger.debug("Time to retrieve from cache: " + (System.currentTimeMillis() - start) + " ms");
        }
        if (result == null) {
            //reference is gone so remove from cache.....
            if (logger.isInfoEnabled()) {
                logger.info("Reference to object associated with cache element [" + key
                        + "] garbage collected remove from cache");
            }
            flush(key);
        }
        return result;
    }

    @Override
    public void flushAll() {
        cache.flush();
    }

    @Override
    public void flush(String key) {
        cache.delete(key);
    }

    @Override
    public Collection keys() {
        throw new UnsupportedOperationException("Not available");
    }

    @Override
    public Collection values() {
        throw new UnsupportedOperationException("Not available");
    }

    /**
     * Stores a cache to some type of durable storage.  Not gaurenteed to be implemented, dependant on the concrete
     * implementation.  This method should perform a NoOp if it is not supported.
     */
    @Override
    public void store() {

    }

    /**
     * Loads a cache from some type of durable storage.  Not gaurenteed to be implemented, dependant on the concrete
     * implementation.  This method should perform a NoOp if it is not supported.
     */
    @Override
    public void load() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy