mirror of
https://github.com/201206030/novel-plus.git
synced 2025-06-24 04:46:37 +00:00
集成redis
This commit is contained in:
@ -50,4 +50,9 @@ public interface CacheKey {
|
||||
* 上一次搜索引擎更新的时间
|
||||
* */
|
||||
String ES_LAST_UPDATE_TIME = "esLastUpdateTime";
|
||||
|
||||
/**
|
||||
* 搜索引擎转换锁
|
||||
* */
|
||||
String ES_TRANS_LOCK = "esTransLock";
|
||||
}
|
@ -51,9 +51,5 @@ public interface CacheService {
|
||||
* */
|
||||
void expire(String key, long timeout);
|
||||
|
||||
/**
|
||||
* 刷新缓存
|
||||
* */
|
||||
void refresh(String key);
|
||||
|
||||
}
|
||||
|
@ -1,24 +1,25 @@
|
||||
package com.java2nb.novel.core.cache.impl;
|
||||
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import net.sf.ehcache.Element;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author xxy
|
||||
*/
|
||||
@ConditionalOnProperty(prefix = "cache", name = "type", havingValue = "ehcache")
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class EhCacheServiceImpl implements CacheService {
|
||||
|
||||
@Autowired
|
||||
private CacheManager cacheManager ;
|
||||
private final CacheManager cacheManager ;
|
||||
|
||||
private static final String CACHE_NAME = "utilCache";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获得一个Cache,没有则创建一个。
|
||||
* @return
|
||||
@ -30,14 +31,6 @@ public class EhCacheServiceImpl implements CacheService {
|
||||
}
|
||||
|
||||
|
||||
public CacheManager getCacheManager() {
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
Element element = getCache().get(key);
|
||||
@ -125,20 +118,6 @@ public class EhCacheServiceImpl implements CacheService {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void refresh(String key) {
|
||||
Element element = getCache().get(key);
|
||||
if (element != null) {
|
||||
Object value = element.getValue();
|
||||
int timeToLive = element.getTimeToLive();
|
||||
element = new Element(key, value);
|
||||
element.setTimeToLive(timeToLive);
|
||||
Cache cache = getCache();
|
||||
cache.put(element);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
73
novel-common/src/main/java/com/java2nb/novel/core/cache/impl/RedisServiceImpl.java
vendored
Normal file
73
novel-common/src/main/java/com/java2nb/novel/core/cache/impl/RedisServiceImpl.java
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
package com.java2nb.novel.core.cache.impl;
|
||||
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author xxy
|
||||
*/
|
||||
@ConditionalOnProperty(prefix = "cache", name = "type", havingValue = "redis")
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class RedisServiceImpl implements CacheService {
|
||||
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
private final RedisTemplate<Object, Object> redisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return stringRedisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String key, String value) {
|
||||
stringRedisTemplate.opsForValue().set(key,value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String key, String value, long timeout) {
|
||||
stringRedisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(String key, Object value) {
|
||||
redisTemplate.opsForValue().set(key,value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(String key, Object value, long timeout) {
|
||||
redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void del(String key) {
|
||||
redisTemplate.delete(key);
|
||||
stringRedisTemplate.delete(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String key) {
|
||||
return redisTemplate.hasKey(key) || stringRedisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void expire(String key, long timeout) {
|
||||
redisTemplate.expire(key,timeout, TimeUnit.SECONDS);
|
||||
stringRedisTemplate.expire(key,timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,28 @@ spring:
|
||||
username: root
|
||||
password: test123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
#Redis服务器IP
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
#Redis服务器连接端口
|
||||
port: 6379
|
||||
#Redis服务器连接密码
|
||||
password: test
|
||||
jedis:
|
||||
pool:
|
||||
#连接池最大连接数(使用负值表示没有限制)
|
||||
max-active: 8
|
||||
#连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
max-wait: 1
|
||||
#连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
max-idle: 8
|
||||
#连接池中的最小空闲连接
|
||||
min-idle: 0
|
||||
#连接超时时间(毫秒)
|
||||
timeout: 30000
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -11,6 +11,9 @@ spring:
|
||||
generator:
|
||||
write-numbers-as-strings: true
|
||||
|
||||
#缓存类型,ehcache(默认)、redis
|
||||
cache:
|
||||
type: ehcache
|
||||
|
||||
mybatis:
|
||||
configuration:
|
||||
|
Reference in New Issue
Block a user