-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): offer default database impl
Signed-off-by: Jasper Lutz Severino <jasperlutzseverino@gmail.com>
- Loading branch information
1 parent
7f43001
commit fb6e820
Showing
6 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>discord-books-database</artifactId> | ||
<groupId>com.lutzseverino.discordbooks</groupId> | ||
<version>2.7.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<name>Discord Books Guava Database</name> | ||
<artifactId>discord-books-database-guava</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>discord-books-core</artifactId> | ||
<version>${project.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
30 changes: 30 additions & 0 deletions
30
database/guava/src/main/java/com/lutzseverino/discordbooks/database/guava/GuavaDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.lutzseverino.discordbooks.database.guava; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.lutzseverino.discordbooks.book.Book; | ||
import com.lutzseverino.discordbooks.database.BookDB; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class GuavaDB implements BookDB { | ||
private final Cache<String, Book> cache; | ||
|
||
public GuavaDB() { | ||
cache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(20, TimeUnit.MINUTES) | ||
.build(); | ||
} | ||
|
||
public GuavaDB(Cache<String, Book> cache) { | ||
this.cache = cache; | ||
} | ||
|
||
@Override public Book get(String key) { | ||
return cache.getIfPresent(key); | ||
} | ||
|
||
@Override public void set(String key, Book value) { | ||
cache.put(key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>discord-books</artifactId> | ||
<groupId>com.lutzseverino.discordbooks</groupId> | ||
<version>2.7.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>pom</packaging> | ||
|
||
<name>Discord Books Databases</name> | ||
<modules> | ||
<module>guava</module> | ||
<module>redis</module> | ||
</modules> | ||
<artifactId>discord-books-database</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>31.1-jre</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>4.2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.13.3</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>discord-books-database</artifactId> | ||
<groupId>com.lutzseverino.discordbooks</groupId> | ||
<version>2.7.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<name>Discord Books Redis Database</name> | ||
<artifactId>discord-books-database-redis</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>discord-books-core</artifactId> | ||
<version>${project.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
39 changes: 39 additions & 0 deletions
39
database/redis/src/main/java/com/lutzseverino/discordbooks/database/redis/JedisDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.lutzseverino.discordbooks.database.redis; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.lutzseverino.discordbooks.book.Book; | ||
import com.lutzseverino.discordbooks.database.BookDB; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.JedisPoolConfig; | ||
|
||
public class JedisDB implements BookDB { | ||
private final ObjectMapper mapper = new ObjectMapper(); | ||
private final JedisPool pool; | ||
|
||
public JedisDB() { | ||
this.pool = new JedisPool(new JedisPoolConfig(), "localhost", 6379); | ||
} | ||
|
||
public JedisDB(JedisPool pool) { | ||
this.pool = pool; | ||
} | ||
|
||
@Override public Book get(String key) { | ||
try (var jedis = pool.getResource()) { | ||
var json = jedis.get(key); | ||
return json == null ? null : mapper.readValue(json, Book.class); | ||
} catch (JsonProcessingException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
@Override public void set(String key, Book value) { | ||
try (var jedis = pool.getResource()) { | ||
jedis.set(key, mapper.writeValueAsString(value)); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters