forked from ardoco/lissa
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement RestRedisCache and RedisClient for REST-based Redis s… #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielDango
wants to merge
1
commit into
feature/add-cache-configurations
Choose a base branch
from
feature/add-rest-cache
base: feature/add-cache-configurations
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
39 changes: 39 additions & 0 deletions
39
src/main/java/edu/kit/kastel/sdq/lissa/ratlr/cache/RedisAdapter.java
This file contains hidden or 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 @@ | ||
| /* Licensed under MIT 2026. */ | ||
| package edu.kit.kastel.sdq.lissa.ratlr.cache; | ||
|
|
||
| import redis.clients.jedis.UnifiedJedis; | ||
|
|
||
| public class RedisAdapter implements UnifiedRedisClient { | ||
|
|
||
| private final UnifiedJedis jedis; | ||
|
|
||
| RedisAdapter(UnifiedJedis jedis) { | ||
| this.jedis = jedis; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean ping() { | ||
| // TODO Find out what ping should return | ||
| return jedis.ping() != null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean exists(String key) { | ||
| return jedis.exists(key); | ||
| } | ||
|
|
||
| @Override | ||
| public String hget(String key, String field) { | ||
| return jedis.hget(key, field); | ||
| } | ||
|
|
||
| @Override | ||
| public long hset(String key, String field, String value) { | ||
| return jedis.hset(key, field, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| jedis.close(); | ||
| } | ||
| } |
This file contains hidden or 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
38 changes: 38 additions & 0 deletions
38
src/main/java/edu/kit/kastel/sdq/lissa/ratlr/cache/RestRedisAdapter.java
This file contains hidden or 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 @@ | ||
| /* Licensed under MIT 2026. */ | ||
| package edu.kit.kastel.sdq.lissa.ratlr.cache; | ||
|
|
||
| import org.fuchss.restredis.client.Client; | ||
|
|
||
| public class RestRedisAdapter implements UnifiedRedisClient { | ||
|
|
||
| private final Client restRedisClient; | ||
|
|
||
| RestRedisAdapter(Client restRedisClient) { | ||
| this.restRedisClient = restRedisClient; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean ping() { | ||
| return restRedisClient.ping(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean exists(String key) { | ||
| return restRedisClient.exists(key); | ||
| } | ||
|
|
||
| @Override | ||
| public String hget(String key, String field) { | ||
| return restRedisClient.hget(key, field); | ||
| } | ||
|
|
||
| @Override | ||
| public long hset(String key, String field, String value) { | ||
| return restRedisClient.hset(key, field, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| restRedisClient.close(); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/edu/kit/kastel/sdq/lissa/ratlr/cache/RestRedisCache.java
This file contains hidden or 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,49 @@ | ||
| /* Licensed under MIT 2026. */ | ||
| package edu.kit.kastel.sdq.lissa.ratlr.cache; | ||
|
|
||
| import org.fuchss.restredis.client.Client; | ||
| import org.fuchss.restredis.client.ClientConfiguration; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| import edu.kit.kastel.sdq.lissa.ratlr.utils.Environment; | ||
|
|
||
| public class RestRedisCache<K extends CacheKey> extends RedisCache<K> { | ||
|
|
||
| /** | ||
| * Creates a new Rest Redis cache instance. | ||
| * This constructor will throw an exception if Rest Redis is unavailable. | ||
| * | ||
| * @param cacheParameter The cache parameter configuration | ||
| * @param mapper The ObjectMapper for JSON operations | ||
| * @throws IllegalArgumentException If Redis connection cannot be established | ||
| */ | ||
| RestRedisCache(CacheParameter<K> cacheParameter, ObjectMapper mapper) { | ||
| super(cacheParameter, mapper, createRedisConnection()); | ||
| } | ||
|
|
||
| private static UnifiedRedisClient createRedisConnection() { | ||
| String restRedisUri = "localhost"; | ||
| if (Environment.getenv("REST_REDIS_URI") != null) { | ||
| restRedisUri = Environment.getenv("REST_REDIS_URI"); | ||
| } | ||
| String restRedisUsername = "admin"; | ||
| if (Environment.getenv("REST_REDIS_USERNAME") != null) { | ||
| restRedisUsername = Environment.getenv("REST_REDIS_USERNAME"); | ||
| } | ||
| String restRedisPassword = "dummy"; | ||
| if (Environment.getenv("REST_REDIS_PASSWORD") != null) { | ||
| restRedisPassword = Environment.getenv("REST_REDIS_PASSWORD"); | ||
| } | ||
|
|
||
| ClientConfiguration config = new ClientConfiguration(restRedisUri, restRedisUsername, restRedisPassword); | ||
| UnifiedRedisClient redis = new RestRedisAdapter(new Client(config)); | ||
|
|
||
| // Check if connection is working | ||
| if (!redis.ping()) { | ||
| throw new IllegalStateException("Could not connect to redis"); | ||
|
DanielDango marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return redis; | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/main/java/edu/kit/kastel/sdq/lissa/ratlr/cache/UnifiedRedisClient.java
This file contains hidden or 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,14 @@ | ||
| /* Licensed under MIT 2026. */ | ||
| package edu.kit.kastel.sdq.lissa.ratlr.cache; | ||
|
|
||
| public interface UnifiedRedisClient { | ||
| boolean ping(); | ||
|
|
||
| boolean exists(String key); | ||
|
|
||
| String hget(String key, String field); | ||
|
|
||
| long hset(String key, String field, String value); | ||
|
|
||
| void close(); | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/test/java/edu/kit/kastel/sdq/lissa/ratlr/cache/RestRedisIntegrationTest.java
This file contains hidden or 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,59 @@ | ||
| /* Licensed under MIT 2026. */ | ||
| package edu.kit.kastel.sdq.lissa.ratlr.cache; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| import edu.kit.kastel.sdq.lissa.ratlr.cache.classifier.ClassifierCacheKey; | ||
| import edu.kit.kastel.sdq.lissa.ratlr.cache.classifier.ClassifierCacheParameter; | ||
|
|
||
| /** | ||
| * Integration test for the REST Redis interface. | ||
| * TODO: maybe testcontainer or something? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can look into the rest-redis repo for examples with testcontainers |
||
| */ | ||
| public class RestRedisIntegrationTest { | ||
|
|
||
| RestRedisCache<ClassifierCacheKey> restCache; | ||
| private final ClassifierCacheParameter cacheParameter = new ClassifierCacheParameter("test", 1, 0.0); | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| restCache = new RestRedisCache<>(cacheParameter, new ObjectMapper()); | ||
|
Comment on lines
+27
to
+29
|
||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test REST Redis client connection") | ||
| void testRestRedisConnection() { | ||
| Cache<ClassifierCacheKey> cache = Cache.createByType( | ||
| "REST_REDIS", new ClassifierCacheParameter("test", 1, 0.0), null, new ObjectMapper()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test REST Redis cache set and get") | ||
| void testRestRedisCacheSetAndGet() { | ||
| restCache.put("key", "value"); | ||
| String value = restCache.get("key", String.class); | ||
| assertEquals("value", value); | ||
| String nonExistingValue = restCache.get("ajhosadljhjyhxcjkhljysdhjk", String.class); | ||
| assertNull(nonExistingValue); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Test if key exists") | ||
| void testExistsMethod() { | ||
| restCache.put("key", "value"); | ||
| String value = restCache.get("key", String.class); | ||
| assertEquals("value", value); | ||
| ClassifierCacheKey cacheKey = cacheParameter.createCacheKey("key"); | ||
| assertTrue(restCache.exists(cacheKey.toJsonKey())); | ||
| assertFalse(restCache.exists("nonExistingKey")); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.