added cache
This commit is contained in:
parent
d2f09e5033
commit
6ef7488495
28
src/main/java/sarsoo/fmframework/cache/CacheEntry.java
vendored
Normal file
28
src/main/java/sarsoo/fmframework/cache/CacheEntry.java
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
package sarsoo.fmframework.cache;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class CacheEntry<T> {
|
||||
|
||||
private LocalDateTime date;
|
||||
private T subject;
|
||||
|
||||
public CacheEntry(T input) {
|
||||
date = LocalDateTime.now();
|
||||
subject = input;
|
||||
}
|
||||
|
||||
public LocalDateTime getTime() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public T getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return date.toString() + ' ' + subject.toString();
|
||||
}
|
||||
|
||||
}
|
16
src/main/java/sarsoo/fmframework/cache/IEphemeralCache.java
vendored
Normal file
16
src/main/java/sarsoo/fmframework/cache/IEphemeralCache.java
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
package sarsoo.fmframework.cache;
|
||||
|
||||
import sarsoo.fmframework.cache.puller.Puller;
|
||||
import sarsoo.fmframework.log.Log;
|
||||
|
||||
public interface IEphemeralCache<T> {
|
||||
|
||||
public <S> T get(Puller<T, S> input);
|
||||
|
||||
public <S> T getNew(Puller<T, S> input);
|
||||
|
||||
public void flush();
|
||||
|
||||
public void dumpToLog(Log log);
|
||||
|
||||
}
|
15
src/main/java/sarsoo/fmframework/cache/IStaticCache.java
vendored
Normal file
15
src/main/java/sarsoo/fmframework/cache/IStaticCache.java
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
package sarsoo.fmframework.cache;
|
||||
|
||||
import sarsoo.fmframework.log.Log;
|
||||
|
||||
public interface IStaticCache<T, S> {
|
||||
|
||||
public T get(S input);
|
||||
|
||||
public T getNew(S input);
|
||||
|
||||
public void flush();
|
||||
|
||||
public void dumpToLog(Log log);
|
||||
|
||||
}
|
85
src/main/java/sarsoo/fmframework/cache/StaticCache.java
vendored
Normal file
85
src/main/java/sarsoo/fmframework/cache/StaticCache.java
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
package sarsoo.fmframework.cache;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
|
||||
import sarsoo.fmframework.cache.puller.Puller;
|
||||
import sarsoo.fmframework.log.Log;
|
||||
import sarsoo.fmframework.log.Logger;
|
||||
import sarsoo.fmframework.log.entry.ErrorEntry;
|
||||
import sarsoo.fmframework.log.entry.LogEntry;
|
||||
|
||||
public class StaticCache<T, S> implements IStaticCache<T, S> {
|
||||
|
||||
private ArrayList<CacheEntry<T>> pool;
|
||||
private Puller<T, S> puller;
|
||||
|
||||
public StaticCache(Puller<T, S> puller) {
|
||||
pool = new ArrayList<CacheEntry<T>>();
|
||||
this.puller = puller;
|
||||
}
|
||||
|
||||
public void setPuller(Puller<T, S> puller) {
|
||||
this.puller = puller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(S input) {
|
||||
|
||||
Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().equals(input)).findFirst();
|
||||
|
||||
if (item.isPresent()) {
|
||||
Logger.getLog().log(new LogEntry("getCachedItem").addArg("found").addArg(input.toString()));
|
||||
return item.get().getSubject();
|
||||
} else {
|
||||
Logger.getLog().log(new LogEntry("getCachedItem").addArg("pulling").addArg(input.toString()));
|
||||
|
||||
T pulled = puller.pull(input);
|
||||
|
||||
if (pulled != null) {
|
||||
Logger.getLog().log(new LogEntry("getCachedItem").addArg("pulled").addArg(input.toString()));
|
||||
pool.add(new CacheEntry<T>(pulled));
|
||||
return pulled;
|
||||
} else {
|
||||
Logger.getLog()
|
||||
.logError(new ErrorEntry("getCachedItem").addArg("null item").addArg(input.toString()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getNew(S input) {
|
||||
Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().equals(input)).findFirst();
|
||||
|
||||
if (item.isPresent()) {
|
||||
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("removed").addArg(input.toString()));
|
||||
pool.remove(item);
|
||||
}
|
||||
|
||||
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("pulling").addArg(input.toString()));
|
||||
|
||||
T pulled = puller.pull(input);
|
||||
|
||||
if (pulled != null) {
|
||||
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("pulled").addArg(input.toString()));
|
||||
pool.add(new CacheEntry<T>(pulled));
|
||||
return pulled;
|
||||
} else {
|
||||
Logger.getLog().logError(new ErrorEntry("getNewCachedItem").addArg("null item").addArg(input.toString()));
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
pool.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dumpToLog(Log log) {
|
||||
pool.stream().forEach(i -> log.log(new LogEntry("dumpCache").addArg(i.toString())));
|
||||
}
|
||||
|
||||
}
|
@ -11,6 +11,8 @@ public class TagPool {
|
||||
|
||||
private static TagPool instance;
|
||||
|
||||
private ArrayList<FMObjList> tagList;
|
||||
|
||||
private TagPool() {
|
||||
tagList = new ArrayList<FMObjList>();
|
||||
}
|
||||
@ -33,8 +35,6 @@ public class TagPool {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private ArrayList<FMObjList> tagList;
|
||||
|
||||
public FMObjList getTag(String name) {
|
||||
System.out.println("gettag " + name);
|
||||
int i;
|
||||
|
18
src/main/java/sarsoo/fmframework/cache/puller/AlbumPuller.java
vendored
Normal file
18
src/main/java/sarsoo/fmframework/cache/puller/AlbumPuller.java
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
package sarsoo.fmframework.cache.puller;
|
||||
|
||||
import sarsoo.fmframework.fm.FmNetwork;
|
||||
import sarsoo.fmframework.music.Album;
|
||||
|
||||
public class AlbumPuller implements Puller<Album, Album> {
|
||||
|
||||
private FmNetwork net;
|
||||
|
||||
public AlbumPuller(FmNetwork net) {
|
||||
this.net = net;
|
||||
}
|
||||
|
||||
public Album pull(Album album) {
|
||||
return net.refresh(album);
|
||||
}
|
||||
|
||||
}
|
23
src/main/java/sarsoo/fmframework/cache/puller/ArtistPuller.java
vendored
Normal file
23
src/main/java/sarsoo/fmframework/cache/puller/ArtistPuller.java
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
package sarsoo.fmframework.cache.puller;
|
||||
|
||||
import sarsoo.fmframework.fm.FmNetwork;
|
||||
import sarsoo.fmframework.music.Album;
|
||||
import sarsoo.fmframework.music.Artist;
|
||||
|
||||
public class ArtistPuller implements Puller<Artist, Artist> {
|
||||
|
||||
private FmNetwork net;
|
||||
|
||||
public ArtistPuller(FmNetwork net) {
|
||||
this.net = net;
|
||||
}
|
||||
|
||||
public ArtistPuller(FmNetwork net, Artist artist) {
|
||||
this.net = net;
|
||||
}
|
||||
|
||||
public Artist pull(Artist artist) {
|
||||
return net.refresh(artist);
|
||||
}
|
||||
|
||||
}
|
7
src/main/java/sarsoo/fmframework/cache/puller/Puller.java
vendored
Normal file
7
src/main/java/sarsoo/fmframework/cache/puller/Puller.java
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
package sarsoo.fmframework.cache.puller;
|
||||
|
||||
public interface Puller<T, S> {
|
||||
|
||||
public T pull(S input);
|
||||
|
||||
}
|
18
src/main/java/sarsoo/fmframework/cache/puller/TrackPuller.java
vendored
Normal file
18
src/main/java/sarsoo/fmframework/cache/puller/TrackPuller.java
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
package sarsoo.fmframework.cache.puller;
|
||||
|
||||
import sarsoo.fmframework.fm.FmNetwork;
|
||||
import sarsoo.fmframework.music.Track;
|
||||
|
||||
public class TrackPuller implements Puller<Track, Track> {
|
||||
|
||||
private FmNetwork net;
|
||||
|
||||
public TrackPuller(FmNetwork net) {
|
||||
this.net = net;
|
||||
}
|
||||
|
||||
public Track pull(Track track) {
|
||||
return net.refresh(track);
|
||||
}
|
||||
|
||||
}
|
30
src/test/java/sarsoo/fmframework/cache/CacheTest.java
vendored
Normal file
30
src/test/java/sarsoo/fmframework/cache/CacheTest.java
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
package sarsoo.fmframework.cache;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import sarsoo.fmframework.cache.puller.AlbumPuller;
|
||||
import sarsoo.fmframework.fm.FmUserNetwork;
|
||||
import sarsoo.fmframework.music.Album;
|
||||
import sarsoo.fmframework.net.Key;
|
||||
import sarsoo.fmframework.music.Album.AlbumBuilder;
|
||||
import sarsoo.fmframework.music.Artist.ArtistBuilder;
|
||||
|
||||
public class CacheTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
StaticCache<Album, Album> cache = new StaticCache<Album, Album>(new AlbumPuller(new FmUserNetwork(Key.getKey(), "sarsoo")));
|
||||
|
||||
Album pimp = cache.get(new AlbumBuilder("to pimp a butterfly", new ArtistBuilder("kendrick lamar").build()).build());
|
||||
System.out.println(pimp.getUserPlayCount());
|
||||
Album pimp2 = cache.get(new AlbumBuilder("to pimp a butterfly", new ArtistBuilder("kendrick lamar").build()).build());
|
||||
System.out.println(pimp2.getUserPlayCount());
|
||||
Album pimp3 = cache.getNew(new AlbumBuilder("to pimp a butterfly", new ArtistBuilder("kendrick lamar").build()).build());
|
||||
System.out.println(pimp3.getUserPlayCount());
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user