use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.
the class AbstractCacheRecordStore method writeThroughCache.
public void writeThroughCache(Data key, Object value) throws CacheWriterException {
if (isWriteThrough() && cacheWriter != null) {
try {
Object objKey = dataToValue(key);
Object objValue = toValue(value);
cacheWriter.write(new CacheEntry<Object, Object>(objKey, objValue));
} catch (Exception e) {
if (!(e instanceof CacheWriterException)) {
throw new CacheWriterException("Exception in CacheWriter during write", e);
} else {
throw (CacheWriterException) e;
}
}
}
}
use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.
the class AbstractCacheRecordStore method deleteAllCacheEntry.
@SuppressFBWarnings("WMI_WRONG_MAP_ITERATOR")
protected void deleteAllCacheEntry(Set<Data> keys) {
if (isWriteThrough() && cacheWriter != null && keys != null && !keys.isEmpty()) {
Map<Object, Data> keysToDelete = new HashMap<Object, Data>();
for (Data key : keys) {
Object localKeyObj = dataToValue(key);
keysToDelete.put(localKeyObj, key);
}
Set<Object> keysObject = keysToDelete.keySet();
try {
cacheWriter.deleteAll(keysObject);
} catch (Exception e) {
if (!(e instanceof CacheWriterException)) {
throw new CacheWriterException("Exception in CacheWriter during deleteAll", e);
} else {
throw (CacheWriterException) e;
}
} finally {
for (Object undeletedKey : keysObject) {
Data undeletedKeyData = keysToDelete.get(undeletedKey);
keys.remove(undeletedKeyData);
}
}
}
}
use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.
the class AbstractCacheRecordStore method deleteCacheEntry.
protected void deleteCacheEntry(Data key) {
if (isWriteThrough() && cacheWriter != null) {
try {
Object objKey = dataToValue(key);
cacheWriter.delete(objKey);
} catch (Exception e) {
if (!(e instanceof CacheWriterException)) {
throw new CacheWriterException("Exception in CacheWriter during delete", e);
} else {
throw (CacheWriterException) e;
}
}
}
}
use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.
the class CacheReadWriteThroughTest method do_putAsAdd_writeThrough.
private void do_putAsAdd_writeThrough(boolean acceptAll) {
final int ENTRY_COUNT = 100;
final String cacheName = randomName();
CacheManager cacheManager = cachingProvider.getCacheManager();
assertNotNull(cacheManager);
assertNull(cacheManager.getCache(cacheName));
PutCacheWriter putCacheWriter;
if (acceptAll) {
putCacheWriter = new PutCacheWriter();
} else {
putCacheWriter = new PutCacheWriter(new ModValueChecker(ENTRY_COUNT / 10));
}
CacheConfig<Integer, Integer> config = createCacheConfig();
config.setWriteThrough(true);
config.setCacheWriterFactory(FactoryBuilder.factoryOf(putCacheWriter));
ICache<Integer, Integer> cache = cacheManager.createCache(cacheName, config).unwrap(ICache.class);
assertNotNull(cache);
List<Integer> bannedKeys = new ArrayList<Integer>();
for (int i = 0; i < ENTRY_COUNT; i++) {
try {
cache.put(i, i);
} catch (CacheWriterException e) {
bannedKeys.add(i);
}
}
assertEquals(ENTRY_COUNT - bannedKeys.size(), cache.size());
for (Integer bannedKey : bannedKeys) {
assertNull(cache.get(bannedKey));
}
}
use of javax.cache.integration.CacheWriterException in project redisson by redisson.
the class JCache method remove.
@Override
public boolean remove(K key) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
long startTime = System.currentTimeMillis();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
V oldValue = getValue(key);
boolean result = removeValue(key);
try {
cacheWriter.delete(key);
} catch (CacheWriterException e) {
if (oldValue != null) {
putValue(key, oldValue);
}
throw e;
} catch (Exception e) {
if (oldValue != null) {
putValue(key, oldValue);
}
throw new CacheWriterException(e);
}
if (result) {
cacheManager.getStatBean(this).addRemovals(1);
}
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return result;
} finally {
lock.unlock();
}
} else {
boolean result = removeValue(key);
if (result) {
cacheManager.getStatBean(this).addRemovals(1);
}
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return result;
}
}
Aggregations