本地cache简单代码实现


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地cache工具
 *
 * @author yanek
 *
 */
public class LocalCache {

 private static Map<String, Object> cacheMap = new ConcurrentHashMap<String, Object>();

 public static void destoryCacheMap() {
  cacheMap = null;
 }

 public static Map<String, Object> getCacheMap() {
  return cacheMap;
 }

 public static void set(String key, Object values) {
  cacheMap.put(key, values);
 }

 public static Object get(String key) {
  return cacheMap.get(key);
 }

 public static String getString(String key) {
  return (String) cacheMap.get(key);
 }

 public static Object getToEmpty(String key) {
  Object o = cacheMap.get(key);
  if (o == null)
   return "";
  else
   return o;
 }

 public static void remove(String key) {
  cacheMap.remove(key);
 }

 public static void clear() {
  cacheMap.clear();
 }

 public static void main(String[] args) {
  
  
  LocalCache.set("001","001");
  LocalCache.set("002","002");
  LocalCache.set("003","003");
  
  System.out.println(LocalCache.get("001"));
  System.out.println(LocalCache.get("002"));
  System.out.println(LocalCache.get("003"));
  
  

 }

}

上一篇:C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义


下一篇:Pagekit – 现代化技术构建的轻量的 CMS 系统