use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapCache method get.
/*
* (non-Javadoc)
*
* @see com.cetsoft.imcache.cache.Cache#get(java.lang.Object)
*/
public V get(K key) {
Pointer pointer = pointerMap.get(key);
if (pointer != null) {
readLock(key);
try {
hit.incrementAndGet();
synchronized (pointer) {
byte[] payload = bufferStore.retrieve(pointer);
return serializer.deserialize(payload);
}
} finally {
readUnlock(key);
}
} else {
miss.incrementAndGet();
V value = cacheLoader.load(key);
if (value != null) {
put(key, value);
}
return value;
}
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapCache method put.
/*
* (non-Javadoc)
*
* @see com.cetsoft.imcache.cache.Cache#put(java.lang.Object,
* java.lang.Object)
*/
public void put(K key, V value) {
writeLock(key);
Pointer pointer = pointerMap.get(key);
try {
byte[] bytes = serializer.serialize(value);
if (pointer == null) {
pointer = bufferStore.store(bytes);
} else {
synchronized (pointer) {
pointer = bufferStore.update(pointer, bytes);
}
}
pointerMap.put(key, pointer);
} finally {
writeUnlock(key);
}
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapCache method invalidate.
/*
* (non-Javadoc)
*
* @see com.cetsoft.imcache.cache.Cache#invalidate(java.lang.Object)
*/
public V invalidate(K key) {
writeLock(key);
try {
Pointer pointer = pointerMap.get(key);
if (pointer != null) {
synchronized (pointer) {
byte[] payload = bufferStore.remove(pointer);
pointerMap.remove(key);
return serializer.deserialize(payload);
}
}
} finally {
writeUnlock(key);
}
return null;
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapByteBufferStoreTest method storeBufferOverFlowNextBuffer.
/**
* Store buffer over flow next buffer.
*/
@Test
public void storeBufferOverFlowNextBuffer() {
int size = 100;
byte[] expectedBytes = new byte[size];
random.nextBytes(expectedBytes);
doReturn(buffer).when(bufferStore).currentBuffer();
doNothing().when(bufferStore).nextBuffer();
doThrow(new BufferOverflowException()).doThrow(new BufferOverflowException()).doReturn(pointer).when(buffer).store(expectedBytes);
Pointer actualPointer = bufferStore.store(expectedBytes);
assertEquals(pointer, actualPointer);
verify(bufferStore, times(3)).currentBuffer();
verify(buffer, times(3)).store(expectedBytes);
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapByteBufferStoreTest method store.
/**
* Store.
*/
@Test
public void store() {
int size = 100;
byte[] expectedBytes = new byte[size];
random.nextBytes(expectedBytes);
Pointer pointer = bufferStore.store(expectedBytes);
byte[] actualBytes = bufferStore.retrieve(pointer);
assertArrayEquals(expectedBytes, actualBytes);
}
Aggregations