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

com.j256.ormlite.dao.LruObjectCache Maven / Gradle / Ivy

package com.j256.ormlite.dao;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Cache for ORMLite which stores a certain number of items for each Class. Inserting an object into the cache once it
 * is full will cause the least-recently-used object to be ejected. They can be injected into a dao with the
 * {@link Dao#setObjectCache(ObjectCache)}.
 * 
 * 

* NOTE: If you set the capacity to be 100 then each Class will allow 100 items in the cache. If you have * 5 classes then the cache will hold 500 objects. *

* * @author graywatson */ public class LruObjectCache implements ObjectCache { private final int capacity; private final ConcurrentHashMap, Map> classMaps = new ConcurrentHashMap, Map>(); public LruObjectCache(int capacity) { this.capacity = capacity; } @Override public synchronized void registerClass(Class clazz) { Map objectMap = classMaps.get(clazz); if (objectMap == null) { objectMap = Collections.synchronizedMap(new LimitedLinkedHashMap(capacity)); classMaps.put(clazz, objectMap); } } @Override public T get(Class clazz, ID id) { Map objectMap = getMapForClass(clazz); if (objectMap == null) { return null; } Object obj = objectMap.get(id); @SuppressWarnings("unchecked") T castObj = (T) obj; return castObj; } @Override public void put(Class clazz, ID id, T data) { Map objectMap = getMapForClass(clazz); if (objectMap != null) { objectMap.put(id, data); } } @Override public void clear(Class clazz) { Map objectMap = getMapForClass(clazz); if (objectMap != null) { objectMap.clear(); } } @Override public void clearAll() { for (Map objectMap : classMaps.values()) { objectMap.clear(); } } @Override public void remove(Class clazz, ID id) { Map objectMap = getMapForClass(clazz); if (objectMap != null) { objectMap.remove(id); } } @Override public T updateId(Class clazz, ID oldId, ID newId) { Map objectMap = getMapForClass(clazz); if (objectMap == null) { return null; } Object obj = objectMap.remove(oldId); if (obj == null) { return null; } objectMap.put(newId, obj); @SuppressWarnings("unchecked") T castObj = (T) obj; return castObj; } @Override public int size(Class clazz) { Map objectMap = getMapForClass(clazz); if (objectMap == null) { return 0; } else { return objectMap.size(); } } @Override public int sizeAll() { int size = 0; for (Map objectMap : classMaps.values()) { size += objectMap.size(); } return size; } private Map getMapForClass(Class clazz) { Map objectMap = classMaps.get(clazz); if (objectMap == null) { return null; } else { return objectMap; } } /** * Little extension of the LimitedLinkedHashMap */ private static class LimitedLinkedHashMap extends LinkedHashMap { private static final long serialVersionUID = -4566528080395573236L; private final int capacity; public LimitedLinkedHashMap(int capacity) { super(capacity, 0.75F, true); this.capacity = capacity; } @Override protected boolean removeEldestEntry(Entry eldest) { return size() > capacity; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy