Skip to content

Commit

Permalink
add: cache size config
Browse files Browse the repository at this point in the history
  • Loading branch information
HimaJyun committed Dec 20, 2018
1 parent dc7e033 commit 80cb4a6
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 48 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: java
jdk:
- oraclejdk8
- oraclejdk8
- oraclejdk11
branches:
only:
- master
- master
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>jp.jyn</groupId>
<artifactId>Jecon</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/jp/jyn/jecon/cache/CacheFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jp.jyn.jecon.cache;

import java.util.HashMap;
import java.util.Map;

public class CacheFactory {
public final static int DISABLE = 0;
public final static int INFINITY = -1;

private CacheFactory() {

}

public static <K, V> Map<K, V> createCache(int size) {
if (size == -1) {
return new HashMap<>();
} else if (size == 0) {
return NoOpMap.getInstance();
} else if (size > 0) {
return new LRUMap<>(size);
}
throw new IllegalArgumentException("Invalid size");
}
}
17 changes: 17 additions & 0 deletions src/main/java/jp/jyn/jecon/cache/LRUMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jp.jyn.jecon.cache;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUMap<K, V> extends LinkedHashMap<K, V> {
private final int maxSize;
public LRUMap(int maxSize) {
super((maxSize * 4) / 3, 0.75f, true);
this.maxSize = maxSize;
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxSize;
}
}
131 changes: 131 additions & 0 deletions src/main/java/jp/jyn/jecon/cache/NoOpMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package jp.jyn.jecon.cache;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* "Do nothing" cache
*
* @param <K> Key
* @param <V> Value
*/
public class NoOpMap<K, V> implements Map<K, V> {
private final static NoOpMap instance = new NoOpMap();
@SuppressWarnings("unchecked")
public static <K, V> NoOpMap<K, V> getInstance() {
return (NoOpMap<K, V>) instance;
}
private NoOpMap() {
}

@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean containsKey(Object key) {
return false;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public V get(Object key) {
return null;
}
@Override
public V put(K key, V value) {
return null;
}
@Override
public V remove(Object key) {
return null;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {

}
@Override
public void clear() {

}
@Override
public Set<K> keySet() {
return Collections.emptySet();
}
@Override
public Collection<V> values() {
return Collections.emptySet();
}
@Override
public Set<Entry<K, V>> entrySet() {
return Collections.emptySet();
}

@Override
public V getOrDefault(Object key, V defaultValue) {
return defaultValue;
}
@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
}

@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
}

@Override
public V putIfAbsent(K key, V value) {
return null;
}

@Override
public boolean remove(Object key, Object value) {
return false;
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return false;
}
@Override
public V replace(K key, V value) {
return null;
}
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
return Objects.requireNonNull(mappingFunction).apply(key);
}
@Override
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
return null;
}
@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return Objects.requireNonNull(remappingFunction).apply(key, null);
}
@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
return value;
}

@Override
public int hashCode() {
return 0;
}
}
76 changes: 39 additions & 37 deletions src/main/java/jp/jyn/jecon/config/ConfigStruct.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jp.jyn.jecon.config;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;

Expand All @@ -8,26 +9,6 @@

public class ConfigStruct {

/*
*    ■■■■■
*       ■
*       ■
*       ■
*       ■    ■■■      ■■■ ■    ■■■    ■■ ■■■
*       ■   ■   ■    ■   ■■   ■   ■    ■■   ■
*       ■  ■     ■  ■     ■  ■     ■   ■    ■
*       ■  ■■■■■■■  ■        ■     ■   ■    ■
* ■     ■  ■        ■        ■     ■   ■    ■
* ■     ■  ■        ■        ■     ■   ■    ■
* ■     ■  ■     ■  ■     ■  ■     ■   ■    ■
*  ■   ■    ■    ■   ■    ■   ■   ■    ■    ■
*   ■■■      ■■■■     ■■■■     ■■■    ■■■  ■■■
*
* J = Hima[J]yun(and [J]ava...)
* econ = [Econ]omy
* (61万円1株売りを1円61万株売りにしそうな名前だけど気にしない……)
*/

/**
* 設定
*/
Expand Down Expand Up @@ -59,6 +40,7 @@ public class ConfigStruct {
public static final String FORMAT_MACRO_MINOR_CURRENCY = "[%MinorCurrency%]";

private DbConfig dbConfig = null;
private CacheConfig cacheConfig = null;

/**
* 各種設定構造体を初期化します。
Expand Down Expand Up @@ -92,7 +74,13 @@ public ConfigStruct reloadConfig() {

// バージョンがない->1.1.3未満
if (!conf.contains("version", true)) {
conf.set("version", 1);
conf.set("version", 2);
customconfig.saveConfig();
} else if (conf.getInt("version") == 1) {
// 1 -> 2
conf.set("cache.id", -1);
conf.set("cache.balance", -1);
conf.set("version", 2);
customconfig.saveConfig();
}

Expand All @@ -109,7 +97,8 @@ public ConfigStruct reloadConfig() {
formatMinorPlural = customconfig.replaceColor(conf.getString("Format.Minor.Plural", ""));
formatFormat = customconfig.replaceColor(conf.getString("Format.Format", ""));

dbConfig = new DbConfig();
dbConfig = new DbConfig(conf.getConfigurationSection("Database"), plg);
cacheConfig = new CacheConfig(conf.getConfigurationSection("cache"));

return this;
}
Expand All @@ -119,31 +108,30 @@ public ConfigStruct reloadConfig() {
*
* @author HimaJyun
*/
public final class DbConfig {
public static final class DbConfig {
public final boolean isMySQL;
public final String url;
public final String prefix;
public final String prefix; // TODO: prefix機能はいらない(文字列連結の無駄遣い)
public final int poolSize;
public final long timeout;
public final Properties propaties = new Properties();

private DbConfig() {
private DbConfig(ConfigurationSection conf, Plugin plugin) {
String tmp = "jdbc:";
if (conf.getString("Database.Type", "sqlite").equalsIgnoreCase("mysql")) {
if (conf.getString("Type", "sqlite").equalsIgnoreCase("mysql")) {
isMySQL = true;
// mysql://localhost:3306/jecon
tmp += "mysql://"
+ conf.getString("Database.MySQL.Host", "localhost:3306")
+ "/"
+ conf.getString("Database.MySQL.Name", "jecon");
propaties.put("user", conf.getString("Database.MySQL.User", "root"));
propaties.put("password", conf.getString("Database.MySQL.Pass"));
prefix = conf.getString("Database.MySQL.Prefix", "jecon_");
+ conf.getString("MySQL.Host", "localhost:3306")
+ "/"
+ conf.getString("MySQL.Name", "jecon");
propaties.put("user", conf.getString("MySQL.User", "root"));
propaties.put("password", conf.getString("MySQL.Pass"));
prefix = conf.getString("MySQL.Prefix", "jecon_");
} else {
isMySQL = false;
// sqlite:plugins/Jecon/jecon.db
File tmpFile = new File(plg.getDataFolder(),
conf.getString("Database.SQLite.File", "jecon.db"));
File tmpFile = new File(plugin.getDataFolder(), conf.getString("SQLite.File", "jecon.db"));
tmpFile.getParentFile().mkdirs();
// URL
tmp += "sqlite:" + tmpFile.getPath();
Expand All @@ -152,16 +140,26 @@ private DbConfig() {
// 共通設定
url = tmp;
// プロパティ取得
tmp = "Database." + (isMySQL ? "MySQL" : "SQLite") + ".Propaties";
tmp = (isMySQL ? "MySQL" : "SQLite") + ".Propaties";
if (conf.contains(tmp)) {
for (String key : conf.getConfigurationSection(tmp).getKeys(false)) {
propaties.put(key, conf.getString(tmp + "." + key));
}
}

// パフォーマンス周り
poolSize = conf.getInt("Database.Poolsize", -1);
timeout = conf.getLong("Database.Timeout", -1);
poolSize = conf.getInt("Poolsize", -1);
timeout = conf.getLong("Timeout", -1);
}
}

public static final class CacheConfig {
public final int id;
public final int balance;

public CacheConfig(ConfigurationSection conf) {
id = conf.getInt("id");
balance = conf.getInt("balance");
}
}

Expand Down Expand Up @@ -263,4 +261,8 @@ public String getFormatFormat() {
public DbConfig getDbConfig() {
return dbConfig;
}

public CacheConfig getCacheConfig() {
return cacheConfig;
}
}
Loading

0 comments on commit 80cb4a6

Please sign in to comment.