一、pom.xml
二、redis.properties
三、spring-redis.xml
四、工具类
1.RedisCache.java
2.RedisCacheTransfer
3.RedisUtil
五、测试
redis.clients
jedis
2.9.0
org.springframework.data
spring-data-redis
1.7.2.RELEASE
#ip\u5730\u5740
redis.hostName=172.20.10.6
#\u7AEF\u53E3\u53F7
redis.port=6379
#\u5982\u679C\u6709\u5BC6\u7801
redis.password=123456
#\u5BA2\u6237\u7AEF\u8D85\u65F6\u65F6\u95F4\u5355\u4F4D\u662F\u6BEB\u79D2 \u9ED8\u8BA4\u662F2000
redis.timeout=10000
##################### redis\u8FDE\u63A5\u6C60\u914D\u7F6E ###########################################
#\u6700\u5927\u7A7A\u95F2\u6570
redis.maxIdle=300
#\u8FDE\u63A5\u6C60\u7684\u6700\u5927\u6570\u636E\u5E93\u8FDE\u63A5\u6570\u3002\u8BBE\u4E3A0\u8868\u793A\u65E0\u9650\u5236,\u5982\u679C\u662Fjedis 2.4\u4EE5\u540E\u7528redis.maxTotal
#redis.maxActive=600
#\u63A7\u5236\u4E00\u4E2Apool\u53EF\u5206\u914D\u591A\u5C11\u4E2Ajedis\u5B9E\u4F8B,\u7528\u6765\u66FF\u6362\u4E0A\u9762\u7684redis.maxActive,\u5982\u679C\u662Fjedis 2.4\u4EE5\u540E\u7528\u8BE5\u5C5E\u6027
redis.maxTotal=1000
#\u6700\u5927\u5EFA\u7ACB\u8FDE\u63A5\u7B49\u5F85\u65F6\u95F4\u3002\u5982\u679C\u8D85\u8FC7\u6B64\u65F6\u95F4\u5C06\u63A5\u5230\u5F02\u5E38\u3002\u8BBE\u4E3A-1\u8868\u793A\u65E0\u9650\u5236\u3002
redis.maxWaitMillis=1000
#\u8FDE\u63A5\u7684\u6700\u5C0F\u7A7A\u95F2\u65F6\u95F4 \u9ED8\u8BA41800000\u6BEB\u79D2(30\u5206\u949F)
redis.minEvictableIdleTimeMillis=300000
#\u6BCF\u6B21\u91CA\u653E\u8FDE\u63A5\u7684\u6700\u5927\u6570\u76EE,\u9ED8\u8BA43
redis.numTestsPerEvictionRun=1024
#\u9010\u51FA\u626B\u63CF\u7684\u65F6\u95F4\u95F4\u9694(\u6BEB\u79D2) \u5982\u679C\u4E3A\u8D1F\u6570,\u5219\u4E0D\u8FD0\u884C\u9010\u51FA\u7EBF\u7A0B, \u9ED8\u8BA4-1
redis.timeBetweenEvictionRunsMillis=30000
#\u662F\u5426\u5728\u4ECE\u6C60\u4E2D\u53D6\u51FA\u8FDE\u63A5\u524D\u8FDB\u884C\u68C0\u9A8C,\u5982\u679C\u68C0\u9A8C\u5931\u8D25,\u5219\u4ECE\u6C60\u4E2D\u53BB\u9664\u8FDE\u63A5\u5E76\u5C1D\u8BD5\u53D6\u51FA\u53E6\u4E00\u4E2A
redis.testOnBorrow=true
#\u5728\u7A7A\u95F2\u65F6\u68C0\u67E5\u6709\u6548\u6027, \u9ED8\u8BA4false
redis.testWhileIdle=true
package cn.kaxlm6.ssm.redis;
import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class RedisCache implements Cache {
private static final Logger LOG = LoggerFactory.getLogger(RedisCache.class);
private final String id;
/**
* The {@code ReadWriteLock}.
*/
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
/**
* 使用中间类RedisCacheTransfer(此类配置在spring-redis.xml中)解决RedisTemplate的静态注入,从而使MyBatis实现第三方缓存
*/
private static RedisTemplate redisTemplate;
private JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
// private static RedisSerializer valueSerializer;
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
LOG.debug("RedisCache:id=" + id);
this.id = id;
}
public static void setRedisTemplate(RedisTemplate redisTemplate) {
RedisCache.redisTemplate = redisTemplate;
//valueSerializer = RedisCache.redisTemplate.getValueSerializer();
}
@Override
public String getId() {
return this.id;
}
@Override
public void putObject(Object key, Object value) {
if (value != null) {
LOG.info(">>>>>>>>>>>>>>>>>>>>>>>>putObject:");
LOG.info("[" + key + "]");
//redisTemplate.opsForValue().set(key.toString(), jdkSerializer.serialize(value), 2, TimeUnit.DAYS);
//byte[] bytes = valueSerializer.serialize(value);
//redisTemplate.opsForValue().set(key.toString(), bytes, 2, TimeUnit.DAYS);
redisTemplate.opsForValue().set(key.toString(), value, 2, TimeUnit.DAYS);
}
}
@Override
public Object getObject(Object key) {
try {
LOG.info(">>>>>>>>>>>>>>>>>>>>>>>>getObject:");
LOG.info("[" + key + "]");
if (key != null) {
return redisTemplate.opsForValue().get(key.toString());
}
} catch (Exception e) {
e.printStackTrace();
LOG.error("redis ");
}
return null;
}
@Override
public Object removeObject(Object key) {
try {
if (key != null) {
redisTemplate.expire(key.toString(), 1, TimeUnit.SECONDS);
}
} catch (Exception e) {
}
return null;
}
@Override
public void clear() {
Long size = redisTemplate.execute(new RedisCallback() {
@Override
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
Long size = connection.dbSize();
connection.flushDb();//连接清除数据
connection.flushAll();
return size;
}
});
LOG.info(">>>>>>>>>>>>>>>>>>>>>>>>clear: 清除了" + size + "个对象");
}
@Override
public int getSize() {
Long size = redisTemplate.execute(new RedisCallback() {
@Override
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
return connection.dbSize();
}
});
return size.intValue();
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
}
package cn.kaxlm6.ssm.redis;
import org.springframework.data.redis.core.RedisTemplate;
/**
* @描述: 静态注入中间类,解决RedisCache中RedisTemplate的静态注入,从而使MyBatis实现第三方缓存
*/
public class RedisCacheTransfer {
public void setRedisTemplate(RedisTemplate redisTemplate) {
RedisCache.setRedisTemplate(redisTemplate);
}
}
package cn.kaxlm6.ssm.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
/**
* 基于spring和redis的redisTemplate工具类
* 针对所有的hash 都是以h开头的方法
* 针对所有的Set 都是以s开头的方法 不含通用方法
* 针对所有的List 都是以l开头的方法
*/
public class RedisUtil {
private RedisTemplate redisTemplate;
public RedisUtil() {
}
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
//=============================common============================
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
//============================String=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key 键
* @param by 要增加几(大于0)
* @return
*/
public long incr(String key, long by) {
if (by < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, by);
}
/**
* 递减
*
* @param key 键
* @param by 要减少几(小于0)
* @return
*/
public long decr(String key, long by) {
if (by < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -by);
}
//================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map
Logger logger = LoggerFactory.getLogger(TestController.class);
@Resource
private HttpServletRequest request;
@Resource
private HttpSession session;
/**
* My工具类
*/
@Resource
private IXlmToolKit xlmToolKit;
/**
* 用户Service
*/
@Resource
private TbUserService tbUserService;
/**
* Redis工具类
*/
@Resource
private RedisUtil redisUtil;
@RequestMapping("/redisData")
public void redisData() {
// 1.先去缓存拿数据
// 2.如果没有那就去数据库拿数据
// 3.然后存入缓存和作用域
// 定义一个KEY
String key = "cn.kaxlm6.ssm.dao.TbUserDao.queryAll";
long l1 = System.currentTimeMillis();
// 1.先去缓存中取数据
Object o = redisUtil.get(key);
long l2 = System.currentTimeMillis();
// 2.判断是否为空,为空去数据库获取数据
if(o == null) {
long l3 = System.currentTimeMillis();
// 3.1.去数据库获取数据,并转换为json字符串格式
o = xlmToolKit.parseJackson(tbUserService.queryAll(null));
long l4 = System.currentTimeMillis();
logger.info(key + " Mysql 本次耗时 :" + (l4-l3) + " 毫秒");
// 3.2.存入redis
redisUtil.set(key, o);
} else {
logger.info(key + " Redis 本次耗时 :" + (l2-l1) + " 毫秒");
// 3.1.将json字符串转换为集合
List tbUsers = JSONArray.parseArray(o.toString(), TbUser.class);
o = xlmToolKit.parseJackson(tbUsers );
// 这一步仅仅只为展示一些处理方式
}
// 4.存入session
session.setAttribute(key, o);
// 5.写出到jsp
xlmToolKit.outJsp(o);
}