Skip to content

Commit

Permalink
fix not call access() when size > halfCapacity
Browse files Browse the repository at this point in the history
Change-Id: Ic613f1d387dae26cc3cd6b69346d28a912453f72
  • Loading branch information
javeme committed Jun 24, 2021
1 parent f2549da commit 0dfc5e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {

// NOTE: the count in number of items, not in bytes
private final long capacity;
private final long halfCapacity;

// For user attachment
private final AtomicReference<Object> attachment;
Expand All @@ -61,7 +60,6 @@ public AbstractCache(long capacity) {
capacity = 0L;
}
this.capacity = capacity;
this.halfCapacity = this.capacity >> 1;
this.attachment = new AtomicReference<>();

this.expire = 0L;
Expand All @@ -77,15 +75,13 @@ public V get(K id) {
if (id == null || this.capacity <= 0L) {
return null;
}
V value = null;
if (this.size() <= this.halfCapacity) {
// Maybe the id removed by other threads and returned null value
value = this.access(id);
}

V value = this.access(id);

if (this.enabledMetrics) {
this.collectMetrics(id, value);
}

return value;
}

Expand All @@ -95,11 +91,8 @@ public V getOrFetch(K id, Function<K, V> fetcher) {
if (id == null || this.capacity <= 0L) {
return null;
}
V value = null;
if (this.size() <= this.halfCapacity) {
// Maybe the id removed by other threads and returned null value
value = this.access(id);
}

V value = this.access(id);

if (this.enabledMetrics) {
this.collectMetrics(id, value);
Expand Down Expand Up @@ -247,10 +240,6 @@ public <T> T attachment() {
return attachment;
}

protected final long halfCapacity() {
return this.halfCapacity;
}

protected abstract V access(K id);

protected abstract boolean write(K id, V value, long timeOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class RamCache extends AbstractCache<Id, Object> {
private final LinkedQueueNonBigLock<Id, Object> queue;

private final KeyLock keyLock;
private final long halfCapacity;

public RamCache() {
this(DEFAULT_SIZE);
Expand All @@ -47,11 +48,12 @@ public RamCache() {
public RamCache(long capacity) {
super(capacity);

this.keyLock = new KeyLock();

if (capacity < 0L) {
capacity = 0L;
}
this.keyLock = new KeyLock();
this.halfCapacity = capacity >> 1;

long initialCapacity = capacity >= MB ? capacity >> 10 : 256;
if (initialCapacity > MAX_INIT_CAP) {
initialCapacity = MAX_INIT_CAP;
Expand All @@ -66,8 +68,7 @@ public RamCache(long capacity) {
protected final Object access(Id id) {
assert id != null;

long halfCapacity = this.halfCapacity();
if (this.map.size() <= halfCapacity) {
if (this.map.size() <= this.halfCapacity) {
LinkNode<Id, Object> node = this.map.get(id);
if (node == null) {
return null;
Expand All @@ -83,13 +84,14 @@ protected final Object access(Id id) {

final Lock lock = this.keyLock.lock(id);
try {
// Maybe the id removed by other threads and returned null value
LinkNode<Id, Object> node = this.map.get(id);
if (node == null) {
return null;
}

// NOTE: update the queue only if the size > capacity/2
if (this.map.size() > halfCapacity) {
if (this.map.size() > this.halfCapacity) {
// Move the node from mid to tail
if (this.queue.remove(node) == null) {
// The node may be removed by others through dequeue()
Expand Down

0 comments on commit 0dfc5e9

Please sign in to comment.