標籤:cond   void   老版本   code   actor   一個   file   .class   list   
1、建立一個ConstantUtil類
/**
 * redis設定檔名
 */
public final static String REDIS_FILE_NAME_CONFIG = "redis.properties";
2、pom檔案中填加
 <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.9.0</version>
</dependency>
3、配置一個redis設定檔
#redis伺服器IP
redis.addr=10.12.12.140
#redis的連接埠號碼 
redis.port=6379
#可用串連執行個體的最大數目,預設為8 如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis執行個體,則此時pool的狀態為exhausted(耗盡)。 
redis.maxActive=1024
#控制一個pool最多有多少個狀態為idle(空閑)的jedis執行個體,預設也是8 
redis.maxIdle=200
#等待可用串連的最大時間,單位毫秒,預設值為-1,表示永不逾時。如果超過等待時間,則直接拋出JedisConnectionException 
redis.maxWait=10000
#初始化串連池逾時時間
redis.timeOut=10000
4、編寫RedisUtil工具類
package com.aspire.prnp.redis;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aspire.prnp.util.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
 //訪問密碼 
// private static String AUTH = "root"; 
 
 //在borrow一個redis執行個體時,是否提前進行validate操作; 
 //如果為true,則得到的jedis執行個體均是可用的 
 private static boolean TEST_ON_BORROW = true; 
 
 private static JedisPool jedisPool = null;
 
 /**
 * 
 * method_name:setRedisParamters
 * date:2017年6月15日下午5:23:27
 * return_type:void
 * description:初始化Redis串連池
 */
 public static void setRedisParamters(String addr,int port,int timeOut,int maxActive,int maxIdle,int maxWait){
 try { 
 JedisPoolConfig config = new JedisPoolConfig();
 config.setMaxTotal(maxActive);//老版本是setMaxActive 
 config.setMaxIdle(maxIdle); 
 config.setMaxWaitMillis(maxWait);//老版本是maxMaxWait 
 config.setTestOnBorrow(TEST_ON_BORROW); 
 jedisPool = new JedisPool(config,addr,port,timeOut);//有密碼的時候傳入AUTH
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 
}
 
 /**
 * 
 * method_name:getJedis
 * date:2017年6月15日下午5:41:24
 * return_type:Jedis
 * description:擷取Jedis執行個體 
 */
 public synchronized static Jedis getJedis(){ 
 try { 
 if(jedisPool != null){ 
 Jedis resource = jedisPool.getResource(); 
 return resource; 
 }else{ 
 return null; 
 } 
 } catch (Exception e) { 
 e.printStackTrace(); 
 return null; 
 } 
 } 
 
 
 
 /**
 * 
 * method_name:returnResource
 * date:2017年6月15日下午5:41:36
 * return_type:void
 * description:釋放jedis資源 
 */
 public static void returnResource(Jedis jedis){ 
 if(jedis != null){ 
 jedis.close(); 
 } 
 } 
 /**
 * 
 * method_name:setString
 * date:2017年6月22日下午3:20:01
 * return_type:void
 * description:設定String
 */
 public static void setString(String key ,String value){ 
 try { 
 value = StringUtil.isEmpty(value) ? "" : value; 
 getJedis().set(key,value); 
 } catch (Exception e) { 
 logger.error("Set key error : "+e); 
 } 
 }
 /**
 * 
 * method_name:setString
 * date:2017年6月22日下午3:20:50
 * return_type:void
 * description:設定String及到期時間
 */
 public static void setString(String key ,int seconds,String value){ 
 try { 
 value = StringUtil.isEmpty(value) ? "" : value; 
 getJedis().setex(key, seconds, value); 
 } catch (Exception e) { 
 logger.error("Set keyex error : "+e); 
 } 
 }
 /**
 * 
 * method_name:getString
 * date:2017年6月22日下午3:24:32
 * return_type:String
 * description:
 */
 public static String getString(String key){ 
 if(getJedis() == null || !getJedis().exists(key)){ 
 return null; 
 } 
 return getJedis().get(key); 
 } 
 
 public static <T> void setList(String key ,List<T> list){ 
 try { 
 getJedis().set(key.getBytes(),ObjectTranscoder.serialize(list)); 
 } catch (Exception e) { 
 logger.error("Set key error : "+e); 
 } 
 }
 
 public static <T> List<T> getList(String key){
 if(getJedis() == null || !getJedis().exists(key.getBytes())){ 
 return null; 
 } 
 byte[] in = getJedis().get(key.getBytes()); 
 List<T> list = (List<T>) ObjectTranscoder.deserialize(in); 
 return list; 
 } 
 
 public static <T> void setMap(String key ,Map<String,T> map){ 
 try { 
 getJedis().set(key.getBytes(),ObjectTranscoder.serialize(map)); 
 } catch (Exception e) { 
 logger.error("Set key error : "+e); 
 } 
 }
 
 public static <T> Map<String,T> getMap(String key){ 
 if(getJedis() == null || !getJedis().exists(key.getBytes())){ 
 return null; 
 } 
 byte[] in = getJedis().get(key.getBytes()); 
 Map<String,T> map = (Map<String, T>) ObjectTranscoder.deserialize(in); 
 return map; 
 } 
}
5、應用執行個體
/**
 * 
 * method_name:queryTrafficCount
 * date:2017年6月19日下午4:46:00
 * return_type:Map<String,Object>
 * description:查詢首頁業務量及使用者數圖表
 */
@RequestMapping(value = "/report/queryTrafficCount.ajax")
@ResponseBody
public Map<String,Object> queryTrafficCount(String type){
long starTime = System.currentTimeMillis();
try{
Configuration c =ConfigurationHelper.getConfiguration(ConstantUtil.REDIS_FILE_NAME_CONFIG);
String addr = c.getString("redis.addr");
int port = c.getInt("redis.port");
int timeOut = c.getInt("redis.timeOut");
int maxActive = c.getInt("redis.maxActive");
int maxIdle = c.getInt("redis.maxIdle");
int maxWait = c.getInt("redis.maxWait");
RedisUtil.setRedisParamters(addr, port, timeOut, maxActive, maxIdle, maxWait);
Map<String, Object> data = null;
if("d".equals(type)){
data = RedisUtil.getMap("trafficCountForDay");
}else if("m".equals(type)){
data = RedisUtil.getMap("trafficCountForMonth");
}else if("y".equals(type)){
data = RedisUtil.getMap("trafficCountForYear");
}
if(data == null){
data = homePageReportService.getChart(type);
if("d".equals(type)){
RedisUtil.setMap("trafficCountForDay", data);
}else if("m".equals(type)){
RedisUtil.setMap("trafficCountForMonth", data);
}else if("y".equals(type)){
RedisUtil.setMap("trafficCountForYear", data);
}
}
long endTime = System.currentTimeMillis();
logger.info("查詢首頁業務量及使用者數controller耗時"+(endTime-starTime));
return data;
}catch(Exception e){
logger.error("查詢首頁業務量及使用者數", e);
return super.fail("查詢首頁業務量及使用者數");
}
}
/**
 * 
 * method_name:queryTrafficAreaFormCount
 * date:2017年6月22日上午11:13:19
 * return_type:Map<String,Object>
 * description:查詢業務量地區資料
 */
@RequestMapping(value = "/report/queryTrafficAreaFormCount.ajax")
@ResponseBody
public Map<String,Object> queryTrafficAreaFormCount(String type){
long starTime = System.currentTimeMillis();
PageVo pageData = null;
try{
Configuration c =ConfigurationHelper.getConfiguration(ConstantUtil.REDIS_FILE_NAME_CONFIG);
String addr = c.getString("redis.addr");
int port = c.getInt("redis.port");
int timeOut = c.getInt("redis.timeOut");
int maxActive = c.getInt("redis.maxActive");
int maxIdle = c.getInt("redis.maxIdle");
int maxWait = c.getInt("redis.maxWait");
RedisUtil.setRedisParamters(addr, port, timeOut, maxActive, maxIdle, maxWait);
Map<String, Object> data = null;
if("d".equals(type)){
data = RedisUtil.getMap("trafficAreaFormCountForDay");
}else if("m".equals(type)){
data = RedisUtil.getMap("trafficAreaFormCountForMonth");
}else if("y".equals(type)){
data = RedisUtil.getMap("trafficAreaFormCountForYear");
}
if(data == null){
data = homePageReportService.getAreaFormChart(type);
if("d".equals(type)){
RedisUtil.setMap("trafficAreaFormCountForDay", data);
}else if("m".equals(type)){
RedisUtil.setMap("trafficAreaFormCountForMonth", data);
}else if("y".equals(type)){
RedisUtil.setMap("trafficAreaFormCountForYear", data);
}
}
pageData = new PageVo(1, 200, 0).format();
pageData.setList((List<Map<String,Object>>)data.get("deliveryAreaData"));
pageData.setTotal(0);
long endTime = System.currentTimeMillis();
logger.info("查詢首頁業務量及使用者數controller耗時"+(endTime-starTime));
return pageData.pageModel();
}catch(Exception e){
logger.error("查詢首頁業務量及使用者數", e);
return super.fail("查詢首頁業務量及使用者數");
}
}
/**
 * 
 * method_name:queryTrafficAreaFormCount
 * date:2017年6月22日上午11:13:19
 * return_type:Map<String,Object>
 * description:查詢業務量企業資料
 */
@RequestMapping(value = "/report/queryTrafficExpFormCount.ajax")
@ResponseBody
public Map<String,Object> queryTrafficExpFormCount(String type){
long starTime = System.currentTimeMillis();
PageVo pageData = null;
try{
Configuration c =ConfigurationHelper.getConfiguration(ConstantUtil.REDIS_FILE_NAME_CONFIG);
String addr = c.getString("redis.addr");
int port = c.getInt("redis.port");
int timeOut = c.getInt("redis.timeOut");
int maxActive = c.getInt("redis.maxActive");
int maxIdle = c.getInt("redis.maxIdle");
int maxWait = c.getInt("redis.maxWait");
RedisUtil.setRedisParamters(addr, port, timeOut, maxActive, maxIdle, maxWait);
Map<String, Object> data = null;
if("d".equals(type)){
data = RedisUtil.getMap("trafficExpFormCountForDay");
}else if("m".equals(type)){
data = RedisUtil.getMap("trafficExpFormCountForMonth");
}else if("y".equals(type)){
data = RedisUtil.getMap("trafficExpFormCountForYear");
}
if(data == null){
data = homePageReportService.getExpFormChart(type);
if("d".equals(type)){
RedisUtil.setMap("trafficExpFormCountForDay", data);
}else if("m".equals(type)){
RedisUtil.setMap("trafficExpFormCountForMonth", data);
}else if("y".equals(type)){
RedisUtil.setMap("trafficExpFormCountForYear", data);
}
}
pageData = new PageVo(1, 200, 0).format();
pageData.setList((List<Map<String,Object>>)data.get("deliveryExpData"));
pageData.setTotal(0);
long endTime = System.currentTimeMillis();
logger.info("查詢首頁業務量及使用者數controller耗時"+(endTime-starTime));
return pageData.pageModel();
}catch(Exception e){
logger.error("查詢首頁業務量及使用者數", e);
return super.fail("查詢首頁業務量及使用者數");
}
}
ps service業務層代碼省略
redis springMVC 配置與應用