package com.kangchul.cache;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;
import com.kangchul.cache.RedisCache;
import com.kangchul.util.ProtoStuffSerializerUtil;
/**
* redis缓存
*
* @author kangchul
*/
@Component
public class RedisCache {
public final static String CAHCENAME = "cache";// 缓存名
public final static int CAHCETIME = 3600;// 默认缓存时间
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 存入redis,不带过期时间
*/
public <T> boolean putCache(String key, T obj) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.setNX(bkey, bvalue);
}
});
return result;
}
/**
* 存入redis,带过期时间
*/
public <T> void putCacheWithExpireTime(String key, T obj, final long expireTime) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj);
redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(bkey, expireTime, bvalue);
return true;
}
});
}
public <T> boolean putListCache(String key, List<T> objList) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.setNX(bkey, bvalue);
}
});
return result;
}
public <T> boolean putListCacheWithExpireTime(String key, List<T> objList, final long expireTime) {
final byte[] bkey = key.getBytes();
final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(bkey, expireTime, bvalue);
return true;
}
});
return result;
}
public <T> T getCache(final String key, Class<T> targetClass) {
byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key.getBytes());
}
});
if (result == null) {
return null;
}
return ProtoStuffSerializerUtil.deserialize(result, targetClass);
}
public <T> List<T> getListCache(final String key, Class<T> targetClass) {
byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key.getBytes());
}
});
if (result == null) {
return null;
}
return ProtoStuffSerializerUtil.deserializeList(result, targetClass);
}
/**
* 精确删除key
*
* @param key
*/
public void deleteCache(String key) {
redisTemplate.delete(key);
}
/**
* 模糊删除key
*
* @param pattern
*/
public void deleteCacheWithPattern(String pattern) {
Set<String> keys = redisTemplate.keys(pattern);
redisTemplate.delete(keys);
}
/**
* 清空所有缓存
*/
public void clearCache() {
deleteCacheWithPattern(RedisCache.CAHCENAME + "|*");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Spring SpringMVC Mybatis 整合 Redis
共70个文件
class:19个
xml:18个
java:12个
需积分: 9 7 下载量 85 浏览量
2018-02-02
14:14:46
上传
评论
收藏 63KB ZIP 举报
温馨提示
Spring SpringMVC Mybatis 整合 Redis .........................................................................................
资源推荐
资源详情
资源评论
收起资源包目录
ssm-redis.zip (70个子文件)
ssm-redis
.project 1KB
src
test
java
main
webapp
WEB-INF
jsp
selAll.jsp 709B
web.xml 2KB
index.jsp 52B
resources
spring-config.xml 1KB
redis.properties 165B
spring-redis.xml 5KB
mapper
UserMapper.xml 1KB
spring-mybatis.xml 2KB
spring-mvc.xml 2KB
jdbc.properties 264B
mybatis-config.xml 650B
log4j.properties 1KB
spring.xml 1KB
java
com
kangchul
service
impl
UserServiceImpl.java 3KB
UserService.java 508B
aop
LogAop.java 826B
model
User.java 803B
controller
UserController.java 764B
dao
UserMapper.java 502B
util
PageBean.java 935B
ProtoStuffSerializerUtil.java 3KB
JsonUtil.java 957B
cache
RedisCache.java 4KB
RedisClusterCache.java 3KB
task
Quartz.java 711B
target
m2e-wtp
web-resources
META-INF
MANIFEST.MF 107B
maven
com.kangchul
ssm-redis
pom.properties 226B
pom.xml 7KB
classes
spring-config.xml 1KB
redis.properties 165B
spring-redis.xml 5KB
mapper
UserMapper.xml 1KB
spring-mybatis.xml 2KB
com
kangchul
service
UserService.class 559B
impl
UserServiceImpl.class 4KB
aop
LogAop.class 1KB
model
User.class 1KB
controller
UserController.class 2KB
dao
UserMapper.class 603B
util
JsonUtil.class 1KB
PageBean.class 2KB
ProtoStuffSerializerUtil.class 5KB
cache
RedisCache$3.class 1KB
RedisCache$5.class 1KB
RedisCache$2.class 1KB
RedisCache.class 4KB
RedisCache$6.class 1KB
RedisCache$4.class 1KB
RedisCache$1.class 1KB
RedisClusterCache.class 5KB
task
Quartz$BizQuartz.class 1KB
Quartz.class 362B
spring-mvc.xml 2KB
jdbc.properties 264B
mybatis-config.xml 650B
log4j.properties 1KB
spring.xml 1KB
test-classes
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 124B
org.eclipse.wst.common.component 668B
.jsdtscope 639B
pom.xml 7KB
.classpath 1KB
.gitignore 162B
共 70 条
- 1
资源评论
JhonSnow
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功