mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
集成redis
This commit is contained in:
parent
68abdeca93
commit
ce2a3b4647
6
novel-admin/Dockerfile
Normal file
6
novel-admin/Dockerfile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FROM java:8
|
||||||
|
ADD novel-admin-1.0.0.jar /root
|
||||||
|
ENV dburl=""
|
||||||
|
ENV username=""
|
||||||
|
ENV password=""
|
||||||
|
ENTRYPOINT ["sh","-c","java -Dspring.datasource.url=${dburl} -Dspring.datasource.username=${username} -Dspring.datasource.password=${password} -jar /root/novel-admin-1.0.0.jar"]
|
@ -31,11 +31,20 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-cache</artifactId>
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--ehcache-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.sf.ehcache</groupId>
|
<groupId>net.sf.ehcache</groupId>
|
||||||
<artifactId>ehcache</artifactId>
|
<artifactId>ehcache</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--集成redis-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-redis</artifactId>
|
||||||
|
<version>${redis.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--mysql驱动-->
|
<!--mysql驱动-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
|
@ -50,4 +50,9 @@ public interface CacheKey {
|
|||||||
* 上一次搜索引擎更新的时间
|
* 上一次搜索引擎更新的时间
|
||||||
* */
|
* */
|
||||||
String ES_LAST_UPDATE_TIME = "esLastUpdateTime";
|
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 expire(String key, long timeout);
|
||||||
|
|
||||||
/**
|
|
||||||
* 刷新缓存
|
|
||||||
* */
|
|
||||||
void refresh(String key);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,23 @@
|
|||||||
package com.java2nb.novel.core.cache.impl;
|
package com.java2nb.novel.core.cache.impl;
|
||||||
|
|
||||||
import com.java2nb.novel.core.cache.CacheService;
|
import com.java2nb.novel.core.cache.CacheService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import net.sf.ehcache.Cache;
|
import net.sf.ehcache.Cache;
|
||||||
import net.sf.ehcache.CacheManager;
|
import net.sf.ehcache.CacheManager;
|
||||||
import net.sf.ehcache.Element;
|
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;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xxy
|
* @author xxy
|
||||||
*/
|
*/
|
||||||
|
@ConditionalOnProperty(prefix = "cache", name = "type", havingValue = "ehcache")
|
||||||
|
@RequiredArgsConstructor
|
||||||
@Service
|
@Service
|
||||||
public class EhCacheServiceImpl implements CacheService {
|
public class EhCacheServiceImpl implements CacheService {
|
||||||
|
|
||||||
@Autowired
|
private final CacheManager cacheManager ;
|
||||||
private CacheManager cacheManager ;
|
|
||||||
|
|
||||||
private static final String CACHE_NAME = "utilCache";
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,14 +31,6 @@ public class EhCacheServiceImpl implements CacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public CacheManager getCacheManager() {
|
|
||||||
return cacheManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String get(String key) {
|
public String get(String key) {
|
||||||
Element element = getCache().get(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
|
username: root
|
||||||
password: test123456
|
password: test123456
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
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:
|
generator:
|
||||||
write-numbers-as-strings: true
|
write-numbers-as-strings: true
|
||||||
|
|
||||||
|
#缓存类型,ehcache(默认)、redis
|
||||||
|
cache:
|
||||||
|
type: ehcache
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
configuration:
|
configuration:
|
||||||
|
@ -40,15 +40,13 @@ public class BookToEsSchedule {
|
|||||||
private final JestClient jestClient;
|
private final JestClient jestClient;
|
||||||
|
|
||||||
|
|
||||||
private boolean lock = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 2分钟导入一次
|
* 2分钟导入一次
|
||||||
*/
|
*/
|
||||||
@Scheduled(fixedRate = 1000 * 60 * 2)
|
@Scheduled(fixedRate = 1000 * 60 * 2)
|
||||||
public void saveToEs() {
|
public void saveToEs() {
|
||||||
if (!lock) {
|
if (cacheService.get(CacheKey.ES_TRANS_LOCK) == null) {
|
||||||
lock = true;
|
cacheService.set(CacheKey.ES_TRANS_LOCK, "1", 60 * 60);
|
||||||
try {
|
try {
|
||||||
//查询需要更新的小说
|
//查询需要更新的小说
|
||||||
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
|
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
|
||||||
@ -84,9 +82,9 @@ public class BookToEsSchedule {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
cacheService.del(CacheKey.ES_TRANS_LOCK);
|
||||||
|
|
||||||
|
|
||||||
lock = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
1
pom.xml
1
pom.xml
@ -38,6 +38,7 @@
|
|||||||
<jjwt.version>0.9.0</jjwt.version>
|
<jjwt.version>0.9.0</jjwt.version>
|
||||||
<elasticsearch.version>6.2.2</elasticsearch.version>
|
<elasticsearch.version>6.2.2</elasticsearch.version>
|
||||||
<jest.version>6.3.1</jest.version>
|
<jest.version>6.3.1</jest.version>
|
||||||
|
<redis.version>1.4.1.RELEASE</redis.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user