LocalCache:java
/** * Created by lis on 17/5/2. */ @Component public class LocalCache implements Serializable{ private static final long serialVersionUID = 1L; /** * 默認有效時長,單位:秒 */ private static final int DEFUALT_TIMEOUT = 3600; private static final long SECOND_TIME = 1000; private static final Map<String, Object> map; private static final Timer timer; /** * 初始化 */ static { timer = new Timer(); map = new ConcurrentHashMap<>(); } /** * 私有構造函數,工具類不容許實例化 */ private LocalCache() { } /** * 清除緩存任務類 */ static class CleanWorkerTask extends TimerTask { private String key; public CleanWorkerTask(String key) { this.key = key; } public void run() { LocalCache.remove(key); } } /** * 增長緩存 * * @param key * @param value */ public static void put(String key, Object value) { map.put(key, value); timer.schedule(new CleanWorkerTask(key), DEFUALT_TIMEOUT); } /** * 增長緩存 * * @param key * @param value * @param timeout 有效時長 */ public static void put(String key, Object value, int timeout) { map.put(key, value); timer.schedule(new CleanWorkerTask(key), timeout * SECOND_TIME); } /** * 增長緩存 * * @param key * @param value * @param expireTime 過時時間 */ public static void put(String key, Object value, Date expireTime) { map.put(key, value); timer.schedule(new CleanWorkerTask(key), expireTime); } /** * 批量增長緩存 * * @param m */ public static void putAll(Map<String, Object> m) { map.putAll(m); for (String key : m.keySet()) { timer.schedule(new CleanWorkerTask(key), DEFUALT_TIMEOUT); } } /** * 批量增長緩存 * * @param m */ public static void putAll(Map<String, Object> m, int timeout) { map.putAll(m); for (String key : m.keySet()) { timer.schedule(new CleanWorkerTask(key), timeout * SECOND_TIME); } } /** * 批量增長緩存 * * @param m */ public static void putAll(Map<String, Object> m, Date expireTime) { map.putAll(m); for (String key : m.keySet()) { timer.schedule(new CleanWorkerTask(key), expireTime); } } /** * 獲取緩存 * * @param key * @return */ public static Object get(String key) { return map.get(key); } /** * 查詢緩存是否包含key * * @param key * @return */ public static boolean containsKey(String key) { return map.containsKey(key); } /** * 刪除緩存 * * @param key */ public static void remove(String key) { map.remove(key); } /** * 刪除緩存 * * @param o */ public static void remove(Object o) { map.remove(o); } /** * 返回緩存大小 * * @return */ public static int size() { return map.size(); } /** * 獲取全部緩存數據 * * @return Map */ public static Map<String, Object> getAllCacheData(){ return map; } /** * 清除全部緩存 * * @return */ public static void clear() { if (size() > 0) { map.clear(); } timer.cancel(); } }
本地緩存CRUD測試web
CacheController:spring
import cn.jiguang.utils.DateUtils; import cn.jiguang.utils.LocalCache; import com.alibaba.fastjson.JSON; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.Map; /** * @desc: 測試緩存 * @author: zengxc * @date: 2018/3/22 */ @RequestMapping(value = "/cache") @RestController public class CacheController { private static final Logger LOG = LoggerFactory.getLogger(CacheController.class); @Autowired private LocalCache localCache; @RequestMapping(method = RequestMethod.POST) public ResponseEntity<?> insertCache(@RequestBody Map<String, Object> map){ try { if (!CollectionUtils.isEmpty(map)){ map.forEach((k,v)-> { localCache.put(k, v, DateUtils.getNextDay(new Date())); }); } LOG.info("request param map:{} cache:{}", JSON.toJSONString(map), JSON.toJSONString(localCache.getAllCacheData())); return ResponseEntity.status(HttpStatus.CREATED).body(null); } catch (Exception e) { LOG.error("exception info:{}", e.getMessage()); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } @RequestMapping(method = RequestMethod.GET) public ResponseEntity<?> getAllCacheData(@RequestParam(value = "key", required = false) String key){ try { if (!StringUtils.isEmpty(key)){ LOG.info("get cache value:{}", JSON.toJSONString(localCache.get(key))); return ResponseEntity.status(HttpStatus.OK).body(localCache.get(key)); }else { LOG.info("get cache value:{}", JSON.toJSONString(localCache.getAllCacheData())); return ResponseEntity.ok(localCache.getAllCacheData()); } } catch (Exception e) { LOG.error("exception info:{}", e.getMessage()); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } @RequestMapping(method = RequestMethod.PUT) public ResponseEntity<?> updateCache(String key, @RequestBody Object value){ try { localCache.put(key, value); return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } catch (Exception e) { LOG.error("exception info:{}", e.getMessage()); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } @RequestMapping(method = RequestMethod.DELETE) public ResponseEntity<?> deleteCache(@RequestParam("key") String key){ try { localCache.remove(key); return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } catch (Exception e) { LOG.error("exception info:{}", e.getMessage()); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } }
DateUtils工具類:json
/** * @desc: * @author: zengxc * @date: 2018/3/23 */ public class DateUtils { /** * 獲取當前系統時間的下一天 * @param date * @return */ public static Date getNextDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, 1); date = calendar.getTime(); return date; } }