Skip to content

Instantly share code, notes, and snippets.

@shalk
Created March 8, 2021 11:12
Show Gist options
  • Save shalk/b83df0e8565fbd7c6ae784e51c15a483 to your computer and use it in GitHub Desktop.
Save shalk/b83df0e8565fbd7c6ae784e51c15a483 to your computer and use it in GitHub Desktop.
MyCacheWrapper
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.HashMap;
/**
* doc.
*
* @author shalk
* @since 2021
*/
class MyCache {
public Object get(String name, String key) {
return new Object();
}
}
public class MyCacheWrapper {
private static final Logger LOG = LoggerFactory.getLogger(MyCacheWrapper.class);
public static <T> T get(MyCache cache, String name, String key) {
try {
return (T) cache.get(name, key);
} catch (ClassCastException e) {
LOG.warn("cast fail", e);
return null;
}
}
public static void main(String[] args) {
MyCache cache = new MyCache();
final Object o = cache.get("aa", "bb");
if (o instanceof HashMap) {
HashMap<String, Date> b = (HashMap<String, Date>) o;
final Date aa = b.get("date1");
System.out.println("aa = " + aa);
}
HashMap<String, Date> b2 = MyCacheWrapper.get(cache, "aa", "bb");
final Date aa = b2.get("date1");
System.out.println("aa = " + aa);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment