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>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--ehcache-->
|
||||
<dependency>
|
||||
<groupId>net.sf.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--集成redis-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-redis</artifactId>
|
||||
<version>${redis.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--mysql驱动-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
|
@ -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,22 +1,23 @@
|
||||
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 static final String CACHE_NAME = "utilCache";
|
||||
private final CacheManager cacheManager ;
|
||||
|
||||
|
||||
/**
|
||||
@ -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:
|
||||
|
@ -40,15 +40,13 @@ public class BookToEsSchedule {
|
||||
private final JestClient jestClient;
|
||||
|
||||
|
||||
private boolean lock = false;
|
||||
|
||||
/**
|
||||
* 2分钟导入一次
|
||||
*/
|
||||
@Scheduled(fixedRate = 1000 * 60 * 2)
|
||||
public void saveToEs() {
|
||||
if (!lock) {
|
||||
lock = true;
|
||||
if (cacheService.get(CacheKey.ES_TRANS_LOCK) == null) {
|
||||
cacheService.set(CacheKey.ES_TRANS_LOCK, "1", 60 * 60);
|
||||
try {
|
||||
//查询需要更新的小说
|
||||
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
|
||||
@ -56,14 +54,14 @@ public class BookToEsSchedule {
|
||||
lastDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
|
||||
}
|
||||
|
||||
long count ;
|
||||
long count;
|
||||
do {
|
||||
|
||||
List<Book> books = bookService.queryBookByUpdateTimeByPage(lastDate,100);
|
||||
for(Book book : books) {
|
||||
List<Book> books = bookService.queryBookByUpdateTimeByPage(lastDate, 100);
|
||||
for (Book book : books) {
|
||||
//导入到ES
|
||||
EsBookVO esBookVO = new EsBookVO();
|
||||
BeanUtils.copyProperties(book,esBookVO,"lastIndexUpdateTime");
|
||||
BeanUtils.copyProperties(book, esBookVO, "lastIndexUpdateTime");
|
||||
esBookVO.setLastIndexUpdateTime(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(book.getLastIndexUpdateTime()));
|
||||
Index action = new Index.Builder(esBookVO).index("novel").type("book").id(book.getId().toString()).build();
|
||||
|
||||
@ -77,16 +75,16 @@ public class BookToEsSchedule {
|
||||
|
||||
count = books.size();
|
||||
|
||||
}while (count == 100);
|
||||
} while (count == 100);
|
||||
|
||||
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(),e);
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
cacheService.del(CacheKey.ES_TRANS_LOCK);
|
||||
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user