in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java [1417:1505]
protected void processUpdate(final ICacheElement<K, V> ce)
{
if (!isAlive())
{
log.error("{0}: No longer alive; aborting put of key = {1}",
() -> logCacheName, ce::getKey);
return;
}
log.debug("{0}: Storing element on disk, key: {1}",
() -> logCacheName, ce::getKey);
// old element with same key
IndexedDiskElementDescriptor old = null;
try
{
IndexedDiskElementDescriptor ded = null;
final byte[] data = getElementSerializer().serialize(ce);
// make sure this only locks for one particular cache region
storageLock.writeLock().lock();
try
{
old = keyHash.get(ce.getKey());
// Item with the same key already exists in file.
// Try to reuse the location if possible.
if (old != null && data.length <= old.len)
{
// Reuse the old ded. The defrag relies on ded updates by reference, not
// replacement.
ded = old;
ded.len = data.length;
}
else
{
// we need this to compare in the recycle bin
ded = new IndexedDiskElementDescriptor(dataFile.length(), data.length);
if (doRecycle)
{
final IndexedDiskElementDescriptor rep = recycle.ceiling(ded);
if (rep != null)
{
// remove element from recycle bin
recycle.remove(rep);
ded = rep;
ded.len = data.length;
recycleCnt++;
this.adjustBytesFree(ded, false);
log.debug("{0}: using recycled ded {1} rep.len = {2} ded.len = {3}",
logCacheName, ded.pos, rep.len, ded.len);
}
}
// Put it in the map
keyHash.put(ce.getKey(), ded);
if (queueInput)
{
queuedPutList.add(ded);
log.debug("{0}: added to queued put list. {1}",
() -> logCacheName, queuedPutList::size);
}
// add the old slot to the recycle bin
if (old != null)
{
addToRecycleBin(old);
}
}
dataFile.write(ded, data);
}
finally
{
storageLock.writeLock().unlock();
}
log.debug("{0}: Put to file: {1}, key: {2}, position: {3}, size: {4}",
logCacheName, fileName, ce.getKey(), ded.pos, ded.len);
}
catch (final IOException e)
{
log.error("{0}: Failure updating element, key: {1} old: {2}",
logCacheName, ce.getKey(), old, e);
}
}