redis springMVC 配置与应用

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业务层代码省略

posted on 2017-06-27 12:40  街机玩家  阅读(447)  评论(0编辑  收藏  举报

导航