Last active
June 17, 2022 18:15
-
-
Save theraccoonbear/9cd3ce43452470861fa8f8494397b5dd to your computer and use it in GitHub Desktop.
Cache Key Interface
This file contains 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
// Things used as keys must implement this | |
public interface Cacheable { | |
public abstract String cacheKey(); | |
} | |
// This is the cache wrapper (it would get an injected CacheManager to use internally) | |
public abstract class GenericCacheableCache<K extends Cacheable, V> { | |
private CacheManager cache; | |
public Optional<V> get(K key) { | |
return Optional.of((V)this.cache.get(key.cacheKey())); | |
} | |
public void put(K key, V value) { | |
return this.cache.put(key.cacheKey(), value); | |
} | |
} | |
// This is the key we actually use | |
public class ProductKey implements Cacheable { | |
private String id; | |
private Region region; | |
public String cacheKey() { | |
return id + ":" + region.getValue(); | |
} | |
} | |
// This is the thing we would then work with to manage product cache | |
public class CacheableProductCache extends GenericCacheableCache<ProductKey, Product> { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment