From ce2a3b464789f05458d1deb6681517cc21bfbe70 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <773861846@qq.com> Date: Mon, 25 May 2020 16:54:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- novel-admin/Dockerfile | 6 ++ novel-common/pom.xml | 9 +++ .../java2nb/novel/core/cache/CacheKey.java | 5 ++ .../novel/core/cache/CacheService.java | 4 - .../core/cache/impl/EhCacheServiceImpl.java | 35 ++------- .../core/cache/impl/RedisServiceImpl.java | 73 +++++++++++++++++++ .../main/resources/application-common-dev.yml | 22 ++++++ .../src/main/resources/application-common.yml | 3 + .../novel/core/schedule/BookToEsSchedule.java | 20 +++-- pom.xml | 1 + 10 files changed, 135 insertions(+), 43 deletions(-) create mode 100644 novel-admin/Dockerfile create mode 100644 novel-common/src/main/java/com/java2nb/novel/core/cache/impl/RedisServiceImpl.java diff --git a/novel-admin/Dockerfile b/novel-admin/Dockerfile new file mode 100644 index 0000000..be018de --- /dev/null +++ b/novel-admin/Dockerfile @@ -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"] \ No newline at end of file diff --git a/novel-common/pom.xml b/novel-common/pom.xml index 16d564f..f210839 100644 --- a/novel-common/pom.xml +++ b/novel-common/pom.xml @@ -31,11 +31,20 @@ org.springframework.boot spring-boot-starter-cache + + net.sf.ehcache ehcache + + + org.springframework.boot + spring-boot-starter-redis + ${redis.version} + + mysql diff --git a/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheKey.java b/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheKey.java index a4e9470..d284646 100644 --- a/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheKey.java +++ b/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheKey.java @@ -50,4 +50,9 @@ public interface CacheKey { * 上一次搜索引擎更新的时间 * */ String ES_LAST_UPDATE_TIME = "esLastUpdateTime"; + + /** + * 搜索引擎转换锁 + * */ + String ES_TRANS_LOCK = "esTransLock"; } \ No newline at end of file diff --git a/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheService.java b/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheService.java index f0e8531..05d7423 100644 --- a/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheService.java +++ b/novel-common/src/main/java/com/java2nb/novel/core/cache/CacheService.java @@ -51,9 +51,5 @@ public interface CacheService { * */ void expire(String key, long timeout); - /** - * 刷新缓存 - * */ - void refresh(String key); } diff --git a/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/EhCacheServiceImpl.java b/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/EhCacheServiceImpl.java index 1886d19..fa3937a 100644 --- a/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/EhCacheServiceImpl.java +++ b/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/EhCacheServiceImpl.java @@ -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); - } - - } - } diff --git a/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/RedisServiceImpl.java b/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/RedisServiceImpl.java new file mode 100644 index 0000000..0498f03 --- /dev/null +++ b/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/RedisServiceImpl.java @@ -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 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); + } + +} diff --git a/novel-common/src/main/resources/application-common-dev.yml b/novel-common/src/main/resources/application-common-dev.yml index a2636a1..3c0f59c 100644 --- a/novel-common/src/main/resources/application-common-dev.yml +++ b/novel-common/src/main/resources/application-common-dev.yml @@ -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 + + + diff --git a/novel-common/src/main/resources/application-common.yml b/novel-common/src/main/resources/application-common.yml index 0744f3a..b3cf4a0 100644 --- a/novel-common/src/main/resources/application-common.yml +++ b/novel-common/src/main/resources/application-common.yml @@ -11,6 +11,9 @@ spring: generator: write-numbers-as-strings: true +#缓存类型,ehcache(默认)、redis +cache: + type: ehcache mybatis: configuration: diff --git a/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java b/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java index 46e31f9..f74e56c 100644 --- a/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java +++ b/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java @@ -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 books = bookService.queryBookByUpdateTimeByPage(lastDate,100); - for(Book book : books) { + List 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; } diff --git a/pom.xml b/pom.xml index 1e60376..8afbccb 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ 0.9.0 6.2.2 6.3.1 + 1.4.1.RELEASE