diff --git a/src/main/java/sarsoo/fmframework/cache/CacheEntry.java b/src/main/java/sarsoo/fmframework/cache/CacheEntry.java new file mode 100644 index 0000000..f0d4f1e --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/CacheEntry.java @@ -0,0 +1,28 @@ +package sarsoo.fmframework.cache; + +import java.time.LocalDateTime; + +public class CacheEntry { + + 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(); + } + +} diff --git a/src/main/java/sarsoo/fmframework/cache/IEphemeralCache.java b/src/main/java/sarsoo/fmframework/cache/IEphemeralCache.java new file mode 100644 index 0000000..764f210 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/IEphemeralCache.java @@ -0,0 +1,16 @@ +package sarsoo.fmframework.cache; + +import sarsoo.fmframework.cache.puller.Puller; +import sarsoo.fmframework.log.Log; + +public interface IEphemeralCache { + + public T get(Puller input); + + public T getNew(Puller input); + + public void flush(); + + public void dumpToLog(Log log); + +} diff --git a/src/main/java/sarsoo/fmframework/cache/IStaticCache.java b/src/main/java/sarsoo/fmframework/cache/IStaticCache.java new file mode 100644 index 0000000..f2e728f --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/IStaticCache.java @@ -0,0 +1,15 @@ +package sarsoo.fmframework.cache; + +import sarsoo.fmframework.log.Log; + +public interface IStaticCache { + + public T get(S input); + + public T getNew(S input); + + public void flush(); + + public void dumpToLog(Log log); + +} diff --git a/src/main/java/sarsoo/fmframework/cache/StaticCache.java b/src/main/java/sarsoo/fmframework/cache/StaticCache.java new file mode 100644 index 0000000..5857efb --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/StaticCache.java @@ -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 implements IStaticCache { + + private ArrayList> pool; + private Puller puller; + + public StaticCache(Puller puller) { + pool = new ArrayList>(); + this.puller = puller; + } + + public void setPuller(Puller puller) { + this.puller = puller; + } + + @Override + public T get(S input) { + + Optional> 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(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> 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(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()))); + } + +} diff --git a/src/main/java/sarsoo/fmframework/cache/TagPool.java b/src/main/java/sarsoo/fmframework/cache/TagPool.java index a986a8c..6a0bd43 100644 --- a/src/main/java/sarsoo/fmframework/cache/TagPool.java +++ b/src/main/java/sarsoo/fmframework/cache/TagPool.java @@ -10,6 +10,8 @@ import sarsoo.fmframework.util.FMObjList; public class TagPool { private static TagPool instance; + + private ArrayList tagList; private TagPool() { tagList = new ArrayList(); @@ -33,8 +35,6 @@ public class TagPool { return instance; } - private ArrayList tagList; - public FMObjList getTag(String name) { System.out.println("gettag " + name); int i; diff --git a/src/main/java/sarsoo/fmframework/cache/puller/AlbumPuller.java b/src/main/java/sarsoo/fmframework/cache/puller/AlbumPuller.java new file mode 100644 index 0000000..ca31356 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/puller/AlbumPuller.java @@ -0,0 +1,18 @@ +package sarsoo.fmframework.cache.puller; + +import sarsoo.fmframework.fm.FmNetwork; +import sarsoo.fmframework.music.Album; + +public class AlbumPuller implements Puller { + + private FmNetwork net; + + public AlbumPuller(FmNetwork net) { + this.net = net; + } + + public Album pull(Album album) { + return net.refresh(album); + } + +} diff --git a/src/main/java/sarsoo/fmframework/cache/puller/ArtistPuller.java b/src/main/java/sarsoo/fmframework/cache/puller/ArtistPuller.java new file mode 100644 index 0000000..e2356c3 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/puller/ArtistPuller.java @@ -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 { + + 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); + } + +} diff --git a/src/main/java/sarsoo/fmframework/cache/puller/Puller.java b/src/main/java/sarsoo/fmframework/cache/puller/Puller.java new file mode 100644 index 0000000..e4751f7 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/puller/Puller.java @@ -0,0 +1,7 @@ +package sarsoo.fmframework.cache.puller; + +public interface Puller { + + public T pull(S input); + +} diff --git a/src/main/java/sarsoo/fmframework/cache/puller/TrackPuller.java b/src/main/java/sarsoo/fmframework/cache/puller/TrackPuller.java new file mode 100644 index 0000000..90b63fc --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/puller/TrackPuller.java @@ -0,0 +1,18 @@ +package sarsoo.fmframework.cache.puller; + +import sarsoo.fmframework.fm.FmNetwork; +import sarsoo.fmframework.music.Track; + +public class TrackPuller implements Puller { + + private FmNetwork net; + + public TrackPuller(FmNetwork net) { + this.net = net; + } + + public Track pull(Track track) { + return net.refresh(track); + } + +} diff --git a/src/test/java/sarsoo/fmframework/cache/CacheTest.java b/src/test/java/sarsoo/fmframework/cache/CacheTest.java new file mode 100644 index 0000000..7dd1622 --- /dev/null +++ b/src/test/java/sarsoo/fmframework/cache/CacheTest.java @@ -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 cache = new StaticCache(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); + } + +}