上帝大脸 发表于 2017-12-21 16:32:00

spring + redis 实例(一)

package com.lcc.cache.redis;  

  
import java.io.IOException;
  
import java.math.BigDecimal;
  
import java.util.ArrayList;
  
import java.util.Date;
  
import java.util.HashMap;
  
import java.util.List;
  
import java.util.Map;
  

  
import org.joda.time.LocalDate;
  
import org.joda.time.LocalDateTime;
  

  
import com.alibaba.dubbo.common.utils.StringUtils;
  
import com.fasterxml.jackson.core.JsonParseException;
  
import com.fasterxml.jackson.core.JsonProcessingException;
  
import com.fasterxml.jackson.databind.JsonMappingException;
  
import com.fasterxml.jackson.databind.ObjectMapper;
  
import com.lcc.api.dto.TruckCacheDto;
  


  
public>  

  
   private RedisUtil redisUtil;
  

  
   private int cacheDay = 1;
  

  
   private String prefix = "truckCache:";
  


  
   public TruckCacheDto getTruckById(String>
  
         Map<Object, Object> map = redisUtil.getHashValue(prefix +>
  
         map.put("id",>  
         TruckCacheDto dto = new TruckCacheDto();
  
         prepareDto(map, dto);
  
         return dto;
  
   }
  

  
   private void prepareDto(Map<Object, Object> map, TruckCacheDto dto)
  
             throws JsonParseException, JsonMappingException, IOException {
  
         dto.setId(map.get("id").toString());
  
         if (map.get("lat") != null) {
  
             dto.setLat(Double.parseDouble(map.get("lat").toString()));
  
         }
  
         if (map.get("lng") != null) {
  
             dto.setLng(Double.parseDouble(map.get("lng").toString()));
  
         }
  
         if (map.get("temp") != null) {
  
             ObjectMapper co = new ObjectMapper();
  
             String temp = map.get("temp").toString();
  
             List tList = co.readValue(temp, List.class);
  
             List<Double> tempList = new ArrayList();
  
             if (tList == null) {
  
               tList = new ArrayList();
  
             }
  
             for (Object o : tList) {
  
               if (o != null) {
  
                     tempList.add(new BigDecimal(o.toString()).doubleValue());
  
               }
  
             }
  
             dto.setTempList(tempList);
  
         }
  
         if (map.get("time") != null) {
  
             dto.setTime(new Date(Long.valueOf(map.get("time").toString())));
  
         }
  
         if (map.get("address") != null) {
  
             dto.setAddress(map.get("address").toString());
  
         }
  
   }
  


  
   public void setLocation(String>  
         Map<Object, Object> map = new HashMap<Object, Object>();
  
         map.put("lat", lat);
  
         map.put("lng", lng);
  
         map.put("time", LocalDateTime.now().toDate().getTime());
  
         if (StringUtils.isNotEmpty(address)) {
  
             map.put("address", address);
  
         }

  
         redisUtil.setHashValue(prefix +>  
   }
  


  
   public <T> void setTemp(String>  
         Map<Object, Object> map = new HashMap<Object, Object>();
  
         ObjectMapper co = new ObjectMapper();
  
         String temp = co.writeValueAsString(tempList);
  
         map.put("temp", temp);
  
         map.put("time", LocalDateTime.now().toDate().getTime());

  
         redisUtil.setHashValue(prefix +>  
   }
  


  
   public <T> void setTempAndLocation(String>  
             throws JsonProcessingException {
  
         Map<Object, Object> map = new HashMap<Object, Object>();
  
         map.put("lat", lat);
  
         map.put("lng", lng);
  
         ObjectMapper co = new ObjectMapper();
  
         String temp = co.writeValueAsString(tempList);
  
         map.put("temp", temp);
  
         map.put("time", LocalDateTime.now().toDate().getTime());
  
         if (StringUtils.isNotEmpty(address)) {
  
             map.put("address", address);
  
         }

  
         redisUtil.setHashValue(prefix +>  
   }
  


  
   public void delete(String>
  
         redisUtil.delete(prefix +>  
   }
  

  
   private Date getExpireDate() {
  
         return LocalDate.now().plusDays(cacheDay).toDate();
  
   }
  

  
   public RedisUtil getRedisUtil() {
  
         return redisUtil;
  
   }
  

  
   public void setRedisUtil(RedisUtil redisUtil) {
  
         this.redisUtil = redisUtil;
  
   }
  

  
   public int getCacheDay() {
  
         return cacheDay;
  
   }
  

  
   public void setCacheDay(int cacheDay) {
  
         this.cacheDay = cacheDay;
  
   }
  

  
}
页: [1]
查看完整版本: spring + redis 实例(一)