-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improved caching * Cache data objects instead of CompletableFutures
- Loading branch information
Showing
6 changed files
with
111 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 30 additions & 14 deletions
44
src/main/java/me/robertlit/spigotresources/internal/AuthorManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,45 @@ | ||
package me.robertlit.spigotresources.internal; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.google.gson.Gson; | ||
import me.robertlit.spigotresources.api.Author; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class AuthorManager { | ||
|
||
private static final String GET_AUTHOR_URL = "https://api.spigotmc.org/simple/0.1/index.php?action=getAuthor&id=%d"; | ||
|
||
private final Map<Integer, Author> idToAuthorMap = Collections.synchronizedMap(new HashMap<>()); | ||
|
||
private final Gson gson = new Gson(); | ||
private final LoadingCache<Integer, Author> authorCache; | ||
|
||
public AuthorManager(long duration, TimeUnit unit) { | ||
this.authorCache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(duration, unit) | ||
.build(new CacheLoader<Integer, Author>() { | ||
@Override | ||
public Author load(@NotNull Integer authorId) throws Exception { | ||
Author author = gson.fromJson(HttpRequester.requestString(String.format(GET_AUTHOR_URL, authorId)), Author.class); | ||
if (author == null) { | ||
throw new Exception(); | ||
} | ||
return author; | ||
} | ||
}); | ||
} | ||
|
||
public CompletableFuture<Author> getAuthor(int authorId, boolean fetch) { | ||
if (fetch || idToAuthorMap.get(authorId) == null) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
Author author = gson.fromJson(HttpRequester.requestString(String.format(GET_AUTHOR_URL, authorId)), Author.class); | ||
idToAuthorMap.put(authorId, author); | ||
return author; | ||
}); | ||
} | ||
return CompletableFuture.completedFuture(idToAuthorMap.get(authorId)); | ||
public CompletableFuture<Author> getAuthor(int authorId) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return authorCache.get(authorId); | ||
} catch (ExecutionException e) { | ||
return null; | ||
} | ||
}); | ||
} | ||
} |
83 changes: 55 additions & 28 deletions
83
src/main/java/me/robertlit/spigotresources/internal/ResourceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,74 @@ | ||
package me.robertlit.spigotresources.internal; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import me.robertlit.spigotresources.api.Resource; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.*; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ResourceManager { | ||
|
||
private static final String GET_RESOURCE_URL = "https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=%d"; | ||
public static final String GET_RESOURCES_BY_AUTHOR_URL = "https://api.spigotmc.org/simple/0.1/index.php?action=getResourcesByAuthor&id=%d"; | ||
|
||
private final Map<Integer, Resource> idToResourceMap = Collections.synchronizedMap(new HashMap<>()); | ||
private final Map<Integer, Collection<Resource>> authorToResourcesMap = Collections.synchronizedMap(new HashMap<>()); | ||
private static final String GET_RESOURCES_BY_AUTHOR_URL = "https://api.spigotmc.org/simple/0.1/index.php?action=getResourcesByAuthor&id=%d"; | ||
|
||
private final Gson gson = new Gson(); | ||
private final LoadingCache<Integer, Resource> resourceCache; | ||
private final LoadingCache<Integer, Collection<Resource>> authorResourcesCache; | ||
|
||
public ResourceManager(long duration, TimeUnit unit) { | ||
this.resourceCache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(duration, unit) | ||
.build(new CacheLoader<Integer, Resource>() { | ||
@Override | ||
public Resource load(@NotNull Integer resourceId) throws Exception { | ||
Resource resource = gson.fromJson(HttpRequester.requestString(String.format(GET_RESOURCE_URL, resourceId)), Resource.class); | ||
if (resource == null) { | ||
throw new Exception(); | ||
} | ||
return resource; | ||
} | ||
}); | ||
this.authorResourcesCache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(duration, unit) | ||
.build(new CacheLoader<Integer, Collection<Resource>>() { | ||
@Override | ||
public Collection<Resource> load(@NotNull Integer authorId) { | ||
Type type = new TypeToken<Collection<Resource>>(){}.getType(); | ||
Collection<Resource> resources = gson.fromJson(HttpRequester.requestString(String.format(GET_RESOURCES_BY_AUTHOR_URL, authorId)), type); | ||
if (resources == null) { | ||
return Collections.emptyList(); | ||
} | ||
return Collections.unmodifiableCollection(resources); | ||
} | ||
}); | ||
} | ||
|
||
public CompletableFuture<Resource> getResource(int resourceId, boolean fetch) { | ||
if (fetch || idToResourceMap.get(resourceId) == null) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
Resource resource = gson.fromJson(HttpRequester.requestString(String.format(GET_RESOURCE_URL, resourceId)), Resource.class); | ||
idToResourceMap.put(resourceId, resource); | ||
return resource; | ||
}); | ||
} | ||
return CompletableFuture.completedFuture(idToResourceMap.get(resourceId)); | ||
public CompletableFuture<Resource> getResource(int resourceId) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return resourceCache.get(resourceId); | ||
} catch (ExecutionException e) { | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
public CompletableFuture<Collection<Resource>> getResourcesByAuthor(int authorId, boolean fetch) { | ||
if (fetch || authorToResourcesMap.get(authorId) == null) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
Type type = new TypeToken<Collection<Resource>>(){}.getType(); | ||
Collection<Resource> resources = gson.fromJson(HttpRequester.requestString(String.format(GET_RESOURCES_BY_AUTHOR_URL, authorId)), type); | ||
if (resources == null) { | ||
return Collections.emptyList(); | ||
} | ||
Collection<Resource> unmodifiable = Collections.unmodifiableCollection(resources); | ||
authorToResourcesMap.put(authorId, unmodifiable); | ||
return unmodifiable; | ||
}); | ||
} | ||
return CompletableFuture.completedFuture(authorToResourcesMap.get(authorId)); | ||
public CompletableFuture<Collection<Resource>> getResourcesByAuthor(int authorId) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return authorResourcesCache.get(authorId); | ||
} catch (ExecutionException e) { | ||
return Collections.emptyList(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters