Java中memcached 儲存 json與map相互轉化
阿新 • • 發佈:2019-01-10
1.Java通過memcache進行內容儲存
public String getSysDict(String key) { String memKey = "_getSysDict_key_" + key; String dictionaryStr = (String) memcachedClient.get(memKey); if (StringUtils.isNotBlank(dictionaryStr)) {// 快取讀取 return dictionaryStr; } else {// 資料庫讀取 List<DictionaryDto> list = iCmsSysDao.getDicts(key); String returnJson = JsonUtils.toJsonString(list); if (!list.isEmpty()) {// 放入快取 memcachedClient.set(memKey, returnJson, new Date(System.currentTimeMillis() + 1000 * 60 * 15)); } return returnJson; } }
2.memcached中獲取的字串轉化為map
boolean isRunning() throws Exception { String value = cmsService.getSysDict("PORTFOLIO_SPREAD_TIME"); Map map = JsonUtils.toJavaBeanList(value, new TypeReference<List<Map<String, String>>>() { }).get(0); String[] array = ((String) map.get("keyvalue")).split(","); String startTime = array[0]; String endTime = array[1]; Date now = new Date(); Date start = DateUtil.parseDate(startTime); Date end = DateUtil.parseDate(endTime); if (now.before(start) || now.after(end)) { return false; } else { return true; } }