Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caffeine Cache #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.deepsymmetry</groupId>
<artifactId>beat-link</artifactId>
<version>7.3.0</version>
<version>7.4.0</version>
<packaging>jar</packaging>

<name>beat-link</name>
Expand Down Expand Up @@ -95,9 +95,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.concurrentlinkedhashmap</groupId>
<artifactId>concurrentlinkedhashmap-lru</artifactId>
<version>1.4.2</version>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -244,7 +244,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>6</release>
<release>8</release>
</configuration>
</plugin>

Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/deepsymmetry/beatlink/data/ArtFinder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.deepsymmetry.beatlink.data;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.deepsymmetry.beatlink.*;
import org.deepsymmetry.beatlink.dbserver.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -72,11 +73,11 @@ public void mediaMounted(SlotReference slot) {
@Override
public void mediaUnmounted(SlotReference slot) {
// Iterate over a copy to avoid concurrent modification issues.
final Set<DataReference> keys = new HashSet<DataReference>(artCache.keySet());
final Set<DataReference> keys = new HashSet<DataReference>(artCache.asMap().keySet());
for (DataReference artReference : keys) {
if (SlotReference.getSlotReference(artReference) == slot) {
logger.debug("Evicting cached artwork in response to unmount report {}", artReference);
artCache.remove(artReference);
artCache.invalidate(artReference);
}
}
// Again iterate over a copy to avoid concurrent modification issues.
Expand Down Expand Up @@ -161,9 +162,9 @@ private void clearArt(DeviceAnnouncement announcement) {
}
}
// Again iterate over a copy to avoid concurrent modification issues
for (DataReference art : new HashSet<DataReference>(artCache.keySet())) {
for (DataReference art : new HashSet<DataReference>(artCache.asMap().keySet())) {
if (art.player == player) {
artCache.remove(art);
artCache.invalidate(art);
}
}
}
Expand Down Expand Up @@ -236,8 +237,7 @@ public AlbumArt getLatestArtFor(DeviceUpdate update) {
* art around even for tracks that are not currently loaded, to save on having to request it again when another
* track from the same album is loaded.
*/
private final ConcurrentLinkedHashMap<DataReference, AlbumArt> artCache =
nzoschke marked this conversation as resolved.
Show resolved Hide resolved
new ConcurrentLinkedHashMap.Builder<DataReference, AlbumArt>().maximumWeightedCapacity(DEFAULT_ART_CACHE_SIZE).build();
private final Cache<DataReference, AlbumArt> artCache = Caffeine.newBuilder().maximumSize(DEFAULT_ART_CACHE_SIZE).build();

/**
* Check how many album art images can be kept in the in-memory second-level cache.
Expand All @@ -246,7 +246,7 @@ public AlbumArt getLatestArtFor(DeviceUpdate update) {
* in-memory art cache.
*/
public long getArtCacheSize() {
return artCache.capacity();
return artCache.policy().eviction().get().getMaximum();
}

/**
Expand All @@ -263,7 +263,7 @@ public void setArtCacheSize(int size) {
throw new IllegalArgumentException("size must be at least 1");

}
artCache.setCapacity(size);
artCache.policy().eviction().get().setMaximum(size);
}

/**
Expand Down Expand Up @@ -412,7 +412,7 @@ private AlbumArt findArtInMemoryCaches(DataReference artReference) {
}

// Not in the hot cache, see if it is in our LRU cache
return artCache.get(artReference);
return artCache.getIfPresent(artReference);
}

/**
Expand Down Expand Up @@ -616,7 +616,7 @@ public void run() {
}
});
hotCache.clear();
artCache.clear();
artCache.invalidateAll();
deliverLifecycleAnnouncement(logger, false);
}
}
Expand Down Expand Up @@ -647,7 +647,7 @@ public String toString() {
StringBuilder sb = new StringBuilder("ArtFinder[running:").append(isRunning()).append(", passive:");
sb.append(MetadataFinder.getInstance().isPassive()).append(", artCacheSize:").append(getArtCacheSize());
if (isRunning()) {
sb.append(", loadedArt:").append(getLoadedArt()).append(", cached art:").append(artCache.size());
sb.append(", loadedArt:").append(getLoadedArt()).append(", cached art:").append(artCache.asMap().size());
}
return sb.append("]").toString();
}
Expand Down