Package org.chaidb.db.helper.cache

Source Code of org.chaidb.db.helper.cache.Cache

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

package org.chaidb.db.helper.cache;

import org.chaidb.db.helper.cache.algorithm.FIFO;
import org.chaidb.db.helper.cache.algorithm.LRU;
import org.chaidb.db.helper.cache.algorithm.ReplacementAlgorithm;
import org.chaidb.db.helper.cache.persistence.CachePersistenceException;
import org.chaidb.db.helper.cache.persistence.DummyPersistenceListener;
import org.chaidb.db.helper.cache.persistence.NoSuchKeyException;
import org.chaidb.db.helper.cache.persistence.PersistenceListener;

public class Cache {
    public Cache(int cacheSize) {
        this(cacheSize, null);
    }

    public Cache(int cacheSize, String name) {
        this(cacheSize, "LRU", name);
    }

    public Cache(int cacheSize, String algorithmName, String name) {
        this(cacheSize, algorithmName, new DummyPersistenceListener(), name);
    }

    public Cache(int cacheSize, String algorithmName, PersistenceListener listener, String name) {
        if (listener == null) {
            throw new IllegalArgumentException("listener must NOT be null");
        } else {
            this.listener = listener;
        }

        if (cacheSize <= 0) {
            throw new IllegalArgumentException("cacheSize must be larger than 0");
        } else {
            if (algorithmName == null) {
                algorithm = new LRU(cacheSize);
            } else if (algorithmName.equals("LRU")) {
                algorithm = new LRU(cacheSize);
            } else if (algorithmName.equals("FIFO")) {
                algorithm = new FIFO(cacheSize);
            } else {
                throw new IllegalArgumentException("unknown replacement algorithm: " + algorithmName);
            }
        }
        this.name = name;
        administrator.register(this);
    }

    public String getName() {
        return this.name;
    }

    public String setName(String name) {
        String oldName = this.name;
        this.name = name;
        return oldName;
    }

    public boolean containsKey(Object key) throws CacheException {
        synchronized (algorithm) {
            if (algorithm.containsKey(key)) {
                return true;
            } else {
                try {
                    listener.retrieve(key);
                } catch (CachePersistenceException e) {
                    throw new CacheException("retrieve value from underlying storage error", e);
                } catch (NoSuchKeyException e) {
                    return false;
                }
                return true;
            }
        }
    }

    public Object getFromCache(Object key) throws CacheException {
        Object value = null;
        synchronized (algorithm) {
            if (!algorithm.containsKey(key)) {
                try {
                    value = listener.retrieve(key);
                } catch (CachePersistenceException e) {
                    throw new CacheException("retrieve value from underlying storage error", e);
                } catch (NoSuchKeyException e) {
                    return null;
                }
                algorithm.put(key, value);
            } else {
                value = algorithm.get(key);
            }
        }

        return value;
    }

    public Object putIntoCache(Object key, Object value) throws CacheException {
        synchronized (algorithm) {
            return algorithm.put(key, value);
        }
    }

    public Object removeFromCache(Object key) throws CacheException {
        synchronized (algorithm) {
            return algorithm.remove(key);
        }
    }

    public void clearCache() {
        synchronized (algorithm) {
            algorithm.clear();
        }
    }

    public int size() {
        synchronized (algorithm) {
            return algorithm.size();
        }
    }

    public int maxSize() {
        return algorithm.getSizeBoundary();
    }

    protected final ReplacementAlgorithm algorithm;
    protected final PersistenceListener listener;

    private String name;

    private static CacheAdministrator administrator = CacheAdministrator.getInstance();
}
TOP

Related Classes of org.chaidb.db.helper.cache.Cache

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.