use of eu.okaeri.persistence.flat.FlatPersistence in project okaeri-platform by OkaeriPoland.
the class ExampleWebApplication method configurePersistence.
// built-in persistence utils
// easy storage for e.g. player properties
// see persistence/PlayerPersistence for details
@Bean(value = "persistence", preload = true)
public DocumentPersistence configurePersistence(@Inject("dataFolder") File dataFolder, TestConfig config) {
// jdbc drivers may require initialization for jdbc urls to work
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException ignored) {
}
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException ignored) {
}
// remember that if plugin is not intended to have shared state
// between multiple instances you must allow users to set persistence's
// basePath manually or add some other possibility to differ keys
// otherwise plugin usage would be limited to one instance per one redis
PersistencePath basePath = PersistencePath.of(config.getStorage().getPrefix());
// multiple backends are possible with an easy switch
switch(config.getStorage().getBackend()) {
case FLAT:
// specify custom child dir in dataFolder or other custom location
File storagePath = new File(config.getStorage().getUri());
return new DocumentPersistence(new FlatPersistence(storagePath, ".yml"), YamlSnakeYamlConfigurer::new, new SerdesWeb());
case REDIS:
// construct redis client based on your needs, e.g. using config
RedisURI redisUri = RedisURI.create(config.getStorage().getUri());
RedisClient redisClient = RedisClient.create(redisUri);
// on the redis side thanks to cjson available in lua scripting
return new DocumentPersistence(new RedisPersistence(basePath, redisClient), JsonSimpleConfigurer::new, new SerdesWeb());
case MONGO:
// construct mongo client based on your needs, e.g. using config
MongoClientURI mongoUri = new MongoClientURI(config.getStorage().getUri());
MongoClient mongoClient = new MongoClient(mongoUri);
// validate if uri contains database
if (mongoUri.getDatabase() == null) {
throw new IllegalArgumentException("Mongo URI needs to specify the database: " + mongoUri.getURI());
}
// it is REQUIRED to use json configurer for the mongo backend
return new DocumentPersistence(new MongoPersistence(basePath, mongoClient, mongoUri.getDatabase()), JsonSimpleConfigurer::new, new SerdesWeb());
case MYSQL:
// setup hikari based on your needs, e.g. using config
HikariConfig mariadbHikari = new HikariConfig();
mariadbHikari.setJdbcUrl(config.getStorage().getUri());
// it is REQUIRED to use json configurer for the mariadb backend
return new DocumentPersistence(new MariaDbPersistence(basePath, mariadbHikari), JsonSimpleConfigurer::new, new SerdesWeb());
case H2:
// setup hikari based on your needs, e.g. using config
HikariConfig jdbcHikari = new HikariConfig();
jdbcHikari.setJdbcUrl(config.getStorage().getUri());
// it is HIGHLY recommended to use json configurer for the jdbc backend
return new DocumentPersistence(new H2Persistence(basePath, jdbcHikari), JsonSimpleConfigurer::new, new SerdesWeb());
default:
throw new RuntimeException("unsupported storage backend: " + config.getStorage().getBackend());
}
}
Aggregations