From edc88bdb9a00e370a1c8be13220dc32f971432ab Mon Sep 17 00:00:00 2001 From: Braydon Date: Fri, 15 Dec 2023 04:24:53 -0500 Subject: [PATCH] feat: Add @TTL to allow expiration of keys --- .../braydon/feather/database/Repository.java | 2 +- .../database/impl/redis/RedisRepository.java | 18 +++++++++- .../database/impl/redis/annotation/TTL.java | 35 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/braydon/feather/database/impl/redis/annotation/TTL.java diff --git a/src/main/java/me/braydon/feather/database/Repository.java b/src/main/java/me/braydon/feather/database/Repository.java index 03c91d1..beeb90c 100644 --- a/src/main/java/me/braydon/feather/database/Repository.java +++ b/src/main/java/me/braydon/feather/database/Repository.java @@ -21,7 +21,7 @@ import java.util.Map; import java.util.UUID; /** - * A repository belonging to a {@link IDatabase}. + * A repository belonging to an {@link IDatabase}. * * @author Braydon * @param the database this repository uses diff --git a/src/main/java/me/braydon/feather/database/impl/redis/RedisRepository.java b/src/main/java/me/braydon/feather/database/impl/redis/RedisRepository.java index 217a46b..895b614 100644 --- a/src/main/java/me/braydon/feather/database/impl/redis/RedisRepository.java +++ b/src/main/java/me/braydon/feather/database/impl/redis/RedisRepository.java @@ -7,9 +7,12 @@ package me.braydon.feather.database.impl.redis; import io.lettuce.core.api.sync.RedisCommands; import lombok.NonNull; +import me.braydon.feather.common.Tuple; import me.braydon.feather.data.Document; import me.braydon.feather.database.Repository; +import me.braydon.feather.database.impl.redis.annotation.TTL; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -79,7 +82,20 @@ public class RedisRepository extends Repository { } for (E entity : entities) { // Set our entities Document document = new Document<>(entity); // Create a document from the entity - commands.hmset(keyPrefix + ":" + document.getKey(), document.toMappedData()); + String key = keyPrefix + ":" + document.getKey(); // The key of this entity + commands.hmset(key, document.toMappedData()); // Set the mapped document in the database + + // Handling @TTL annotations + for (Tuple tuple : document.getMappedData().values()) { + Field field = tuple.getLeft(); + if (!field.isAnnotationPresent(TTL.class)) { // Missing @TTL + continue; + } + long ttl = field.getAnnotation(TTL.class).value(); // Get the ttl value + if (ttl > 0L) { // Value is above zero, set it + commands.expire(key, ttl); + } + } } if (multi) { // Execute the commands in bulk commands.exec(); diff --git a/src/main/java/me/braydon/feather/database/impl/redis/annotation/TTL.java b/src/main/java/me/braydon/feather/database/impl/redis/annotation/TTL.java new file mode 100644 index 0000000..7312d9d --- /dev/null +++ b/src/main/java/me/braydon/feather/database/impl/redis/annotation/TTL.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Braydon (Rainnny). All rights reserved. + * + * For inquiries, please contact braydonrainnny@gmail.com + */ +package me.braydon.feather.database.impl.redis.annotation; + +import java.lang.annotation.*; + +/** + * Fields flagged with this annotation will + * expire after the amount of defined seconds. + * + * @author Braydon + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Documented @Inherited +public @interface TTL { + /** + * The time-to-live value. + *

+ * If the value is zero or + * below, then there will + * be no expiration. + *

+ * The key this TTL rule is assigned + * to will expire in the defined amount + * of seconds from when the key was updated. + *

+ * + * @return the value in seconds + */ + long value() default -1L; +} \ No newline at end of file