Commit 288b6cfc authored by Krishna Nikhil Vedurumudi's avatar Krishna Nikhil Vedurumudi
Browse files

Created an interface for Redis extensions on ICache interface

parent fbecf868
Pipeline #92149 passed with stages
in 9 minutes and 37 seconds
/*
* Copyright 2022 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.core.common.cache;
/**
* Interface that extends ICache with Redis specific functionalities.
* @param <K>
* @param <O>
*/
public interface IRedisCache<K, O> extends ICache<K, O> {
/**
* Puts entry in cache with ttl measured in milliseconds
*/
void put(K key, long ttl, O value);
/**
* Updates a key's ttl in milliseconds
*/
boolean updateTtl(K key, long ttl);
/**
* Gets the ttl for a key in milliseconds
*/
long getTtl(K key);
/**
* Gets redis INFO
*/
String info();
/**
* Increment the integer value of a key by one
*/
Long increment(K key);
/**
* Increment the integer value of a key by the given amount
*/
Long incrementBy(K key, long amount);
/**
* Decrement the integer value of a key by one
*/
Long decrement(K key);
/**
* Decrement the integer value of a key by the given amount
*/
Long decrementBy(K key, long amount);
}
......@@ -18,14 +18,13 @@ import com.lambdaworks.redis.ClientOptions;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.SetArgs;
import com.lambdaworks.redis.SocketOptions;
import com.lambdaworks.redis.api.StatefulRedisConnection;
import com.lambdaworks.redis.api.sync.RedisCommands;
import com.lambdaworks.redis.codec.CompressionCodec;
import java.util.concurrent.TimeUnit;
public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
public class RedisCache<K, V> implements IRedisCache<K, V>, AutoCloseable {
private final StatefulRedisConnection<K, V> connection;
private final RedisClient client;
......@@ -87,6 +86,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Puts entry in cache with ttl measured in milliseconds
*/
@Override
public void put(K key, long ttl, V value) {
SetArgs args = new SetArgs();
args.px(ttl);
......@@ -119,6 +119,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Updates a key's ttl in milliseconds
*/
@Override
public boolean updateTtl(K key, long ttl) {
return commands.pexpire(key, ttl);
}
......@@ -126,6 +127,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Gets the ttl for a key in milliseconds
*/
@Override
public long getTtl(K key) {
return commands.pttl(key);
}
......@@ -133,6 +135,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Gets redis INFO
*/
@Override
public String info() {
return commands.info();
}
......@@ -140,6 +143,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Increment the integer value of a key by one
*/
@Override
public Long increment(K key) {
return this.incrementBy(key, 1L);
}
......@@ -147,6 +151,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Increment the integer value of a key by the given amount
*/
@Override
public Long incrementBy(K key, long amount) {
return commands.incrby(key, amount);
}
......@@ -154,6 +159,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Decrement the integer value of a key by one
*/
@Override
public Long decrement(K key) {
return this.decrementBy(key, 1L);
}
......@@ -161,6 +167,7 @@ public class RedisCache<K, V> implements ICache<K, V>, AutoCloseable {
/**
* Decrement the integer value of a key by the given amount
*/
@Override
public Long decrementBy(K key, long amount) {
return commands.decrby(key, amount);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment