package com.zjf.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.Callable;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
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;
public class RedisCache implements Cache{
private RedisTemplate<String, Object> redisTemplate;
private String name;
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
public Object getNativeCache() {
// TODO Auto-generated method stub
return this.redisTemplate;
}
public ValueWrapper get(Object key) {
// TODO Auto-generated method stub
final String keyf = key.toString();
Object object = null;
object = redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = keyf.getBytes();
byte[] value = connection.get(key);
if (value == null) {
return null;
}
return toObject(value);
}
});
return (object != null ? new SimpleValueWrapper(object) : null);
}
public void put(Object key, Object value) {
// TODO Auto-generated method stub
final String keyf = key.toString();
final Object valuef = value;
final long liveTime = 86400;
redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keyb = keyf.getBytes();
byte[] valueb = toByteArray(valuef);
connection.set(keyb, valueb);
if (liveTime > 0) {
connection.expire(keyb, liveTime);
}
return 1L;
}
});
}
private byte[] toByteArray(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bytes = bos.toByteArray();
oos.close();
bos.close();
}catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}
private Object toObject(byte[] bytes) {
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
public void evict(Object key) {
// TODO Auto-generated method stub
final String keyf = key.toString();
redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
return connection.del(keyf.getBytes());
}
});
}
public void clear() {
// TODO Auto-generated method stub
redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection)
throws DataAccessException {
connection.flushDb();
return "ok";
}
});
}
public <T> T get(Object key, Class<T> type) {
// TODO Auto-generated method stub
return null;
}
public ValueWrapper putIfAbsent(Object key, Object value) {
// TODO Auto-generated method stub
return null;
}
public <T> T get(Object key, Callable<T> valueLoader) {
// TODO Auto-generated method stub
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springmvc.rar (39个子文件)
springmvc
pom.xml 1KB
target
test-classes
com
zjf
m2e-wtp
web-resources
META-INF
maven
com.zjf
springmvc
pom.xml 1KB
pom.properties 220B
MANIFEST.MF 107B
classes
redis.properties 1KB
com
zjf
util
RedisCache$3.class 1KB
RedisCache$1.class 1KB
RedisCache.class 5KB
RedisCache$4.class 1KB
RedisCache$2.class 2KB
spring
cache
dao
UserDao.class 432B
impl
UserDaoImpl.class 2KB
model
User.class 1KB
DemoController.class 1KB
service
UserService.class 444B
impl
UserServiceImpl.class 2KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 733B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 575B
org.eclipse.jdt.core.prefs 430B
src
test
resources
java
com
zjf
main
webapp
WEB-INF
config
spring-servlet.xml 1KB
applicationContext.xml 4KB
web.xml 1KB
view
adduser.jsp 373B
finduser.jsp 374B
lib
resources
java
redis.properties 1KB
com
zjf
util
RedisCache.java 5KB
spring
cache
dao
UserDao.java 409B
impl
UserDaoImpl.java 1KB
model
User.java 1KB
DemoController.java 820B
service
UserService.java 363B
impl
UserServiceImpl.java 2KB
.project 1KB
.classpath 2KB
共 39 条
- 1
资源评论
- zzzf19922018-03-27看看内容~~~~~~
- iwanttoseeyou2017-12-06下载下来不能启动 报错
- Jandey.柳2019-08-10主要是看看框架用法
xiaolang8762400
- 粉丝: 13
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- CVE-2019-0708漏洞检测与利用工具
- STM32单片机FPGA毕设电路原理论文报告利用c8051f020系列单片机实现智能仪器开发
- STM32单片机FPGA毕设电路原理论文报告利用8位单片机实现与Internet网络通信应用研究
- STM32单片机FPGA毕设电路原理论文报告利用80C196单片机测量三相异步电动机功率因数
- STM32单片机FPGA毕设电路原理论文报告力学传感器与单片机的接口设计
- p107-u07FLT2.wav
- STM32单片机FPGA毕设电路原理论文报告雷达幅频特性测试仪的智能化研究
- STM32单片机FPGA毕设电路原理论文报告可组网电子温湿度测量仪的设计与实现
- STM32单片机FPGA毕设电路原理论文报告可在单片机上实现的语音混沌保密通信方法
- STM32单片机FPGA毕设电路原理论文报告可实现的基于MCS51单片机的恒温控制系统的设计
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功