feat: Add @TTL to allow expiration of keys

This commit is contained in:
Braydon 2023-12-15 04:24:53 -05:00
parent 0f75a1c261
commit edc88bdb9a
3 changed files with 53 additions and 2 deletions

@ -21,7 +21,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
* A repository belonging to a {@link IDatabase}. * A repository belonging to an {@link IDatabase}.
* *
* @author Braydon * @author Braydon
* @param <D> the database this repository uses * @param <D> the database this repository uses

@ -7,9 +7,12 @@ package me.braydon.feather.database.impl.redis;
import io.lettuce.core.api.sync.RedisCommands; import io.lettuce.core.api.sync.RedisCommands;
import lombok.NonNull; import lombok.NonNull;
import me.braydon.feather.common.Tuple;
import me.braydon.feather.data.Document; import me.braydon.feather.data.Document;
import me.braydon.feather.database.Repository; 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -79,7 +82,20 @@ public class RedisRepository<ID, E> extends Repository<Redis, ID, E> {
} }
for (E entity : entities) { // Set our entities for (E entity : entities) { // Set our entities
Document<String> document = new Document<>(entity); // Create a document from the entity Document<String> 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<Field, String> 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 if (multi) { // Execute the commands in bulk
commands.exec(); commands.exec();

@ -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.
* <p>
* If the value is zero or
* below, then there will
* be no expiration.
* <p>
* The key this TTL rule is assigned
* to will expire in the defined amount
* of seconds from when the key was updated.
* </p>
*
* @return the value in seconds
*/
long value() default -1L;
}