- package com.alex.commonutil.cache;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- /**
- * A simple implementation of {@link Cache}.
- * <p>
- * This cache implements most of the methods defined in {@link Cache}, using a
- * <tt>ConcurrentHashMap</tt> to store objects, which could provide better
- * performance in concurrent enviroment.
- * </p>
- */
- public abstract class AbstractCache implements Cache {
- /**
- * This cache uses a concurrent hash map to store object.
- */
- Map storage = new HashMap();
- public boolean hasKey(Object key) {
- if (key == null) {
- return false;
- }
- return storage.containsKey(key);
- }
- /**
- * Identifier of this cache, unique and not null.
- */
- private final Object identifier;
- /**
- * Constructs a new cache with speicified identifier. The identifier should
- * be unique and not <tt>null</tt>.
- *
- * @param identifier
- * the identifier of the new cache, which is unique and not null
- * @throws IllegalArgumentException
- * if the identifier is <tt>null</tt>
- */
- public AbstractCache(Object identifier) throws IllegalArgumentException {
- if (identifier == null) {
- throw new IllegalArgumentException(
- "The identifier of cache should NOT be null.");
- }
- this.identifier = identifier;
- }
- public void clear() {
- storage.clear();
- }
- public boolean hasObject(Object object) {
- if (object == null) {
- return false;
- }
- return storage.containsValue(object);
- }
- public Object fetch(Object key) {
- return storage.get(key);
- }
- public Collection fetchAll() {
- return storage.values();
- }
- public Object getIdentifier() {
- return this.identifier;
- }
- public void remove(Object key) {
- storage.remove(key);
- }
- public void save(Object key, Object value) throws IllegalArgumentException {
- if (key == null || value == null) {
- throw new IllegalArgumentException(
- "No null key or value allowed in cache.");
- }
- storage.put(key, value);
- }
- public void save(Object value) throws IllegalArgumentException {
- save(createKey(value), value);
- }
- public int size() {
- return storage.size();
- }
- public abstract Object createKey(Object object) throws IllegalArgumentException;
- public void saveAll(Collection values) {
- for (Iterator it = values.iterator(); it.hasNext();) {
- save(it.next());
- }
- }
- public void saveAll(Map values) {
- storage.putAll(values);
- }
- public Collection keys() {
- return storage.keySet();
- }
- }
- package com.alex.commonutil.cache;
- import java.util.Collection;
- import java.util.Map;
- /**
- * A cache keeps objects that may be used frequently in memory, it is made up of
- * a pool of entries . Usually cache is used to improve performance, because
- * memory is always faster that DB or disk I/O.
- * <p>
- * Caches do not accept <tt>null</tt> keys or values. They may store kinds of
- * objects, however, it is better that each cache stores only one type of
- * objects, using a key mapping for association.
- * </p>
- * <p>
- * For performance, reading methods is not necessary to be synchronized.
- * However, adding, updating and removing should be synchronized when caches
- * might be accessed by many threads.
- * </p>
- */
- public interface Cache {
- /**
- * Clears contents of this caches.
- */
- public void clear();
- /**
- * Creates a unique key for given object using some rules, if the object is
- * not in cache yet. If the object exists in cache, returns its key.
- *
- * @param object
- * an object in which the cache would save
- * @return the key of the object
- * @throws IllegalArgumentException
- * if the object is <tt>null</tt>
- */
- public Object createKey(Object object) throws IllegalArgumentException;
- /**
- * Returns the value to which maps the specified key in cache. Returns
- * <tt>null</tt> if the cache contains no mapping for this key.
- * <p>
- * If this method is not synchronized, the performance might be higher, and
- * would not brings chaos when accessed by more than one thread.
- * </p>
- *
- * @param key
- * key whose associated value is to be returned
- * @return the value to which the cache maps the specified key, or null if
- * the cacache contains no mapping for this key.
- */
- public Object fetch(Object key);
- /**
- * Returns all the objects stored in this cache.
- * <p>
- * This method is not needed to be synchronized.
- * </p>
- *
- * @return a <code>Collection</code> of objects stored in the cache
- * @see #fetch(Object)
- */
- public Collection fetchAll();
- /**
- * Returns the identifier of this cache, which should be <tt>unique</tt>
- * and NOT <tt>null</tt>.
- *
- * @return the unique identifier of this cache
- */
- public Object getIdentifier();
- /**
- * Returns <tt>true</tt> if this cache contains a mapping for the
- * specified key. (There can be at most one such mapping.)
- * <p>
- * If the key is <tt>null</tt>, this method always returns <tt>false</tt>.
- * Because cache does allow <tt>null</tt> keys or values.
- * </p>
- *
- * @param key
- * key whose presence in this cache is to be tested.
- * @return true if this map contains a mapping for the specified key
- */
- public boolean hasKey(Object key);
- /**
- * Returns <tt>true</tt> if the cache has one or more keys to the
- * specified object. More formally, returns <tt>true</tt> if and only if
- * the cache contains at least one mapping to an object.
- * <p>
- * Since cache does not allow null values, if the object is <tt>null</tt>,
- * always returns <tt>false</tt>.
- * </p>
- *
- * @param object
- * value whose presence in this map is to be tested.
- * @return <tt>true</tt> if this cache maps one or more keys to the
- * specified value
- */
- public boolean hasObject(Object object);
- /**
- * Removes the mapping for this key from this cache if the key presents. If
- * the key is null, this method has no effect on cache.
- *
- * @param key
- * mapping key in the cache which is to be removed
- */
- public void remove(Object key);
- /**
- * Store the specified object with the specified key in this cache. If the
- * cache previously contained a mapping for this key, the old value is
- * replaced by the specified value.
- * <p>
- * Note that this method do not accept null key or null value. And this
- * method should be synchronized when possibly accessed by more than one
- * thread.
- * </p>
- *
- * @param key
- * key with which the specified object is to be associated.
- * @param value
- * object to be stored in the cache, and associated with the
- * specified key
- * @throws IllegalArgumentException
- * if the key or value is null
- */
- public void save(Object key, Object value) throws IllegalArgumentException;
- /**
- * Save the object to the cache. The implementation must provide a key for
- * the association in the cache.
- * <p>
- * This method does not accepts null values. And it should be synchronized
- * in enviroment of multi-thread.
- * </p>
- *
- * @param value
- * the object to save
- * @throws IllegalArgumentException
- * if the value is null
- * @see #save(Object, Object)
- * @see #createKey(Object)
- */
- public void save(Object value) throws IllegalArgumentException;
- /**
- * Adds all of the elements in the specified collection to this cache.
- *
- * @param values
- * objects to be saved into this cache
- * @throws IllegalArgumentException
- * if one or more objects are null
- */
- public void saveAll(Collection values) throws IllegalArgumentException;
- /**
- * Copies all of the mappings from the specified map to this cache. The
- * effect of this call is equivalent to that of calling
- * {@link #save(Object,Object) save(key, value)} on this cache once for each
- * mapping from key to value in the specified map.
- *
- * @param values
- * mappings to be stored in this map
- * @throws IllegalArgumentException
- * if one key or object is null
- */
- public void saveAll(Map values) throws IllegalArgumentException;
- /**
- * Returns the number of objects stored in this cache. If the cache contains
- * no data, returns zero.
- *
- * @return the number of objects stored in a cache; returns zero if the
- * cache contains no data
- */
- public int size();
- /**
- * Returns a collection of the keys contained in this cache.
- *
- * @return keys in this cache
- */
- public Collection keys();
- }
- package com.alex.commonutil.cache;
- /**
- */
- public class CacheIncomingDataValidator {
- /**
- * Check whether an object is a valid instance of a specified Class.
- *
- * @param obj the object going to be validated
- * @param clazz the specified Class
- *
- * @return true or false
- */
- public static boolean isValidObject(Class clazz, Object obj){
- if(null == obj || null == clazz){
- return false;
- }
- return clazz == obj.getClass();
- }
- }
- package com.alex.commonutil.cache;
- /**
- * This class consists exclusively of static methods that operate on or return
- * caches.
- */
- public class Caches {
- /**
- * If the key or value is null, throws an <tt>IllegalArgumentException</tt>.
- *
- * @param object
- * the key or value may store in a cache
- * @throws IllegalArgumentException
- * if the key or value is null
- */
- public static void verifyKeyValue(Object object)
- throws IllegalArgumentException {
- if (object == null) {
- throw new IllegalArgumentException(
- "Cache does not allow null key or values.");
- }
- }
- /**
- * If the identifier of a cache is null, this method will throw an
- * <tt>IllegalArgumentException</tt>.
- *
- * @param identifier
- * the identifier of a cache
- * @throws IllegalArgumentException
- * if the identifier is null
- */
- public static void verifyIdentifier(Object identifier)
- throws IllegalArgumentException {
- if (identifier == null) {
- throw new IllegalArgumentException(
- "The identifier of a cache should not be null");
- }
- }
- }
- package com.alex.commonutil.cache;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /**
- * The cache service manages some caches, and providing a unified interface for
- * access them. Caches managed by the service can be located by the identifiers.
- * <p>
- * Since caches do not accept <tt>null</tt> keys or values, there are many
- * validations in those methods.
- * </p>
- */
- public abstract class CacheService {
- /**
- * Add a cache into the service's management. The cache must provide an
- * available identifier.
- *
- * @param cache
- * a cache to be managed
- * @throws IllegalArgumentException
- * if the cache does not provide an available identifier
- */
- public abstract void addCache(Cache cache) throws IllegalArgumentException;
- /**
- * Removes a cache of specified identifier out of the management. If the
- * cache is not under control, this method has no effect.
- *
- * @param identifier
- * the identifier of a cache
- */
- public abstract void removeCache(Object identifier);
- /**
- * Clear contents of one cache associated with speicified identifier. If the
- * cache does not exists or the identifier is <tt>null</tt>, this method
- * has no effect.
- *
- * @param identifier
- * the identifier of a cache
- */
- public void clear(Object identifier) {
- if (hasCache(identifier)) {
- getCache(identifier).clear();
- }
- }
- /**
- * Clears contents of all the caches.
- */
- public void clearAll() {
- for (Iterator it = getCaches().iterator(); it.hasNext();) {
- Cache cache = (Cache) it.next();
- cache.clear();
- }
- }
- /**
- * Determines in which cache the given object will be put. By the cache
- * identifier returned by this method, it would be easy to locate the cache.
- *
- * @param object
- * the object to be cached
- * @return the identifier of a cache in which the object will be put
- */
- public abstract Object getCacheIdentifier(Object object);
- /**
- * Get a collection of all the caches managed by this service.
- *
- * @return an collection of caches managed by the service
- */
- public abstract Collection getCaches();
- /**
- * Returns true if the cache associated with specified identifier has a
- * mapping to the specified object.
- * <p>
- * If the given object is null, always returns false, because cache do not
- * allow <tt>null</tt> keys or values.
- * </p>
- *
- * @param identifier
- * the identifier of a cache
- * @param object
- * key whose presence in the cache is to be tested
- * @param asKey
- * whether take the object as key
- * @return true if the cache associated with specified identifier has one or
- * more mapping to the specified key
- */
- public boolean contains(Object identifier, Object object, boolean asKey) {
- if (identifier == null || object == null) {
- return false;
- }
- if (!hasCache(identifier)) {
- return false;
- }
- Cache cache = getCache(identifier);
- return (asKey == true) ? cache.hasKey(object) : cache.hasObject(object);
- }
- /**
- * Returns true if the cache associated with specified identifier has a
- * mapping to the specified key.
- * <p>
- * If the key is <tt>null</tt>, always returns false, because cache do
- * not allow <tt>null</tt> keys or values.
- * </p>
- *
- * @param identifier
- * the identifier of a cache
- * @param object
- * key whose presence in the cache is to be tested
- * @return true if the cache associated with specified identifier has a
- * mapping to the specified key
- */
- public boolean contains(Object identifier, Object object) {
- return contains(identifier, object, false);
- }
- /**
- * Returns the number of caches managed by the service.
- *
- * @return count of caches
- */
- public abstract int countOfCaches();
- /**
- * Returns the value to which maps the specified key in cache. Returns
- * <tt>null</tt> if the cache contains no mapping for this key.
- * <p>
- * If this method is not synchronized, the performance might be higher, and
- * would not brings chaos when accessed by more than one thread.
- * </p>
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * the key whose associated value is to be returned
- * @return the value to which the cache maps the specified key, or
- * <tt>null</tt> if the cacache contains no mapping for this key.
- */
- public Object fetch(Object identifier, Object key) {
- return getCache(identifier).fetch(key);
- }
- /**
- * Returns all the objects stored in a cache associated with specified
- * identifier.
- * <p>
- * This method is not needed to be synchronized.
- * </p>
- *
- * @param identifier
- * the identifier of a cache
- * @return a <code>Collection</code> of objects stored in the cache who is
- * associated with specified identifier
- * @see #fetch(Object, Object)
- */
- public Collection fetchAll(Object identifier) {
- if (!hasCache(identifier)) {
- return null;
- }
- return getCache(identifier).fetchAll();
- }
- /**
- * Locates a cache by the its identifier. If the cache does not exist,
- * returns <tt>null</tt>.
- *
- * @param identifier
- * the identifier of the cache
- * @return a cache located by the identifier
- */
- public abstract Cache getCache(Object identifier);
- /**
- * Returns <tt>true</tt> if a cache is under management of the service,
- * and it can be located by the identifier. If the cache cannot be located
- * by the identifier, this method returns <tt>false</tt>. If the
- * identifier is <tt>null</tt>, this method always returns false.
- *
- * @param identifier
- * the identifier of the cache
- * @return <tt>true</tt> if the cache can be located by the identifier
- */
- public abstract boolean hasCache(Object identifier);
- /**
- * Removes the mapping for this key from a cache if it is present. If the
- * key is <tt>null</tt>, this method has no effect on cache.
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * mapping key in the cache which is to be removed
- */
- public void remove(Object identifier, Object key) {
- if (!hasCache(identifier) || key == null) {
- return;
- }
- getCache(identifier).remove(key);
- }
- /**
- * Save an object to corresponding cache. The implementation must specify a
- * cache by {@link #getCacheIdentifier(Object)}, and provide an available
- * key for the association in the cache.
- *
- * @param object
- * the object to save
- * @throws IllegalArgumentException
- * if the object is <tt>null</tt>
- * @see Cache#createKey(Object)
- * @see #getCacheIdentifier(Object)
- */
- public void save(Object object) throws IllegalArgumentException {
- Caches.verifyKeyValue(object);
- Object identifier = getCacheIdentifier(object);
- Object key = getCache(identifier).createKey(object);
- save(identifier, key, object);
- }
- /**
- * Save the object to the cache with an associated key. The implementation
- * must specify a cache.
- * <p>
- * This method does not accepts <tt>null</tt> values. And it should be
- * <tt>synchronized</tt> in enviroment of multi-thread.
- * </p>
- *
- * @param key
- * key with which the specified object is to be associated.
- * @param value
- * object to be stored in the cache, and associated with the
- * specified key
- * @throws IllegalArgumentException
- * if the key or value is <tt>null</tt>
- * @see #save(Object, Object, Object)
- * @see Cache#createKey(Object)
- */
- public void save(Object key, Object value) throws IllegalArgumentException {
- Caches.verifyKeyValue(key);
- Caches.verifyKeyValue(value);
- Object identifier = getCacheIdentifier(value);
- save(identifier, key, value);
- }
- /**
- * Store the specified object with the specified key in a cache associated
- * with specified identifier. If the cache previously contained a mapping
- * for this key, the old value is replaced by the specified value.
- * <p>
- * This is a default implementation. If the cache cannot located by the
- * identifier, it would not be created. The sub classes may want to create a
- * new cache when necessary.
- * </p>
- * <p>
- * Note that this method do not accept <tt>null</tt> key or <tt>null</tt>
- * value. And this method should be synchronized when possibly accessed by
- * more than one thread.
- * </p>
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * key with which the specified object is to be associated.
- * @param value
- * object to be stored in the cache, and associated with the
- * specified key
- * @throws IllegalArgumentException
- * if the identifier, key or value is <tt>null</tt>
- */
- public void save(final Object identifier, final Object key,
- final Object value) throws IllegalArgumentException {
- /*
- * Make sure the identifier, key and value is not null
- */
- Caches.verifyIdentifier(identifier);
- Caches.verifyKeyValue(key);
- Caches.verifyKeyValue(value);
- getCache(identifier).save(key, value);
- }
- /**
- * Returns the number of objects stored in a cache. If the cache does not
- * exist or the given identifier is <tt>null</tt>, always return zero.
- *
- * @param identifier
- * the identifier of a cache
- * @return number of objects stored in a cache; or zero if the identifier is
- * <tt>null</tt> or the cache does not exist
- */
- public int sizeOfCache(Object identifier) {
- if (!hasCache(identifier)) {
- return 0;
- }
- return getCache(identifier).size();
- }
- /**
- * Save all of the elements in the specified collection to a cache specified
- * by the identifier.
- *
- * @param identifier
- * the identifier of a cache
- * @param objects
- * objects to be saved into a cache
- * @throws IllegalArgumentException
- * if the identifier, one or more objects in collection are
- * <tt>null</tt>
- */
- public void saveAll(Object identifier, Collection objects)
- throws IllegalArgumentException {
- Caches.verifyIdentifier(identifier);
- getCache(identifier).saveAll(objects);
- }
- /**
- * Copies all of the mappings from the specified map to a cache speicified
- * by the identifier.
- *
- * @param identifier
- * the identifier of a cache
- * @param objects
- * objects to be saved into a cache
- * @throws IllegalArgumentException
- * if the identifier, one key or object in mapping is
- * <tt>null</tt>
- */
- public void saveAll(Object identifier, Map objects)
- throws IllegalArgumentException {
- Caches.verifyIdentifier(identifier);
- getCache(identifier).saveAll(objects);
- }
- /**
- * Save all of the elements in the specified collection to a cache. Before
- * saving these objects, please ensure that the corresponding cache has
- * provided available keys.
- *
- * @param objects
- * objects to be saved into a cache
- * @throws IllegalArgumentException
- * if one object is <tt>null</tt>
- * @see #save(Object)
- * @see Cache#createKey(Object)
- */
- public void saveAll(Collection objects) throws IllegalArgumentException {
- for (Iterator it = objects.iterator(); it.hasNext();) {
- save(it.next());
- }
- }
- /**
- * Copies all of the mappings from the specified map to a cache. The effect
- * of this call is equivalent to that of calling
- * {@link #save(Object,Object) save(key, value)} on this cache once for each
- * mapping from key to value in the specified map.
- *
- * @param objects
- * mappings to be stored in this map
- * @see #save(Object, Object)
- * @throws IllegalArgumentException
- * if one key or object is <tt>null</tt>
- */
- public void saveAll(Map objects) throws IllegalArgumentException {
- Set keys = objects.keySet();
- for (Iterator it = keys.iterator(); it.hasNext();) {
- Object k = (Object) it.next();
- save(k, objects.get(k));
- }
- }
- }
- package com.alex.commonutil.cache;
- import java.util.Collection;
- import java.util.HashMap;
- public class ConcreteCacheService extends CacheService {
- /**
- * A lock used when creating cache. If a cache does not exist, this lock can
- * make sure that it will be created only once. And this
- * <tt>ReentrantLock</tt> can also provide better performance than the
- * <tt>synchronized</tt> key word.
- */
- private Object lock = new Object();
- /**
- * All the caches managed by the service.
- */
- private HashMap caches = new HashMap();
- public synchronized void addCache(Cache cache) throws IllegalArgumentException {
- if (cache == null) {
- throw new IllegalArgumentException("The cache is null.");
- }
- Object identifier = cache.getIdentifier();
- Caches.verifyIdentifier(identifier);
- caches.put(identifier, cache);
- }
- public synchronized int countOfCaches() {
- return caches.size();
- }
- /**
- * Locates a cache by the class of objects stored in it.
- *
- * @param identifier
- * the identifier of the cache
- * @return a cache located by the class
- */
- public synchronized Cache getCache(Object identifier) {
- return (Cache)caches.get(identifier);
- }
- public synchronized boolean hasCache(Object identifier) {
- if (!(identifier instanceof Class)) {
- return false;
- }
- return caches.containsKey(identifier);
- }
- /**
- * Store the specified object with the specified key in a cache associated
- * with specified identifier. If the cache cannot located by the identifier,
- * a new cache associated with the identifier will be created.
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * key with which the specified object is to be associated.
- * @param value
- * object to be stored in the cache, and associated with the
- * specified key
- * @throws IllegalArgumentException
- * if the identifier, key or value is <tt>null</tt>
- */
- public void save(final Object identifier, final Object key,
- final Object value) throws IllegalArgumentException {
- Caches.verifyIdentifier(identifier);
- Caches.verifyKeyValue(key);
- Caches.verifyKeyValue(value);
- /*
- * If a cache of certain class does not exist, a new cache of this class
- * will be created. The lock can make sure that the cache will be
- * created only once.
- */
- if (!hasCache(identifier)) {
- synchronized (lock) {
- if (!hasCache(identifier)) {
- Cache cache = new AbstractCache(identifier) {
- public Object createKey(Object object) {
- return key;
- }
- };
- addCache(cache);
- cache.save(key, value);
- } else {
- getCache(identifier).save(key, value);
- }
- }
- } else {
- getCache(identifier).save(key, value);
- }
- }
- public synchronized Collection getCaches() {
- return caches.values();
- }
- public synchronized void removeCache(Object identifier) {
- caches.remove(identifier);
- }
- public Object getCacheIdentifier(Object object) {
- // if (object instanceof AbbrTradeLog) {
- // AbbrTradeLog a = (AbbrTradeLog) object;
- // return ((BBCacheServiceManager.TRADERID).equals(a.getCodeType())) ? BBCacheServiceManager.USERIDCACHE
- // : AbbrTradeLog.class;
- // }
- return object.getClass();
- }
- }
- package com.alex.commonutil.cache;
- import java.util.Collection;
- /**
- * A persistable cache means it has a persisent storage as its backup, such as a
- * database, file system or some other devices.
- * <p>
- * A cache may need a persistent storage as its backup, because cache can not
- * hold data permanently. If power is off, the server crashes, or the cache
- * crashes, all the data in caches will be lost. It is much better that each new
- * added object can be persisted. When cache crashes, it can easily recover data
- * from persistent storage. And this makes data in a cache much safer than those
- * in caches without persistent storage.
- * </p>
- */
- public interface PersistableCache extends Cache {
- /**
- * Persist contents in this cache to certain storage.
- *
- * @throws Exception
- * if an error occured
- */
- public void persist() throws Exception;
- /**
- * Persist one object to which maps the specified key in this cache to
- * certain storage.
- *
- * @param key
- * key with which the specified object is to be associated in
- * this cache.
- * @throws Exception
- * if an error occured
- */
- public void persist(Object key) throws Exception;
- /**
- * Synchronizes contents of the cache with its persistence.
- *
- * @see #load()
- * @see #persist()
- * @throws Exception
- * if an error occured
- */
- public void synchronize() throws Exception;
- /**
- * Synchronizes contents of the cache associated with speicified key with
- * its persistence.
- *
- * @see #load(Object)
- * @see #persist(Object)
- * @throws Exception
- * if an error occured
- */
- public void synchronize(Object key) throws Exception;
- /**
- * Load all the data from persistence to cache; returns all the loaded
- * objects.
- *
- * @return all the loaded objects
- * @throws Exception
- * if an error occured
- */
- public Collection load() throws Exception;
- /**
- * Load object associated with given key to the cache; and retusn the loaded
- * object.
- *
- * @param key
- * the key whose associated object is to be loaded
- * @return the object associated with the key
- * @throws Exception
- * if an error occured
- */
- public Object load(Object key) throws Exception;
- }
- package com.alex.commonutil.cache;
- import java.util.Collection;
- import java.util.Iterator;
- /**
- * This service manages persistable caches. A persistable cache means it has a
- * persisent storage as its backup, such as a database, file system or some
- * other devices.
- */
- public abstract class PersistableCacheService extends CacheService {
- /**
- * Persist contents in a cache associated with specified identifier to
- * certain storage.
- *
- * @param identifier
- * the identifier of a cache
- * @throws Exception
- * if an error occured
- */
- public void persist(Object identifier) throws Exception {
- if (hasCache(identifier)) {
- ((PersistableCache)getCache(identifier)).persist();
- }
- }
- /**
- * Persist one object to which maps the specified key in cache associated
- * with specified identifier to certain storage.
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * key with which the specified object is to be associated in a
- * cache.
- * @throws Exception
- * if an error occured
- */
- public void persist(Object identifier, Object key) throws Exception {
- Caches.verifyKeyValue(key);
- if (hasCache(identifier)) {
- ((PersistableCache)getCache(identifier)).persist(key);
- }
- }
- /**
- * Persists all data in all caches to certain storage.
- *
- * @throws Exception
- * if an error occured
- */
- public void persistAll() throws Exception {
- for (Iterator it = getCaches().iterator(); it.hasNext();){
- PersistableCache cache = (PersistableCache) it;
- cache.persist();
- }
- }
- /**
- * Synchronizes contents of the cache with its persistent storage.
- *
- * @param identifier
- * the identifier of a cache
- * @throws Exception
- * if an error occured
- */
- public void synchronize(Object identifier) throws Exception {
- if (hasCache(identifier)) {
- ((PersistableCache)getCache(identifier)).synchronize();
- }
- }
- /**
- * Synchronizes content of specified key in the cache with its persistent
- * storage.
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * the key with which the specified object is to be associated in
- * a cache. *
- * @throws Exception
- * if an error occured
- */
- public void synchronize(Object identifier, Object key) throws Exception {
- Caches.verifyKeyValue(key);
- if (hasCache(identifier)) {
- ((PersistableCache)getCache(identifier)).synchronize(key);
- }
- }
- /**
- * Synchronizes contents of the all caches with their persistent storages.
- *
- * @throws Exception
- * if an error occured
- */
- public void synchronizeAll() throws Exception {
- // for (PersistableCache cache : getCaches()) {
- // cache.synchronize();
- // }
- for (Iterator it = getCaches().iterator(); it.hasNext();){
- PersistableCache cache = (PersistableCache) it;
- cache.synchronize();
- }
- }
- /**
- * Load all the data from persistence to all caches managed by the service;
- * returns all the loaded objects.
- *
- * @return all the loaded objects
- * @throws Exception
- * if an error occured
- */
- public void loadAll() throws Exception {
- // for (PersistableCache cache : getCaches()) {
- // cache.load();
- // }
- for (Iterator it = getCaches().iterator(); it.hasNext();){
- PersistableCache cache = (PersistableCache) it;
- cache.load();
- }
- }
- /**
- * Load objects in persistent storage for a cache which can be located by
- * the identifier; and retusn the loaded objects.
- *
- * @param identifier
- * the identifier of a cache
- * @return the objects for the cache in persist storage
- * @throws Exception
- * if an error occured
- */
- public Collection load(Object identifier) throws Exception {
- return (hasCache(identifier)) ? ((PersistableCache)getCache(identifier)).load() : null;
- }
- /**
- * Load objects associated with given key to a cache which can be located by
- * the identifier; and retusn the loaded object.
- *
- * @param identifier
- * the identifier of a cache
- * @param key
- * the key whose associated object is to be loaded
- * @return the object associated with the key
- * @throws Exception
- * if an error occured
- */
- public Object load(Object identifier, Object key) throws Exception {
- Caches.verifyKeyValue(key);
- return (hasCache(identifier)) ? ((PersistableCache)getCache(identifier)).load(key) : null;
- }
- public abstract Cache getCache(Object identifier);
- public abstract Collection getCaches();
- }
- package com.alex.commonutil.cache.test;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import com.alex.commonutil.cache.AbstractCache;
- import com.alex.commonutil.cache.Cache;
- import com.alex.commonutil.cache.CacheService;
- import com.alex.commonutil.cache.ConcreteCacheService;
- public class CacheTest {
- public static String CACHE_A = "CACHE_A";
- public static String CACHE_B = "CACHE_B";
- public static void main(String[] args) {
- CacheService cs = new ConcreteCacheService();
- Cache cacheA = new AbstractCache(CACHE_A) {
- public Object createKey(Object object)
- throws IllegalArgumentException {
- String str = (String) object;
- return str;// tradeNumber as key
- }
- };
- cs.addCache(cacheA);
- List listTest = new ArrayList();
- listTest.add("AAA");
- listTest.add("BBB");
- cacheA.save("listTest", listTest);
- Cache cacheB = new AbstractCache(CACHE_B) {
- public Object createKey(Object object)
- throws IllegalArgumentException {
- String str = (String) object;
- return str;// tradeNumber as key
- }
- };
- cs.addCache(cacheB);
- Cache cacheAA = cs.getCache(CACHE_A);
- List list=(List)cacheAA.fetch("listTest");
- Iterator itr = list.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
- package com.alex.commonutil.cache.test;
- import java.io.Serializable;
- import org.apache.commons.lang.builder.EqualsBuilder;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- import org.apache.commons.lang.builder.ToStringBuilder;
- public class ValueObject implements Serializable {
- private static final long serialVersionUID = 2118933350226194725L;
- public java.lang.String getField1() {
- return field1;
- }
- public void setField1(java.lang.String field1) {
- this.field1 = field1;
- }
- public java.lang.String getField2() {
- return field2;
- }
- public void setField2(java.lang.String field2) {
- this.field2 = field2;
- }
- // fields
- private java.lang.String field1;
- private java.lang.String field2;
- public String toString() {
- return new ToStringBuilder(this).toString();
- }
- public int hashCode() {
- return new HashCodeBuilder().append(this.getField1()).append(
- this.getField1()).toHashCode();
- }
- public boolean equals(Object obj) {
- if (obj == null)
- return false;
- if (!(obj instanceof ValueObject))
- return false;
- if (this == obj)
- return true;
- ValueObject other = (ValueObject) obj;
- return new EqualsBuilder()
- .append(this.getField1(), other.getField1()).append(
- this.getField2(), other.getField2()).isEquals();
- }
- }