Skip to content

Discord4J Store implementation backed by PostgreSQL and Redis

License

Notifications You must be signed in to change notification settings

CapybaraLabs/d4j-store

Repository files navigation

D4J Stores Release Coverage

Implementations of the Discord4J Store (Discord entity cache) including an extensive test suite.

Postgres

An implementation backed by everyone's favorite open sauce database PostgreSQL.

Gradle

repositories {
	maven { url "https://jitpack.io" }
}

dependencies {
	implementation "dev.capybaralabs.d4j-store:postgres:x.y.z"
}

Example Usage

fun connectionFactory(): ConnectionFactory {
	return ConnectionFactories.get(
		ConnectionFactoryOptions.builder()
			.option(ConnectionFactoryOptions.DRIVER, "postgresql")
			.option(ConnectionFactoryOptions.HOST, "127.0.0.1")
			.option(ConnectionFactoryOptions.PORT, 5432)
			.option(ConnectionFactoryOptions.USER, "cache")
			.option(ConnectionFactoryOptions.PASSWORD, "cache")
			.option(ConnectionFactoryOptions.DATABASE, "cache")
			.build()
	)
}

fun connectionPool(): ConnectionPool {
	val configuration = ConnectionPoolConfiguration.builder(connectionFactory())
		.maxIdleTime(Duration.ofMillis(1000))
		.maxSize(20)
		.maxAcquireTime(Duration.ofSeconds(5))
		.build()

	return ConnectionPool(configuration)
}

fun postgresStoreLayout(): StoreLayout {
	return PostgresStoreLayout(connectionPool())
}

fun store(): Store {
	return Store.fromLayout(postgresStoreLayout())
}

fun gatewayBootstrap(discordClient: DiscordClient): GatewayBootstrap<GatewayOptions> {
	return discordClient
		.gateway()
		.setStore(store())
		.withEventDispatcher { ed ->
			ed.on(Event::class.java)
				.doOnNext { logger().trace("Event received ${it.javaClass.simpleName}") }
				.retry()
		}
}

Redis

An implementation backed by everyone's second favorite open sauce database Redis.

Gradle

repositories {
	maven { url "https://jitpack.io" }
}

dependencies {
	implementation "dev.capybaralabs.d4j-store:redis:x.y.z"
}

Example Usage

fun connectionFactory(): ReactiveRedisConnectionFactory {
	val redisUri = RedisURI.create("redis://localhost:6379/0")
	val redisConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisUri)
	val connectionFactory = LettuceConnectionFactory(redisConfiguration)
	connectionFactory.afterPropertiesSet()

	return connectionFactory
}

fun redisStoreLayout(): StoreLayout {
	return RedisStoreLayout(connectionFactory)
}

fun store(): Store {
	return Store.fromLayout(redisStoreLayout())
}

fun gatewayBootstrap(discordClient: DiscordClient): GatewayBootstrap<GatewayOptions> {
	return discordClient
		.gateway()
		.setStore(store())
		.withEventDispatcher { ed ->
			ed.on(Event::class.java)
				.doOnNext { logger().trace("Event received ${it.javaClass.simpleName}") }
				.retry()
		}
}

SonarCloud