增加redis
This commit is contained in:
parent
39565f716f
commit
bea04d5d39
|
@ -0,0 +1,7 @@
|
|||
ip=127.0.0.1
|
||||
port=6379
|
||||
password=
|
||||
max_active=500
|
||||
max_idle=5
|
||||
max_wait=10000
|
||||
timeout=10000
|
|
@ -21,10 +21,10 @@ import javax.servlet.http.HttpServletRequest;
|
|||
* @date 2016年10月15日
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/cache")
|
||||
public class CacheController extends BaseController {
|
||||
@RequestMapping("/ehcache")
|
||||
public class EhCacheController extends BaseController {
|
||||
|
||||
private static Logger _log = LoggerFactory.getLogger(CacheController.class);
|
||||
private static Logger _log = LoggerFactory.getLogger(EhCacheController.class);
|
||||
|
||||
private final static String EHCACHE_NAME = PropertiesFileUtil.getInstance().get("ehcache");
|
||||
|
||||
|
@ -34,8 +34,7 @@ public class CacheController extends BaseController {
|
|||
@RequestMapping("/test")
|
||||
@ResponseBody
|
||||
public Object test(HttpServletRequest request) {
|
||||
System.out.println(System.getProperty("java.io.tmpdir"));
|
||||
return "success";
|
||||
return System.getProperty("java.io.tmpdir");
|
||||
}
|
||||
|
||||
/**
|
|
@ -0,0 +1,82 @@
|
|||
package com.zheng.cms.controller;
|
||||
|
||||
import com.zheng.common.util.RedisUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 缓存controller
|
||||
*
|
||||
* @author shuzheng
|
||||
* @date 2016年11月26日
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/redis")
|
||||
public class RedisController extends BaseController {
|
||||
|
||||
private static Logger _log = LoggerFactory.getLogger(RedisController.class);
|
||||
|
||||
@RequestMapping("/test")
|
||||
@ResponseBody
|
||||
public Object test(HttpServletRequest request) {
|
||||
System.out.println(System.getProperty("java.io.tmpdir"));
|
||||
for (int i = 1; i <= 10000; i ++) {
|
||||
RedisUtil.set("key" + i, "value" + i, i * 5);
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增缓存记录
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/add")
|
||||
@ResponseBody
|
||||
public Object add(HttpServletRequest request) {
|
||||
String key = request.getParameter("key");
|
||||
String value = request.getParameter("value");
|
||||
String time = request.getParameter("time");
|
||||
RedisUtil.set(key, value, Integer.parseInt(time));
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存记录
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/remove")
|
||||
@ResponseBody
|
||||
public Object remove(HttpServletRequest request) {
|
||||
String key = request.getParameter("key");
|
||||
RedisUtil.remove(key);
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存记录
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/get")
|
||||
@ResponseBody
|
||||
public Object get(HttpServletRequest request) {
|
||||
String key = request.getParameter("key");
|
||||
String value = RedisUtil.get(key);
|
||||
if (null == value) {
|
||||
_log.debug("【redis】没有找到key={}的记录!", key);
|
||||
return "";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
|
@ -96,7 +96,7 @@
|
|||
<artifactId>mybatis-ehcache</artifactId>
|
||||
<version>${mybatis-ehcache.version}</version>
|
||||
</dependency>
|
||||
<!-- 数据库 -->
|
||||
<!-- mysql -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
|
@ -108,6 +108,12 @@
|
|||
<artifactId>druid</artifactId>
|
||||
<version>1.0.14</version>
|
||||
</dependency>
|
||||
<!-- redis -->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<!-- 日志 -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
|
|
|
@ -56,6 +56,16 @@ public class PropertiesFileUtil {
|
|||
}
|
||||
}
|
||||
|
||||
// 根据key读取value(整形)
|
||||
public Integer getInt(String key) {
|
||||
try {
|
||||
String value = resourceBundle.getString(key);
|
||||
return Integer.parseInt(value);
|
||||
}catch (MissingResourceException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Date getLoadTime() {
|
||||
return loadTime;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,179 @@
|
|||
package com.zheng.common.util;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* Redis 工具类
|
||||
* Created by shuzheng on 2016/11/26.
|
||||
*/
|
||||
public class RedisUtil {
|
||||
|
||||
protected static ReentrantLock lockPool = new ReentrantLock();
|
||||
protected static ReentrantLock lockJedis = new ReentrantLock();
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
|
||||
|
||||
//Redis服务器IP
|
||||
private static String IP = PropertiesFileUtil.getInstance("redis").get("ip");
|
||||
|
||||
//Redis的端口号
|
||||
private static int PORT = PropertiesFileUtil.getInstance("redis").getInt("port");
|
||||
|
||||
//访问密码
|
||||
private static String PASSWORD = PropertiesFileUtil.getInstance("redis").get("password");
|
||||
//可用连接实例的最大数目,默认值为8;
|
||||
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
|
||||
private static int MAX_ACTIVE = PropertiesFileUtil.getInstance("redis").getInt("max_active");
|
||||
|
||||
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
|
||||
private static int MAX_IDLE = PropertiesFileUtil.getInstance("redis").getInt("max_idle");
|
||||
|
||||
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
|
||||
private static int MAX_WAIT = PropertiesFileUtil.getInstance("redis").getInt("max_wait");
|
||||
|
||||
//超时时间
|
||||
private static int TIMEOUT = PropertiesFileUtil.getInstance("redis").getInt("timeout");
|
||||
|
||||
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
|
||||
private static boolean TEST_ON_BORROW = true;
|
||||
|
||||
private static JedisPool jedisPool = null;
|
||||
|
||||
/**
|
||||
* redis过期时间,以秒为单位
|
||||
*/
|
||||
public final static int EXRP_HOUR = 60 * 60; //一小时
|
||||
public final static int EXRP_DAY = 60 * 60 * 24; //一天
|
||||
public final static int EXRP_MONTH = 60 * 60 * 24 * 30; //一个月
|
||||
|
||||
/**
|
||||
* 初始化Redis连接池
|
||||
*/
|
||||
private static void initialPool() {
|
||||
try {
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
config.setMaxTotal(MAX_ACTIVE);
|
||||
config.setMaxIdle(MAX_IDLE);
|
||||
config.setMaxWaitMillis(MAX_WAIT);
|
||||
config.setTestOnBorrow(TEST_ON_BORROW);
|
||||
//jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT, PASSWORD);
|
||||
jedisPool = new JedisPool(config, IP.split(",")[0], PORT, TIMEOUT);
|
||||
} catch (Exception e) {
|
||||
logger.error("First create JedisPool error : " + e);
|
||||
try {
|
||||
//如果第一个IP异常,则访问第二个IP
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
config.setMaxTotal(MAX_ACTIVE);
|
||||
config.setMaxIdle(MAX_IDLE);
|
||||
config.setMaxWaitMillis(MAX_WAIT);
|
||||
config.setTestOnBorrow(TEST_ON_BORROW);
|
||||
//jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT, PASSWORD);
|
||||
jedisPool = new JedisPool(config, IP.split(",")[1], PORT, TIMEOUT);
|
||||
} catch (Exception e2) {
|
||||
logger.error("Second create JedisPool error : " + e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在多线程环境同步初始化
|
||||
*/
|
||||
private static synchronized void poolInit() {
|
||||
if (jedisPool == null) {
|
||||
initialPool();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 同步获取Jedis实例
|
||||
* @return Jedis
|
||||
*/
|
||||
public synchronized static Jedis getJedis() {
|
||||
if (jedisPool == null) {
|
||||
poolInit();
|
||||
}
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (jedisPool != null) {
|
||||
jedis = jedisPool.getResource();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Get jedis error : " + e);
|
||||
} finally {
|
||||
returnResource(jedis);
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放jedis资源
|
||||
* @param jedis
|
||||
*/
|
||||
public static void returnResource(final Jedis jedis) {
|
||||
if (jedis != null && jedisPool != null) {
|
||||
jedisPool.returnResource(jedis);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 String
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public synchronized static void set(String key, String value) {
|
||||
try {
|
||||
value = StringUtils.isEmpty(value) ? "" : value;
|
||||
getJedis().set(key, value);
|
||||
} catch (Exception e) {
|
||||
logger.error("Set key error : " + e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 过期时间
|
||||
* @param key
|
||||
* @param value
|
||||
* @param seconds 以秒为单位
|
||||
*/
|
||||
public synchronized static void set(String key, String value, int seconds) {
|
||||
try {
|
||||
value = StringUtils.isEmpty(value) ? "" : value;
|
||||
getJedis().setex(key, seconds, value);
|
||||
} catch (Exception e) {
|
||||
logger.error("Set keyex error : " + e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取String值
|
||||
* @param key
|
||||
* @return value
|
||||
*/
|
||||
public synchronized static String get(String key) {
|
||||
if (getJedis() == null || !getJedis().exists(key)) {
|
||||
return null;
|
||||
}
|
||||
return getJedis().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除值
|
||||
* @param key
|
||||
*/
|
||||
public synchronized static void remove(String key) {
|
||||
try {
|
||||
getJedis().del(key);
|
||||
} catch (Exception e) {
|
||||
logger.error("Remove keyex error : " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue