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

com.buschmais.xo.impl.cache.TransactionalCache Maven / Gradle / Ivy

The newest version!
package com.buschmais.xo.impl.cache;

import java.util.Collection;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
 * Transactional cache which handles read and write access to instances.
 *
 * @param 
 *     The datastore id type.
 */
public class TransactionalCache {

    @RequiredArgsConstructor
    @EqualsAndHashCode
    @ToString
    private static class CacheKey {

        private final Id id;

    }

    /**
     * The access mode indicating how an instance has been accessed.
     */
    public enum Mode {
        READ,
        WRITE
    }

    /**
     * The read cache.
     */
    private final WeakReferenceCache readCache;

    /**
     * The write cache.
     */
    private final ReferenceCache writeCache;

    /**
     * Constructor.
     */
    public TransactionalCache() {
        this.readCache = new WeakReferenceCache<>();
        this.writeCache = new ReferenceCache<>();
    }

    /**
     * Put an instance into the cache.
     *
     * @param id
     *     The id.
     * @param value
     *     The instance.
     * @param mode
     *     The mode.
     */
    public void put(Id id, Object value, Mode mode) {
        if (Mode.WRITE.equals(mode)) {
            writeCache.put(id, value);
        }
        readCache.put(new CacheKey(id), value);
    }

    /**
     * Lookup an instance in the cache identified by its id.
     *
     * @param id
     *     The id.
     * @param mode
     *     The mode.
     * @return The corresponding instance or null if no instance is
     * available.
     */
    public Object get(Id id, Mode mode) {
        Object value = writeCache.get(id);
        if (value == null) {
            value = readCache.get(new CacheKey(id));
            if (value != null && Mode.WRITE.equals(mode)) {
                writeCache.put(id, value);
            }
        }
        return value;
    }

    /**
     * Removes an instance from the cache.
     *
     * @param id
     *     The id.
     */
    public void remove(Id id) {
        readCache.remove(new CacheKey(id));
        writeCache.remove(id);
    }

    /**
     * Flush the cache, i.e. remove all entries which are written.
     */
    public void flush() {
        writeCache.clear();
    }

    /**
     * Clear the cache.
     */
    public void clear() {
        writeCache.clear();
        readCache.clear();
    }

    /**
     * Returns the instance which have been read.
     *
     * @return The read instances.
     */
    public Collection readInstances() {
        return readCache.values();
    }

    /**
     * Returns the instance which have been written.
     *
     * @return The written instances.
     */
    public Collection writtenInstances() {
        return writeCache.values();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy