package com.zhang.redis;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import com.zhang.utils.JsonUtils;
@Component("redisService")
public class RedisServiceImpl implements RedisService {
private StringRedisTemplate redisTemplate;
@Autowired
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public <T> void put(String key, T obj) {
redisTemplate.opsForValue().set(key, JsonUtils.toJson(obj));
}
public <T> void put(String key, T obj, int timeout) {
put(key, obj, timeout, TimeUnit.MINUTES);
}
public <T> void put(String key, T obj, int timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, JsonUtils.toJson(obj), timeout, unit);
}
public <T> T get(String key, Class<T> cls) {
return JsonUtils.fromJson(redisTemplate.opsForValue().get(key), cls);
}
public <E, T extends Collection<E>> T get(String key, Class<E> cls, Class<T> collectionClass) {
return JsonUtils.fromJson(redisTemplate.opsForValue().get(key), cls, collectionClass);
}
public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier) {
T t = get(key, cls);
if (t == null) {
t = supplier.get();
if (t != null) {
put(key, t);
}
}
return t;
}
public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout) {
T t = get(key, cls);
if (t == null) {
t = supplier.get();
if (t != null) {
put(key, t, timeout);
}
}
return t;
}
public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout, TimeUnit unit) {
T t = get(key, cls);
if (t == null) {
t = supplier.get();
if (t != null) {
put(key, t, timeout, unit);
}
}
return t;
}
public <T> T putIfAbsent(String key, Class<T> cls, Supplier<T> supplier, int timeout, TimeUnit unit, boolean refresh) {
T t = get(key, cls);
if (t == null) {
t = supplier.get();
if (t != null) {
put(key, t, timeout, unit);
}
} else {
if (refresh) {
expire(key, timeout, unit);
}
}
return t;
}
public <E, T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls, Supplier<T> supplier) {
T t = get(key, cls, collectionCls);
if (t == null || t.isEmpty()) {
t = supplier.get();
if (t != null && t.size() > 0) {
put(key, t);
}
}
return t;
}
public <E, T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls, Supplier<T> supplier, int timeout) {
return putIfAbsent(key, cls, collectionCls, supplier, timeout, TimeUnit.SECONDS);
}
public <E, T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls, Supplier<T> supplier, int timeout, TimeUnit unit) {
return putIfAbsent(key, cls, collectionCls, supplier, timeout, unit, false);
}
public <E, T extends Collection<E>> T putIfAbsent(String key, Class<E> cls, Class<T> collectionCls, Supplier<T> supplier, int timeout, TimeUnit unit, boolean refresh) {
T t = get(key, cls, collectionCls);
if (t == null || t.isEmpty()) {
t = supplier.get();
if (t != null && t.size() > 0) {
put(key, t, timeout, unit);
}
} else {
if (refresh) {
expire(key, timeout, unit);
}
}
return t;
}
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
public void delete(String key) {
redisTemplate.delete(key);
}
public boolean expire(String key, long timeout, TimeUnit timeUnit) {
return redisTemplate.expire(key, timeout, timeUnit);
}
public boolean expire(String key, long timeout) {
return expire(key, timeout, TimeUnit.MINUTES);
}
public void put(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public void put(String key, String value, int timeout) {
put(key, value, timeout, TimeUnit.MINUTES);
}
public void put(String key, String value, int timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Maven_Redis.zip (50个子文件)
Maven_Redis
pom.xml 5KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
com.zhang
Maven_Redis
pom.xml 5KB
pom.properties 219B
MANIFEST.MF 112B
classes
config
mybatis-config.xml 260B
spring-mvc.xml 2KB
Spring-redis.xml 1KB
spring-common.xml 2KB
log4j.properties 830B
com
zhang
dao
UserDao.class 513B
model
User.class 2KB
result
Result.class 1KB
controller
RedisController.class 2KB
UserController.class 3KB
redis
RedisServiceImpl.class 8KB
RedisService.class 3KB
service
UserService.class 3KB
utils
JsonObjectMapper$1.class 1KB
JsonUtils.class 3KB
JsonObjectMapper.class 1KB
mybatis
user.xml 1KB
.settings
org.eclipse.m2e.core.prefs 86B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 245B
org.eclipse.core.resources.prefs 55B
org.eclipse.wst.common.component 664B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 48B
.jsdtscope 626B
org.eclipse.jdt.core.prefs 422B
src
test
resources
java
main
webapp
WEB-INF
web.xml 3KB
index.html 128B
resources
config
mybatis-config.xml 260B
spring-mvc.xml 2KB
Spring-redis.xml 1KB
spring-common.xml 2KB
log4j.properties 830B
mybatis
user.xml 1KB
java
com
zhang
dao
UserDao.java 348B
model
User.java 998B
result
Result.java 564B
controller
UserController.java 3KB
RedisController.java 1KB
redis
RedisService.java 2KB
RedisServiceImpl.java 4KB
service
UserService.java 1KB
utils
JsonUtils.java 2KB
JsonObjectMapper.java 1KB
.project 1KB
.classpath 1KB
共 50 条
- 1
资源评论
- Derrick-然2018-03-17没有数据库的sql语句啊,发个数据库创建代码
zhangjinru
- 粉丝: 0
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功