Skip to content

Commit

Permalink
Added TTL to save operation (#7)
Browse files Browse the repository at this point in the history
* Added TTL to save operation
* Changed demo package names.
  • Loading branch information
gabheadz committed Mar 1, 2022
1 parent 2d8ca51 commit adee1cd
Show file tree
Hide file tree
Showing 51 changed files with 660 additions and 298 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=bancolombia_bin-stash&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=bancolombia_bin-stash)
[![codecov](https://codecov.io/gh/bancolombia/bin-stash/branch/master/graph/badge.svg)](https://codecov.io/gh/bancolombia/bin-stash)
[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/bancolombia/bin-stash/blob/master/LICENSE)
[![Scorecards supply-chain security](https://github.com/bancolombia/bin-stash/actions/workflows/scorecards-analysis.yml/badge.svg)](https://github.com/bancolombia/bin-stash/actions/workflows/scorecards-analysis.yml)

Library for caching data:

Expand All @@ -20,23 +19,23 @@ For local cache only

```gradle
dependencies {
implementation 'com.github.bancolombia:bin-stash-local:1.0.2'
implementation 'com.github.bancolombia:bin-stash-local:1.0.3'
}
```

For a centralized (redis) cache only

```gradle
dependencies {
implementation 'com.github.bancolombia:bin-stash-centralized:1.0.2'
implementation 'com.github.bancolombia:bin-stash-centralized:1.0.3'
}
```

For an hybrid (local and centralized) cache

```gradle
dependencies {
implementation 'com.github.bancolombia:bin-stash-hybrid:1.0.2'
implementation 'com.github.bancolombia:bin-stash-hybrid:1.0.3'
}
```

Expand All @@ -45,10 +44,8 @@ dependencies {
```yaml
stash:
memory:
expireTime: 10
maxSize: 10_000
redis:
expireTime: 60
host: myredis.host
port: 6379
database: 0
Expand Down
1 change: 0 additions & 1 deletion configuration/centralized-cache/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
dependencies {
compile project(':bin-stash-usecase')
compile project(':bin-stash-memory')
compile project(':bin-stash-redis')
implementation 'org.springframework:spring-core'
implementation 'org.springframework:spring-context'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CentralizedCacheConfig<V extends Object> {
@Value("${stash.redis.password:}")
private String password;

@Value("${stash.redis.expireTime:60}")
@Value("${stash.redis.expireTime:-1}")
private int redisExpireTime;

@Bean(name = "centralMemStashBean")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Configuration
public class HybridCacheConfig<V extends Object> {

@Value("${stash.memory.expireTime:60}")
@Value("${stash.memory.expireTime:-1}")
private int localExpireTime;

@Value("${stash.memory.maxSize:1000}")
Expand All @@ -36,7 +36,7 @@ public class HybridCacheConfig<V extends Object> {
@Value("${stash.redis.password:}")
private String password;

@Value("${stash.redis.expireTime:60}")
@Value("${stash.redis.expireTime:-1}")
private int redisExpireTime;

@Bean(name = "hybridMemStashBean")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Configuration
public class LocalCacheConfig<V extends Object> {

@Value("${stash.memory.expireTime:60}")
@Value("${stash.memory.expireTime:-1}")
private int expireTime;

@Value("${stash.memory.maxSize:1000}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.com.bancolombia.binstash.model;

public class InvalidValueException extends RuntimeException{
public InvalidValueException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package co.com.bancolombia.binstash.model;

/**
* Synchronization rule to be evaluated by the double tier cache in order to decide if a key/value
* should be synchronized upstream or downstream.
*/
@FunctionalInterface
public interface SyncRule {

/**
* Logic to decide if a key (keyArg) should be synchronized <pre>SyncType.UPSTREAM</pre> or
* <pre>SyncType.DOWNSTREAM</pre>, being the local cache always thw downstream and the centralized
* the downstream.
*
* @param keyArg the key to be evaluated
* @param syncType the sync to be evaluated. It is either <pre>SyncType.UPSTREAM</pre> or
* * <pre>SyncType.DOWNSTREAM</pre>
* @return true if the key should be propagated in the defined <pre>SyncType</pre> direction, false otherwise.
*/
boolean apply(String keyArg, SyncType syncType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Set;

/**
* Repo for storing Map&lt;String, String&gt; data.
* Repo for storing hash collections of data.
*/
public interface HashStash {

Expand All @@ -18,6 +18,15 @@ public interface HashStash {
*/
Mono<Map<String, String>> hSave(String key, Map<String, String> value);

/**
* Saves a Map value under key, indicating a Time to live for the data in the cache
* @param key key value to index map
* @param value map to store in cache
* @param ttl time to live in seconds
* @return inserted map.
*/
Mono<Map<String, String>> hSave(String key, Map<String, String> value, int ttl);

/**
* Adds/Updates fileds in map.
*
Expand All @@ -28,6 +37,17 @@ public interface HashStash {
*/
Mono<String> hSave(String key, String field, String value);

/**
* Adds/Updates fileds in map, possibly indicating a ttl for the key in the cache.
*
* @param key key value to index map
* @param field field to update/add into map
* @param value value to set field to
* @param ttl time to live in seconds
* @return field value updated/added in map.
*/
Mono<String> hSave(String key, String field, String value, int ttl);

/**
* Gets field value from map
*
Expand All @@ -44,6 +64,10 @@ public interface HashStash {
*/
Mono<Map<String, String>> hGetAll(String key);

/**
* Gets a set of all keys currently stored
* @return Set o f keys
*/
Mono<Set<String>> keySet();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,101 @@
import java.util.Set;

/**
* Cache for storing &lt;String, Map&gt; data.
* Cache API for storing multiple <pre>Map&lt;String, String&gt;</pre> data in cache. Implementor will use the <pre>HashStash</pre>
* as repository.
*/
public interface MapCache {

/**
* Stores the argument Map into the cache under the provided key.
* @param key the key under which the map should be stored into.
* @param value the map to store
* @return the original map.
*/
Mono<Map<String, String>> saveMap(String key, Map<String, String> value);

/**
* Stores the argument Map into the cache under the provided key, and setting the expiration
* of the key
* @param key the key under which the map should be stored into.
* @param value the map to store
* @param ttl the time to live of the key in the cache
* @return the original map.
*/
Mono<Map<String, String>> saveMap(String key, Map<String, String> value, int ttl);

/**
* Creates a new map, if it doesn't previously exist in the cache with the provided <pre>key</pre>, and stores
* the field and value into such map. If the map exists, then add/updates the field-value.
* @param key the key under which the map should be stored into.
* @param field the field to store
* @param value the value to store
* @return the original map.
*/
Mono<String> saveMap(String key, String field, String value);

/**
* Creates a new map, if it doesn't previously exist in the cache with the provided <pre>key</pre>, and stores
* the field and value into such map. If the map exists, then add/updates the field-value.
* @param key the key under which the map should be stored into.
* @param field the field to store
* @param value the value to store
* @param ttl the time to live of the key in the cache
* @return the original map.
*/
Mono<String> saveMap(String key, String field, String value, int ttl);

/**
* Fetches a value stored in a map in the cache.
* @param key the key under which the map exists in the cache.
* @param field the name of the field in the map.
* @return the string value associated to the field name.
*/
Mono<String> getMap(String key, String field);

/**
* Fetches the whole map stored under a key in the cache.
* @param key the key under which the map exists in the cache.
* @return the Map object.
*/
Mono<Map<String, String>> getMap(String key);

/**
* Checks whether a map is stored under a given key in the cache.
* @param key the key to check if exists in the cache.
* @return true if there is a map stored in the cache with such key, false otherwise.
*/
Mono<Boolean> existsMap(String key);

/**
* Checks whether field exists within a map stored under a given key in the cache.
* @param key the key to check if exists as a map in the cache.
* @param field the field value to check within the map.
* @return true if there is a map stored in the cache with such key, and a field with a given name in the map,
* or false otherwise.
*/
Mono<Boolean> existsMap(String key, String field);

/**
* obtains a Set with all keys stored in cache
* @return a Mono containing a Set of strings mapping each key that exists in cache.
* Retrieves all keys existing in the cache. Note this operation will return ALL keys existing in the underlying
* cache whether those keys represent maps or single key-values.
* @return a set of all keys.
*/
Mono<Set<String>> keySet();

/**
* Removes a map identified by key, effectively removing all field-value pairs.
* @param key the key under which the map is stored in the cache.
* @return true if the map was evicted, false otherwise.
*/
Mono<Boolean> evictMap(String key);

/**
* Removes a field from a map identified by key, effectively removing a single field-value pair.
* @param key the key under which the map is stored in the cache.
* @param field the field under which the value is stored in the map.
* @return true if the field-value was removed from map, false otherwise.
*/
Mono<Boolean> evictMap(String key, String field);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.Set;

/**
* Cache for storing &lt;String, T&gt; data.
* API for storing Objects in cache. Implementor will use <pre>Stash</pre> as a repository, and Objects will be
* serialized into <pre>String</pre> before invoking the appropriate <pre>Stash</pre> save operations, and deserialized
* back into the corresponding Type on get operations.
*/
public interface ObjectCache<T> {

Expand All @@ -17,6 +19,15 @@ public interface ObjectCache<T> {
*/
Mono<T> save(String key, T value);

/**
* Save value to cache, alternatively specifying a TTL for the key
* @param key key to index value
* @param value value to store
* @param ttl time key should live in cache
* @return value stored
*/
Mono<T> save(String key, T value, int ttl);

/**
* Gets an element from cache
* @param key key to which value was stored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
import java.util.Set;

/**
* Repo for storing key/value string data.
* Repo for storing key/value <pre>String</pre> data.
*/
public interface StringStash {

/**
* Saves a key-value in a repository
* @param key key to store related value
* @param value value to be stored
* @param ttl time the key should live in the stash
* @return the same value stored.
*/
Mono<String> save(String key, String value, int ttl);

/**
* Saves a key-value in a repository
* @param key key to store related value
Expand All @@ -24,12 +33,30 @@ public interface StringStash {
*/
Mono<String> get(String key);

/**
* Gets a set of all keys currently stored
* @return Set o f keys
*/
Mono<Set<String>> keySet();

/**
* Checks if a given key exists in the repository
* @param key the key to be checked.
* @return true if the key exists, false otherwise.
*/
Mono<Boolean> exists(String key);

/**
* Remove the specified key, and its value, from the repo, if such key exists.
* @param key the key to be evicted.
* @return true if the key and corresponding value were evicted.
*/
Mono<Boolean> evict(String key);

/**
* Prune whole repository, evicting all keys and its associated values.
* @return true if the process completed successfully, false otherwise.
*/
Mono<Boolean> evictAll();

}
Expand Down
Loading

0 comments on commit adee1cd

Please sign in to comment.