package com.test.cache;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import org.apache.log4j.Logger;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.test.utils.MD5;
import redis.clients.jedis.exceptions.JedisConnectionException;
public class RedisCache implements Cache {
private static final Logger logger = Logger.getLogger(RedisCache.class);
private static JedisConnectionFactory jedisConnectionFactory;
private final String id;
/**
* The {@code ReadWriteLock}.
*/
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
logger.debug(">>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id=" + id);
this.id = id;
}
@Override
public void clear() {
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
connection.flushDb();
connection.flushAll();
logger.debug("出现CUD操作,清空对应Mapper缓存======>");
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
@Override
public String getId() {
return this.id;
}
@Override
public Object getObject(Object key) {
Object result = null;
JedisConnection connection = null;
try {
key = MD5.getMD5Str(key.toString());
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
result = serializer.deserialize(connection.get(serializer.serialize(key)));
if (result == null) {
removeObject(key);
return null;
}
logger.debug("从缓存中获取-----key=:" + key);
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return result;
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
@Override
public int getSize() {
int result = 0;
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
result = Integer.valueOf(connection.dbSize().toString());
logger.debug(this.id + "---->>>>总缓存数:" + result);
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return result;
}
@Override
public void putObject(Object key, Object value) {
if (value.toString().equals("[]")) {
logger.info("key:="+key+"--- value 为空 :" + value);
return;
}
JedisConnection connection = null;
try {
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
key = MD5.getMD5Str(key.toString());
connection = jedisConnectionFactory.getConnection();
connection.set(serializer.serialize(key), serializer.serialize(value));
logger.debug("添加缓存--------key:=" + key + " value:=" + value);
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
@Override
public Object removeObject(Object key) {
JedisConnection connection = null;
Object result = null;
try {
key = MD5.getMD5Str(key.toString());
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
result = connection.expire(serializer.serialize(key), 0);
logger.debug("LRU算法从缓存中移除-----" + this.id + " key" + key);
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
return result;
}
public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
RedisCache.jedisConnectionFactory = jedisConnectionFactory;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ssmr.zip (49个子文件)
user_t.sql 371B
ssmr
.project 1KB
pom.xml 5KB
target
test-classes
com
test
testmybatis
TestMyBatis.class 2KB
m2e-wtp
web-resources
META-INF
maven
ssmr
ssmr
pom.xml 5KB
pom.properties 195B
classes
spring-mybatis.xml 4KB
log4j.properties 1KB
com
test
utils
MD5.class 3KB
dao
TestDao.class 223B
cache
RedisCache.class 6KB
RedisCacheTransfer.class 631B
controller
TestController.class 1KB
service
impl
TestServiceImpl.class 959B
TestService.class 228B
pojo
User.class 989B
mapping
UserMapper.xml 963B
spring-mvc.xml 3KB
jdbc.properties 178B
mybatis-cache.xml 3KB
redis.properties 162B
.settings
org.eclipse.wst.common.project.facet.core.prefs.xml 321B
.jsdtscope 639B
org.eclipse.core.resources.prefs 86B
org.eclipse.wst.common.component 668B
org.eclipse.wst.validation.prefs 50B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.common.project.facet.core.xml 346B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
test
resources
java
com
test
testmybatis
TestMyBatis.java 1KB
main
resources
spring-mybatis.xml 4KB
log4j.properties 1KB
mapping
UserMapper.xml 963B
spring-mvc.xml 3KB
jdbc.properties 178B
mybatis-cache.xml 3KB
redis.properties 162B
java
com
test
utils
MD5.java 2KB
dao
TestDao.java 160B
cache
RedisCacheTransfer.java 319B
RedisCache.java 4KB
controller
TestController.java 700B
service
TestService.java 163B
impl
TestServiceImpl.java 532B
pojo
User.java 605B
webapp
WEB-INF
web.xml 2KB
.classpath 1KB
共 49 条
- 1
资源评论
- qq_360576412018-05-16报错了,404,No mapping found for HTTP request with URI [/ssmr/] in DispatcherServlet with name 'Springmvc',楼主啥意思呢,扫面包没问题啊
- caoyongqiangsm2017-12-04下载后,报错了。liu9751232017-12-05启动报错可能是因为你没有修改redis和mysql的连接信息
- chensuifeng2017-09-04挺好的,值得学习
- 致阳2017-07-26下载后,报错了。liu9751232017-12-05启动报错可能是因为你没有修改redis和mysql的连接信息
- yeaung202017-07-06暂时没有用到,但值得学习,谢谢分享
liu975123
- 粉丝: 4
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功