| 1 |
package org.apache.commons.jcs3.engine.memory.lru; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.util.Collections; |
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
import org.apache.commons.jcs3.engine.behavior.ICacheElement; |
| 27 |
import org.apache.commons.jcs3.engine.control.CompositeCache; |
| 28 |
import org.apache.commons.jcs3.engine.memory.AbstractMemoryCache; |
| 29 |
import org.apache.commons.jcs3.engine.memory.util.MemoryElementDescriptor; |
| 30 |
import org.apache.commons.jcs3.engine.stats.behavior.IStats; |
| 31 |
import org.apache.commons.jcs3.log.Log; |
| 32 |
import org.apache.commons.jcs3.log.LogManager; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class LHMLRUMemoryCache<K, V> |
| 38 |
extends AbstractMemoryCache<K, V> |
| 39 |
{ |
| 40 |
|
| 41 |
private static final Log log = LogManager.getLog( LRUMemoryCache.class ); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@Override |
| 49 |
public void initialize( final CompositeCache<K, V> hub ) |
| 50 |
{ |
| 51 |
super.initialize( hub ); |
| 52 |
log.info( "initialized LHMLRUMemoryCache for {0}", this::getCacheName ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@Override |
| 61 |
public Map<K, MemoryElementDescriptor<K, V>> createMap() |
| 62 |
{ |
| 63 |
return Collections.synchronizedMap( new LHMSpooler() ); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@Override |
| 73 |
public void update( final ICacheElement<K, V> ce ) |
| 74 |
throws IOException |
| 75 |
{ |
| 76 |
putCnt.incrementAndGet(); |
| 77 |
map.put( ce.getKey(), new MemoryElementDescriptor<>(ce) ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@Override |
| 87 |
protected void lockedGetElement(final MemoryElementDescriptor<K, V> me) |
| 88 |
{ |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
protected void lockedRemoveElement(final MemoryElementDescriptor<K, V> me) |
| 100 |
{ |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@Override |
| 109 |
protected void lockedRemoveAll() |
| 110 |
{ |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
@Override |
| 121 |
public IStats getStatistics() |
| 122 |
{ |
| 123 |
final IStats stats = super.getStatistics(); |
| 124 |
stats.setTypeName( "LHMLRU Memory Cache" ); |
| 125 |
|
| 126 |
return stats; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void dumpCacheEntries() |
| 135 |
{ |
| 136 |
dumpMap(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@Override |
| 147 |
public int freeElements( final int numberToFree ) |
| 148 |
throws IOException |
| 149 |
{ |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected class LHMSpooler |
| 160 |
extends java.util.LinkedHashMap<K, MemoryElementDescriptor<K, V>> |
| 161 |
{ |
| 162 |
|
| 163 |
private static final long serialVersionUID = -1255907868906762484L; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public LHMSpooler() |
| 170 |
{ |
| 171 |
super( (int) ( getCacheAttributes().getMaxObjects() * .5 ), .75F, true ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@SuppressWarnings("synthetic-access") |
| 181 |
@Override |
| 182 |
protected boolean removeEldestEntry( final Map.Entry<K, MemoryElementDescriptor<K, V>> eldest ) |
| 183 |
{ |
| 184 |
final ICacheElement<K, V> element = eldest.getValue().getCacheElement(); |
| 185 |
|
| 186 |
if ( size() <= getCacheAttributes().getMaxObjects() ) |
| 187 |
{ |
| 188 |
return false; |
| 189 |
} |
| 190 |
log.debug( "LHMLRU max size: {0}. Spooling element, key: {1}", |
| 191 |
() -> getCacheAttributes().getMaxObjects(), element::getKey); |
| 192 |
|
| 193 |
waterfal( element ); |
| 194 |
|
| 195 |
log.debug( "LHMLRU size: {0}", () -> map.size() ); |
| 196 |
return true; |
| 197 |
} |
| 198 |
} |
| 199 |
} |