Skip to content

Instantly share code, notes, and snippets.

@aikar
Created January 21, 2016 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aikar/f63444ad2260c9f7d738 to your computer and use it in GitHub Desktop.
Save aikar/f63444ad2260c9f7d738 to your computer and use it in GitHub Desktop.
static final RemovalListener<Object, EmpireUser> USER_REMOVAL_LISTENER = notification -> {
final EmpireUser user = notification.getValue();
switch (notification.getCause()) {
case EXPIRED:
case SIZE:
if (user != null && user.getPlayer() != null && user.getPlayer().isOnline()) {
user.refreshCaches();
}
}
};
public static Cache<UUID, String> uuidToNameCache =
CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterAccess(3, TimeUnit.DAYS)
.build();
public static Cache<UUID, EmpireUser> EmpireUser_uuidCache =
CacheBuilder.newBuilder()
.maximumSize(512)
.expireAfterAccess(15, TimeUnit.MINUTES)
.removalListener(USER_REMOVAL_LISTENER)
.build();
public static Cache<String, EmpireUser> EmpireUser_nameCache =
CacheBuilder.newBuilder()
.maximumSize(1024)
.expireAfterAccess(30, TimeUnit.MINUTES)
.removalListener(USER_REMOVAL_LISTENER)
.build();
public static Cache<Long, EmpireUser> EmpireUser_userIdCache =
CacheBuilder.newBuilder()
.maximumSize(1024)
.expireAfterAccess(30, TimeUnit.MINUTES)
.removalListener(USER_REMOVAL_LISTENER)
.build();
public static LoadingCache<Long, Boolean> bannedCache =
CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(new CacheLoader<Long, Boolean>() {
@Override
public Boolean load(Long user) throws Exception {
try {
return EmpireDb.getFirstRow(
"SELECT user_id FROM user_ban WHERE user_id = ?", user) != null;
} catch (SQLException e) {
Util.printException(
"Exception in isBanned: " + user, e);
}
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment