Skip to content

Instantly share code, notes, and snippets.

@theraccoonbear
Last active June 17, 2022 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theraccoonbear/9cd3ce43452470861fa8f8494397b5dd to your computer and use it in GitHub Desktop.
Save theraccoonbear/9cd3ce43452470861fa8f8494397b5dd to your computer and use it in GitHub Desktop.
Cache Key Interface
// 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