com.alachisoft.ncache.jsr107.NCacheCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.jsr107;
import com.alachisoft.ncache.client.CacheEventDescriptor;
import com.alachisoft.ncache.client.CacheItem;
import com.alachisoft.ncache.runtime.caching.expiration.Expiration;
import com.alachisoft.ncache.runtime.caching.expiration.ExpirationType;
import com.alachisoft.ncache.runtime.CacheItemPriority;
import com.alachisoft.ncache.runtime.JSON.JsonValueBase;
import com.alachisoft.ncache.runtime.dependencies.CacheDependency;
import com.alachisoft.ncache.runtime.events.EventDataFilter;
import com.alachisoft.ncache.runtime.events.EventType;
import com.alachisoft.ncache.runtime.util.TimeSpan;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.Factory;
import javax.cache.event.CacheEntryListener;
import javax.cache.expiry.Duration;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheLoaderException;
import javax.cache.integration.CacheWriter;
import javax.cache.integration.CompletionListener;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.EntryProcessorResult;
import javax.cache.processor.MutableEntry;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
//import com.alachisoft.tayzgrid.web.caching.Cache;
public class NCacheCache implements Cache {
private static final Object NOT_THERE = new Object();
protected final NCacheManager cacheManager;
protected final com.alachisoft.ncache.client.Cache ncache;
private final NCacheConfiguration cfg;
private CacheLoader cacheLoader = null;
private CacheWriter cacheWriter = null;
private boolean closed = false;
private CacheEventDescriptor addEventDescriptor = null;
private CacheEventDescriptor deleteEventDescriptor = null;
private CacheEventDescriptor updateEventDescriptor = null;
private boolean isExpireConfigure = false;
/**
* @param cacheManager
* @param cfg
* @param ncache
*/
public NCacheCache(final NCacheManager cacheManager, final NCacheConfiguration cfg, final com.alachisoft.ncache.client.Cache ncache) {
if (ncache == null) {
throw new NullPointerException();
}
this.cacheManager = cacheManager;
this.cfg = cfg;
this.ncache = ncache;
final Iterable> cacheEntryListenerConfigurations = cfg.getInitialCacheEntryListenerConfigurations();
if (cacheEntryListenerConfigurations != null) {
for (CacheEntryListenerConfiguration listenerCfg : cacheEntryListenerConfigurations) {
registerCacheEntryListener(listenerCfg);
}
}
}
/**
* get value against key
*
* @param key - the key whose associated value is to be returned
* @return value
* @throws NullPointerException if key is null
*/
@Override
public V get(K key) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key cannot be null");
}
Object value = null;
String cacheKey = getNCacheKey(key);
try {
value = ncache.get(cacheKey, Object.class);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
return getValue(value);
}
/**
* get map of key value against keys set
*
* @param keys - the keys whose associated values is to be returned
* @return
* @throws NullPointerException if any key is null
*/
@Override
public Map getAll(Set extends K> keys) {
checkNCacheStatus();
if (keys == null)
throw getNullPointerException("key");
ArrayList cacheKeys = new ArrayList();
cacheKeys = getCacheKeys(keys);
Map result = null;
try {
result = (Map) ncache.getBulk(cacheKeys, null);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
return result;
}
/**
* check key exist in cache
*
* @param key
* @return
* @throws NullPointerException if key is null
*/
@Override
public boolean containsKey(K key) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
String cacheKey = getNCacheKey(key);
try {
return ncache.contains(cacheKey);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* load key from data source
*
* @param keys - the keys to load
* @param replaceExistingValues - when true existing values in the Cache will be replaced by those loaded from a CacheLoader
* @param completionListener - the CompletionListener (may be null)
* @throws NullPointerException if key is null
*/
@Override
public void loadAll(final Set extends K> keys, final boolean replaceExistingValues, final CompletionListener completionListener) {
checkNCacheStatus();
if (keys == null)
throw getNullPointerException("keys");
if (cacheLoader == null) {
if (completionListener != null)
completionListener.onCompletion();
return;
}
cacheManager.getExecutorService().submit(new Callable() {
@Override
public Void call() throws Exception {
for (String key : getCacheKeys(keys)) {
try {
V value = null;
if (ncache.contains(key)) {
if (replaceExistingValues)
value = (V) ncache.get(key, null);
//cacheLoader.load(k);
} else {
value = (V) ncache.get(key, null);
}
if (value != null && getExpiratonType() == 3)
insert((K) key, value);
} catch (Exception e) {
if (completionListener != null) {
completionListener.onException(new CacheLoaderException(e));
}
return null;
}
}
if (completionListener != null) {
completionListener.onCompletion();
}
return null;
}
});
}
/**
* Associates the specified value with the specified key in the cache.
*
* @param key - key with which the specified value is to be associated
* @param value - value to be associated with the specified key
* @throws NullPointerException if key is null
*/
@Override
public void put(K key, V value) {
put(key, value, null, null);
}
public void put(K key, V value, CacheDependency depdenency, CacheItemPriority priority) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
if (value == null) {
throw getNullPointerException("Value");
}
String cacheKey = getNCacheKey(key);
try {
CacheItem cacheItem = new CacheItem(value);
if (priority != null)
cacheItem.setCacheItemPriority(priority);
if (depdenency != null) {
cacheItem.setDependency(depdenency);
}
if (getExpiratonType() == 1 || getExpiratonType() == 4) {
Expiration expiration = new Expiration(ExpirationType.Sliding, getSlidingExpiration());
cacheItem.setExpiration(expiration);
}
if (getExpiratonType() == 2) {
Expiration expiration = new Expiration(ExpirationType.Absolute, getSlidingExpiration());
cacheItem.setExpiration(expiration);
}
if (getExpiratonType() == 3) {
insert(key, value);
return;
}
ncache.insert(cacheKey, cacheItem);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* Associates the specified value with the specified key in this cache, returning an existing value if one existed.
*
* @param key - key with which the specified value is to be associated
* @param valueToPut - value to be associated with the specified key
* @return
* @throws NullPointerException if key or valueToPut is null
*/
@Override
public V getAndPut(K key, V valueToPut) {
checkNCacheStatus();
if (key == null)
throw getNullPointerException("key");
if (valueToPut == null)
throw getNullPointerException("value");
Object value = null;
//String cacheKey = getTayzGridKey(key);
try {
value = get(key);
if (getExpiratonType() == 3)
insert(key, valueToPut);
else
this.put(key, valueToPut);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
return getValue(value);
}
/**
* Copies all of the entries from the specified map to the Cache.
*
* @param map - mappings to be stored in this cache
* @throws NullPointerException if map is null
*/
@Override
public void putAll(Map extends K, ? extends V> map) {
checkNCacheStatus();
if (map == null)
throw getNullPointerException("map");
String[] keys = new String[map.size()];
CacheItem[] cacheItems = new CacheItem[map.size()];
int index = -1;
for (Map.Entry extends K, ? extends V> entry : map.entrySet()) {
if (entry.getValue() == null) {
throw getNullPointerException("value");
}
if (entry.getKey() == null) {
throw getNullPointerException("key");
}
index++;
keys[index] = getNCacheKey(entry.getKey());
cacheItems[index] = new CacheItem(entry.getValue());
if (getExpiratonType() == 1 || getExpiratonType() == 4) {
Expiration expiration = new Expiration(ExpirationType.Sliding, getSlidingExpiration());
cacheItems[index].setExpiration(expiration);
}
if (getExpiratonType() == 2) {
Expiration expiration = new Expiration(ExpirationType.Sliding, getSlidingExpiration());
cacheItems[index].setExpiration(expiration);
}
if (getExpiratonType() == 3) {
if (this.containsKey(entry.getKey())) {
try {
Expiration expiration = new Expiration(ExpirationType.Sliding, getSlidingExpiration());
cacheItems[index].setExpiration(getAccessExpiryDuration(entry.getKey(), ncache.getCacheItem(keys[index])));
} catch (Exception ex) {
}
} else {
cacheItems[index].setExpiration(new Expiration(ExpirationType.Sliding, getSlidingExpiration()));
}
}
}
try {
Map inputMap = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
inputMap.put(keys[i], cacheItems[i]); // is there a clearer way?
}
ncache.insertBulk(inputMap);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* Atomically associates the specified key with the given value if it is not already associated with a value.
*
* @param key - key with which the specified value is to be associated
* @param value - value to be associated with the specified key
* @return
* @throws NullPointerException if key or value is null
*/
@Override
public boolean putIfAbsent(K key, V value) {
checkNCacheStatus();
if (!containsKey(key)) {
if (getExpiratonType() == 3 || getExpiratonType() == 4) {
insert(key, value);
} else {
put(key, value);
}
return true;
} else
return false;
}
/**
* Removes the mapping for a key from this cache if it is present.
*
* @param key - key whose mapping is to be removed from the cache
* @return
* @throws NullPointerException if key is null
*/
@Override
public boolean remove(K key) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
String cacheKey = getNCacheKey(key);
try {
Object obj = ncache.remove(cacheKey,null);
return obj != null;
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* @param key - key whose mapping is to be removed from the cache
* @param value - value expected to be associated with the specified key
* @return
* @throws NullPointerException if key or value is null
*/
@Override
public boolean remove(K key, V value) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
if (value == null) {
throw getNullPointerException("value");
}
V old = get(key);
if (old != null) {
if (containsKey(key) && old.equals(value)) {
remove(key);
return true;
}
if (getExpiratonType() == 4) {
insert(key, get(key));
}
}
return false;
}
/**
* Atomically removes the mapping for a key only if currently mapped to the given value.
*
* @param key - key with which the specified value is associated
* @return
* @throws NullPointerException if key is null
*/
@Override
public V getAndRemove(K key) {
checkNCacheStatus();
if (key == null)
throw getNullPointerException("key");
String cacheKey = getNCacheKey(key);
try {
return getValue(ncache.remove(cacheKey,null));
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* Atomically replaces the entry for a key only if currently mapped to a given value.
*
* @param key - key with which the specified value is associated
* @param oldValue - value expected to be associated with the specified key
* @param newValue - value to be associated with the specified key
* @return
* @throws NullPointerException if key, oldValue or newValue is null
*/
@Override
public boolean replace(K key, V oldValue, V newValue) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
if (oldValue == null) {
throw getNullPointerException("oldValue");
}
if (newValue == null) {
throw new NullPointerException("newValue");
}
V value = this.get(key);
if (value != null) {
if (value.equals(oldValue)) {
this.put(key, newValue);
return true;
}
}
return false;
}
/**
* Atomically replaces the entry for a key only if currently mapped to some value.
*
* @param key - key with which the specified value is associated
* @param value - value expected to be associated with the specified key
* @return
* @throws NullPointerException if key or value is null
*/
@Override
public boolean replace(K key, V value) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
if (value == null) {
throw getNullPointerException("value");
}
if (this.containsKey(key)) {
if (getExpiratonType() == 3) {
insert(key, value);
} else {
this.put(key, value);
}
return true;
}
return false;
}
/**
* Atomically replaces the value for a given key if and only if there is a value currently mapped by the key.
*
* @param key - key with which the specified value is associated
* @param value - value to be associated with the specified key
* @return
* @throws NullPointerException if key or value is null
*/
@Override
public V getAndReplace(K key, V value) {
checkNCacheStatus();
if (key == null)
throw getNullPointerException("key");
if (value == null)
throw getNullPointerException("value");
V existingValue = this.get(key);
if (existingValue != null) {
if (getExpiratonType() == 3) {
insert(key, value);
} else {
this.put(key, value);
}
}
return existingValue;
}
/**
* Removes entries for the specified keys.
*
* @param keys - the keys to remove
* @throws NullPointerException if any key is null
*/
@Override
public void removeAll(Set extends K> keys) {
checkNCacheStatus();
if (keys == null)
throw getNullPointerException("keys");
ArrayList cacheKeys = new ArrayList();
int index = -1;
for (K key : keys) {
if (key == null) {
throw new NullPointerException();
}
cacheKeys.add(getNCacheKey(key));
}
try {
ncache.deleteBulk(cacheKeys);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* Removes all of the mappings from this cache.
*/
@Override
public void removeAll() {
checkNCacheStatus();
try {
ncache.clear();
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* Clears the contents of the cache, without notifying listeners or CacheWriters.
*/
@Override
public void clear() {
checkNCacheStatus();
try {
ncache.clear();
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
/**
* Provides a standard way to access the configuration of a cache using JCache configuration or additional proprietary configuration.
*
* @param type - the configuration interface or class to return. This includes Configuration.class and CompleteConfigurations.
* @return
*/
@Override
public > C getConfiguration(Class type) {
checkNCacheStatus();
if (type.isAssignableFrom(cfg.getClass())) {
return type.cast(cfg);
}
return null;
}
/**
* Invokes an EntryProcessor against the Cache.Entry specified by the provided key
*
* @param key - the key to the entry
* @param entryProcessor - the EntryProcessor to invoke
* @param arguments - additional arguments to pass to the EntryProcessor
* @return
* @throws EntryProcessorException
* @throws NullPointerException if key is null
*/
@Override
public T invoke(K key, EntryProcessor entryProcessor, Object... arguments) throws EntryProcessorException {
checkNCacheStatus();
if (key == null)
throw getNullPointerException("key");
if (entryProcessor == null)
throw getNullPointerException("EntryProcessor");
T finaloutcome;
try {
V value = this.get(key);
boolean fromLoader = false;
if (cfg.isReadThrough()) {
value = this.get(key);
fromLoader = true;
}
final TayzGridMutableEntry entry = new TayzGridMutableEntry(this, value, key, fromLoader);
finaloutcome = entryProcessor.process(entry, arguments);
entry.apply(this);
} catch (EntryProcessorException t) {
if (t instanceof CacheException) {
throw t;
}
throw new EntryProcessorException(t);
}
return finaloutcome;
}
/**
* Invokes an EntryProcessor against the set of Cache.Entrys specified by the set of keys.
*
* @param set - the set of keys for entries to process
* @param entryProcessor - the EntryProcessor to invoke
* @param arguments - additional arguments to pass to the EntryProcessor
* @return
* @throws NullPointerException if EntryProcessorResult is null
*/
@Override
public Map> invokeAll(Set extends K> set, EntryProcessor entryProcessor, Object... arguments) {
checkNCacheStatus();
if (entryProcessor == null)
throw new NullPointerException();
final Map> results = new HashMap>();
for (K key : set) {
final T result = invoke(key, entryProcessor, arguments);
if (result != null) {
results.put(key, new EntryProcessorResult() {
@Override
public T get() throws EntryProcessorException {
return result;
}
});
}
}
return results;
}
/**
* Return the name of the cache.
*
* @return
*/
@Override
public String getName() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
checkNCacheStatus();
return ncache.toString();
}
/**
* Gets the CacheManager that owns and manages the Cache.
*
* @return
*/
@Override
public CacheManager getCacheManager() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
checkNCacheStatus();
return cacheManager;
}
/**
* Closing a Cache signals to the CacheManager that produced or owns the Cache that it should no longer be managed.
*/
@Override
public void close() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//checkTayzGridStatus();
if (!isClosed()) {
try {
cacheManager.destroyCache(ncache.toString());
//ncache.dispose();
closed = true;
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
}
void dispose() {
checkNCacheStatus();
cacheManager.destroyCache(this.getName());
closed = true;
}
/**
* Determines whether this Cache instance has been closed.
*
* @return
*/
@Override
public boolean isClosed() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return closed;
}
/**
* Provides a standard way to access the underlying concrete caching implementation to provide access to further, proprietary features.
*
* @param type - the proprietary class or interface of the underlying concrete cache. It is this type that is returned.
* @return
*/
@Override
public T unwrap(Class type) {
//To change body of generated methods, choose Tools | Templates.
checkNCacheStatus();
if (type.isAssignableFrom(ncache.getClass()))
return type.cast(ncache);
if (type.isAssignableFrom(getClass()))
return type.cast(this);
return null;
}
/**
* Registers a CacheEntryListener.
*
* @param cacheEntryListenerConfiguration - a factory and related configuration for creating the listener
*/
@Override
public final void registerCacheEntryListener(CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/**
* Deregisters a listener, using the CacheEntryListenerConfiguration that was used to register it.
*
* @param cacheEntryListenerConfiguration - the factory and related configuration that was used to create the listener
*/
@Override
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/**
* @return
*/
@Override
public Iterator> iterator() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
checkNCacheStatus();
try {
return new NCacheEntryIterator(this);
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
void shutdown() {
try {
closed = true;
ncache.close();
} catch (Exception ex) {
}
}
private void checkNCacheStatus() {
if (closed) {
throw new IllegalStateException("The specified cache is close");
}
}
private String getNCacheKey(K key) {
return key.toString();
}
private V getValue(Object object) {
if (object == null) {
return null;
}
try {
return (V) object;
} catch (Exception ex) {
throw new ClassCastException();
}
}
private NullPointerException getNullPointerException(String argument) {
return new NullPointerException("argument : " + argument + " pass in the function is null");
}
private ArrayList getCacheKeys(Set extends K> keys) {
ArrayList cacheKeys = new ArrayList();
int index = -1;
for (K key : keys) {
if (key == null) {
throw getNullPointerException("key");
}
cacheKeys.add(getNCacheKey(key));
}
return cacheKeys;
}
protected TimeSpan getSlidingExpiration() {
Duration duration = cfg.getExpiryPolicy().getExpiryForCreation();
int sec = (int) TimeUnit.SECONDS.convert(duration.getDurationAmount(), duration.getTimeUnit());
return (new TimeSpan(0, 0, sec));
}
protected java.util.Date getAbsoluteExpiration() {
Duration duration = cfg.getExpiryPolicy().getExpiryForCreation();
final int sec = (int) TimeUnit.SECONDS.convert(duration.getDurationAmount(), duration.getTimeUnit());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, sec);
return calendar.getTime();
}
protected byte getExpiratonType() {
if (cfg.getExpiryPolicy().getClass().toString().contains("TouchedExpiryPolicy")) {
return 1;
} else if (cfg.getExpiryPolicy().getClass().toString().contains("CreatedExpiryPolicy")) {
return 2;
} else if (cfg.getExpiryPolicy().getClass().toString().contains("AccessedExpiryPolicy")) {
return 3;
} else if (cfg.getExpiryPolicy().getClass().toString().contains("ModifiedExpiryPolicy")) {
return 4;
}
return 0;
}
private void insert(K key, V value) {
checkNCacheStatus();
if (key == null) {
throw getNullPointerException("key");
}
if (value == null) {
throw getNullPointerException("Value");
}
String cacheKey = getNCacheKey(key);
try {
CacheItem item = ncache.getCacheItem(cacheKey);
if (item != null) {
Date now = new Date();
TimeSpan timeSpan = new TimeSpan(Math.abs(item.getLastModifiedTime().getHours() - now.getHours()), Math.abs(item.getLastModifiedTime().getMinutes() - now.getMinutes()), Math.abs(item.getLastModifiedTime().getSeconds() - now.getSeconds()));
CacheItem cacheItem = new CacheItem(value);
TimeSpan duration = getSlidingExpiration();
int interval = (int) ((int) timeSpan.getTotalMiliSeconds() - duration.getTotalMiliSeconds() < 0 ? -(timeSpan.getTotalMiliSeconds() - duration.getTotalMiliSeconds()) : timeSpan.getTotalMiliSeconds() - duration.getTotalMiliSeconds());
TimeSpan time = new TimeSpan(interval);
cacheItem.setExpiration(new Expiration(ExpirationType.Sliding, time));
System.out.println("Key = " + cacheItem.getExpiration().toString());
ncache.insert(cacheKey, cacheItem);
} else {
CacheItem cacheItem = new CacheItem(value);
cacheItem.setExpiration(new Expiration(ExpirationType.Sliding, getSlidingExpiration()));
ncache.insert(cacheKey, cacheItem);
}
} catch (Exception ex) {
throw new CacheException(ex.getMessage());
}
}
private Expiration getAccessExpiryDuration(K key, CacheItem item) {
String cacheKey = getNCacheKey(key);
TimeSpan time = getSlidingExpiration();
if (item != null) {
Date now = new Date();
TimeSpan timeSpan = new TimeSpan(Math.abs(item.getLastModifiedTime().getHours() - now.getHours()), Math.abs(item.getLastModifiedTime().getMinutes() - now.getMinutes()), Math.abs(item.getLastModifiedTime().getSeconds() - now.getSeconds()));
TimeSpan duration = getSlidingExpiration();
int interval = (int) ((int) timeSpan.getTotalMiliSeconds() - duration.getTotalMiliSeconds() < 0 ? -(timeSpan.getTotalMiliSeconds() - duration.getTotalMiliSeconds()) : timeSpan.getTotalMiliSeconds() - duration.getTotalMiliSeconds());
time = new TimeSpan(interval);
}
return new Expiration(ExpirationType.Sliding, time);
}
// private Object V(V value) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
public void setClose() {
this.closed = false;
}
private static class NCacheEntryIterator implements Iterator> {
private NCacheCache jcache;
private Entry current;
private Enumeration enumerator;
public NCacheEntryIterator(NCacheCache jcache) {
this.jcache = jcache;
current = null;
enumerator = jcache.ncache;
}
/**
* iterator has next item or not.
*
* @return
*/
@Override
public boolean hasNext() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return enumerator.hasMoreElements();
}
/**
* get next entry from iterator
*
* @return
*/
@Override
public Entry next() {
if (enumerator.hasMoreElements()) {
try {
current = null;
com.alachisoft.ncache.client.internal.caching.Entry map = (com.alachisoft.ncache.client.internal.caching.Entry) enumerator.nextElement();
//CacheItem cacheitem = jcache.tayzGrid.getCacheItem(enumerator.nextElement());
if (jcache.getExpiratonType() == 3) {
jcache.insert((K) map.getKey(), (V) map.getValue());
}
if (map.getValue() != null)
current = new NCacheEntry(new CacheItem(map.getValue()), map.getKey(), jcache.cfg.getKeyType(), jcache.cfg.getValueType());
} catch (Exception ex) {
}
}
return current;
}
@Override
public void remove() {
}
}
private static class TayzGridMutableEntry implements MutableEntry {
private final NCacheCache jCache;
private final K key;
private final boolean fromLoader;
private final V initialValue;
private volatile V newValue;
private volatile boolean deleted;
private volatile boolean skipDelete;
public TayzGridMutableEntry(final NCacheCache jCache, final V element, final K key, final boolean fromLoader) {
this.jCache = jCache;
this.key = key;
this.fromLoader = fromLoader;
if (element != null) {
initialValue = element;
} else {
initialValue = null;
}
newValue = initialValue;
}
@Override
public boolean exists() {
return !deleted && newValue != null;
}
@Override
public void remove() {
skipDelete = initialValue == null && newValue != null;
newValue = null;
deleted = true;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
if (newValue != initialValue) return newValue;
if (initialValue != null && !fromLoader) {
final Duration expiryForAccess = jCache.cfg.getExpiryPolicy().getExpiryForAccess();//it wont call in hibernate
if (expiryForAccess != null && expiryForAccess.isZero()) {
remove();
}
}
return initialValue;
}
@Override
public void setValue(final V value) {
if (value == null) {
throw new EntryProcessorException();
}
deleted = false;
newValue = value;
}
@Override
public T unwrap(final Class type) {
if (type.isAssignableFrom(this.getClass()))
return type.cast(this);
if (type.isAssignableFrom(Object.class))
return type.cast(this);
return null;
}
void apply(final NCacheCache jCache) {
if (deleted && !skipDelete) {
jCache.remove(key);
}
if (newValue != initialValue && newValue != null) {
jCache.put(key, newValue);
}
}
}
}