Improve memory footprint in low throughput caches

This commit is contained in:
Luck
2016-12-20 12:42:02 +00:00
Unverified
parent 829eacb0fb
commit 8a692200d5
14 changed files with 178 additions and 1 deletions
@@ -41,6 +41,7 @@ import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Holds an easily accessible cache of a user's data in a number of contexts
@@ -59,6 +60,7 @@ public class UserCache implements UserData {
private final CalculatorFactory calculatorFactory;
private final LoadingCache<Contexts, PermissionCache> permission = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<Contexts, PermissionCache>() {
@Override
public PermissionCache load(Contexts contexts) {
@@ -73,6 +75,7 @@ public class UserCache implements UserData {
});
private final LoadingCache<Contexts, MetaCache> meta = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<Contexts, MetaCache>() {
@Override
public MetaCache load(Contexts contexts) {
@@ -153,4 +156,9 @@ public class UserCache implements UserData {
permission.asMap().values().forEach(PermissionData::invalidateCache);
}
public void cleanup() {
permission.cleanUp();
meta.cleanUp();
}
}
@@ -76,6 +76,7 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
@@ -142,6 +143,7 @@ public abstract class PermissionHolder {
/* External Caches - may depend on the state of other instances. */
private LoadingCache<GetAllNodesHolder, SortedSet<LocalizedNode>> getAllNodesCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<GetAllNodesHolder, SortedSet<LocalizedNode>>() {
@Override
public SortedSet<LocalizedNode> load(GetAllNodesHolder getAllNodesHolder) {
@@ -149,6 +151,7 @@ public abstract class PermissionHolder {
}
});
private LoadingCache<ExtractedContexts, Set<LocalizedNode>> getAllNodesFilteredCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<ExtractedContexts, Set<LocalizedNode>>() {
@Override
public Set<LocalizedNode> load(ExtractedContexts extractedContexts) throws Exception {
@@ -156,6 +159,7 @@ public abstract class PermissionHolder {
}
});
private LoadingCache<ExportNodesHolder, Map<String, Boolean>> exportNodesCache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(new CacheLoader<ExportNodesHolder, Map<String, Boolean>>() {
@Override
public Map<String, Boolean> load(ExportNodesHolder exportNodesHolder) throws Exception {
@@ -167,6 +171,12 @@ public abstract class PermissionHolder {
/* Caching apply methods. Are just called by the caching instances to gather data about the instance. */
protected void forceCleanup() {
getAllNodesCache.cleanUp();
getAllNodesFilteredCache.cleanUp();
exportNodesCache.cleanUp();
}
private void invalidateCache(boolean enduring) {
if (enduring) {
enduringCache.invalidate();
@@ -149,4 +149,12 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
super.clearNodes();
getPlugin().getUserManager().giveDefaultIfNeeded(this, false);
}
public void cleanup() {
UserCache cache = userData;
if (cache != null) {
cache.cleanup();
}
forceCleanup();
}
}
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.tasks;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.core.model.User;
@RequiredArgsConstructor
public class CacheHousekeepingTask implements Runnable {
private final LuckPermsPlugin plugin;
@Override
public void run() {
for (User user : plugin.getUserManager().getAll().values()) {
user.cleanup();
}
}
}