From 29d074ec188f790a6877af4fcfa0154e6fc7c310 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <1179705413@qq.com> Date: Fri, 14 Apr 2023 10:40:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20Ehcache?= =?UTF-8?q?=EF=BC=8C=E9=BB=98=E8=AE=A4=E7=BC=93=E5=AD=98=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=B8=BA=20Redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- novel-admin/pom.xml | 4 - .../java2nb/common/config/ShiroConfig.java | 47 ++----- .../src/main/resources/application-dev.yml | 2 +- .../src/main/resources/application.yml | 13 +- novel-common/pom.xml | 6 - .../core/cache/impl/EhCacheServiceImpl.java | 122 ------------------ .../core/cache/impl/RedisServiceImpl.java | 82 ++++++------ .../main/resources/application-common-dev.yml | 2 +- .../src/main/resources/application-common.yml | 6 +- novel-crawl/src/main/resources/ehcache.xml | 30 ----- novel-front/src/main/resources/ehcache.xml | 30 ----- 11 files changed, 56 insertions(+), 288 deletions(-) delete mode 100644 novel-common/src/main/java/com/java2nb/novel/core/cache/impl/EhCacheServiceImpl.java delete mode 100644 novel-crawl/src/main/resources/ehcache.xml delete mode 100644 novel-front/src/main/resources/ehcache.xml diff --git a/novel-admin/pom.xml b/novel-admin/pom.xml index 70b1290..5a7bcaf 100644 --- a/novel-admin/pom.xml +++ b/novel-admin/pom.xml @@ -200,10 +200,6 @@ org.springframework.boot spring-boot-starter-cache - - net.sf.ehcache - ehcache - diff --git a/novel-admin/src/main/java/com/java2nb/common/config/ShiroConfig.java b/novel-admin/src/main/java/com/java2nb/common/config/ShiroConfig.java index 20d344d..39cf48f 100644 --- a/novel-admin/src/main/java/com/java2nb/common/config/ShiroConfig.java +++ b/novel-admin/src/main/java/com/java2nb/common/config/ShiroConfig.java @@ -5,11 +5,8 @@ import com.java2nb.common.redis.shiro.RedisCacheManager; import com.java2nb.common.redis.shiro.RedisManager; import com.java2nb.common.redis.shiro.RedisSessionDAO; import com.java2nb.system.shiro.UserRealm; -import net.sf.ehcache.CacheManager; -import org.apache.shiro.cache.ehcache.EhCacheManager; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.session.SessionListener; -import org.apache.shiro.session.mgt.eis.MemorySessionDAO; import org.apache.shiro.session.mgt.eis.SessionDAO; import org.apache.shiro.spring.LifecycleBeanPostProcessor; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; @@ -29,6 +26,7 @@ import java.util.LinkedHashMap; */ @Configuration public class ShiroConfig { + @Value("${spring.redis.host}") private String host; @Value("${spring.redis.password}") @@ -38,9 +36,6 @@ public class ShiroConfig { @Value("${spring.redis.timeout}") private int timeout; - @Value("${spring.cache.type}") - private String cacheType ; - @Value("${server.session-timeout}") private int tomcatTimeout; @@ -67,8 +62,8 @@ public class ShiroConfig { shiroFilterFactoryBean.setSuccessUrl("/index"); shiroFilterFactoryBean.setUnauthorizedUrl("/403"); LinkedHashMap filterChainDefinitionMap = new LinkedHashMap<>(); - filterChainDefinitionMap.put("/login","anon"); - filterChainDefinitionMap.put("/getVerify","anon"); + filterChainDefinitionMap.put("/login", "anon"); + filterChainDefinitionMap.put("/getVerify", "anon"); filterChainDefinitionMap.put("/css/**", "anon"); filterChainDefinitionMap.put("/js/**", "anon"); filterChainDefinitionMap.put("/fonts/**", "anon"); @@ -92,11 +87,7 @@ public class ShiroConfig { //设置realm. securityManager.setRealm(userRealm()); // 自定义缓存实现 使用redis - if (Constant.CACHE_TYPE_REDIS.equals(cacheType)) { - securityManager.setCacheManager(rediscacheManager()); - } else { - securityManager.setCacheManager(ehCacheManager()); - } + securityManager.setCacheManager(rediscacheManager()); securityManager.setSessionManager(sessionManager()); return securityManager; } @@ -108,8 +99,7 @@ public class ShiroConfig { } /** - * 开启shiro aop注解支持. - * 使用代理方式;所以需要开启代码支持; + * 开启shiro aop注解支持. 使用代理方式;所以需要开启代码支持; * * @param securityManager * @return @@ -138,8 +128,7 @@ public class ShiroConfig { } /** - * cacheManager 缓存 redis实现 - * 使用的是shiro-redis开源插件 + * cacheManager 缓存 redis实现 使用的是shiro-redis开源插件 * * @return */ @@ -151,8 +140,7 @@ public class ShiroConfig { /** - * RedisSessionDAO shiro sessionDao层的实现 通过redis - * 使用的是shiro-redis开源插件 + * RedisSessionDAO shiro sessionDao层的实现 通过redis 使用的是shiro-redis开源插件 */ @Bean public RedisSessionDAO redisSessionDAO() { @@ -163,11 +151,7 @@ public class ShiroConfig { @Bean public SessionDAO sessionDAO() { - if (Constant.CACHE_TYPE_REDIS.equals(cacheType)) { - return redisSessionDAO(); - } else { - return new MemorySessionDAO(); - } + return redisSessionDAO(); } /** @@ -184,19 +168,4 @@ public class ShiroConfig { return sessionManager; } - @Bean - public EhCacheManager ehCacheManager() { - EhCacheManager em = new EhCacheManager(); - em.setCacheManager(cacheManager()); - return em; - } - - @Bean("cacheManager2") - CacheManager cacheManager(){ - return CacheManager.create(); - } - - - - } diff --git a/novel-admin/src/main/resources/application-dev.yml b/novel-admin/src/main/resources/application-dev.yml index c7028e1..647122b 100644 --- a/novel-admin/src/main/resources/application-dev.yml +++ b/novel-admin/src/main/resources/application-dev.yml @@ -39,7 +39,7 @@ spring: redis: host: 127.0.0.1 port: 6379 - password: test + password: test123456 # 连接超时时间(毫秒) timeout: 10000 jedis: diff --git a/novel-admin/src/main/resources/application.yml b/novel-admin/src/main/resources/application.yml index d2e2bf5..7c8e35f 100644 --- a/novel-admin/src/main/resources/application.yml +++ b/novel-admin/src/main/resources/application.yml @@ -1,8 +1,8 @@ server: session-timeout: 18000 -# tomcat: -# max-threads: 1000 -# min-spare-threads: 30 + # tomcat: + # max-threads: 1000 + # min-spare-threads: 30 port: 80 # uri-encoding: utf-8 #security: @@ -15,7 +15,7 @@ spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss - profiles: + profiles: active: dev servlet: multipart: @@ -24,10 +24,7 @@ spring: devtools: restart: enabled: true - cache: - type: ehcache - ehcache: - config: classpath:ehcache.xml + mybatis: configuration: #自动将数据库带下划线的表字段值映射到Java类的驼峰字段上 diff --git a/novel-common/pom.xml b/novel-common/pom.xml index aa63d57..2a9443d 100644 --- a/novel-common/pom.xml +++ b/novel-common/pom.xml @@ -32,12 +32,6 @@ spring-boot-starter-cache - - - net.sf.ehcache - ehcache - - org.springframework.boot 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 deleted file mode 100644 index e935bdd..0000000 --- a/novel-common/src/main/java/com/java2nb/novel/core/cache/impl/EhCacheServiceImpl.java +++ /dev/null @@ -1,122 +0,0 @@ -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.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 { - - private final CacheManager cacheManager; - - - /** - * 获得一个Cache,没有则创建一个。 - * - * @return - */ - private Cache getCache() { - - Cache cache = cacheManager.getCache("util_cache"); - return cache; - } - - - @Override - public String get(String key) { - Element element = getCache().get(key); - return element == null ? null : (String) element.getObjectValue(); - } - - @Override - public void set(String key, String value) { - Element element = new Element(key, value); - Cache cache = getCache(); - //不过期 - cache.getCacheConfiguration().setEternal(true); - cache.put(element); - - } - - @Override - public void set(String key, String value, long timeout) { - Element element = new Element(key, value); - element.setTimeToLive((int) timeout); - Cache cache = getCache(); - cache.put(element); - - } - - @Override - public void del(String key) { - getCache().remove(key); - - - } - - @Override - public boolean contains(String key) { - return getCache().isKeyInCache(key); - } - - @Override - public void expire(String key, long timeout) { - Element element = getCache().get(key); - if (element != null) { - Object value = element.getValue(); - element = new Element(key, value); - element.setTimeToLive((int) timeout); - Cache cache = getCache(); - cache.put(element); - } - } - - - /** - * 根据key获取缓存的Object类型数据 - */ - @Override - public Object getObject(String key) { - Element element = getCache().get(key); - return element == null ? null : element.getObjectValue(); - } - - - /** - * 设置Object类型的缓存 - */ - @Override - public void setObject(String key, Object value) { - Element element = new Element(key, value); - Cache cache = getCache(); - //不过期 - cache.getCacheConfiguration().setEternal(true); - cache.put(element); - - } - - - /** - * 设置一个有过期时间的Object类型的缓存,单位秒 - */ - @Override - public void setObject(String key, Object value, long timeout) { - Element element = new Element(key, value); - element.setTimeToLive((int) timeout); - 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 index 0498f03..aad1a04 100644 --- 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 @@ -2,7 +2,6 @@ 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; @@ -12,62 +11,61 @@ 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 StringRedisTemplate stringRedisTemplate; - private final RedisTemplate redisTemplate; + private final RedisTemplate redisTemplate; - @Override - public String get(String key) { - return stringRedisTemplate.opsForValue().get(key); - } + @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) { + 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 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 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) { + 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 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 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 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); - } + @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 d59ac01..bc9da55 100644 --- a/novel-common/src/main/resources/application-common-dev.yml +++ b/novel-common/src/main/resources/application-common-dev.yml @@ -9,7 +9,7 @@ spring: #Redis服务器连接端口 port: 6379 #Redis服务器连接密码 - password: test + password: test123456 jedis: pool: #连接池最大连接数(使用负值表示没有限制) diff --git a/novel-common/src/main/resources/application-common.yml b/novel-common/src/main/resources/application-common.yml index e98b078..de4f9b5 100644 --- a/novel-common/src/main/resources/application-common.yml +++ b/novel-common/src/main/resources/application-common.yml @@ -6,7 +6,7 @@ spring: mode: LEGACYHTML5 #去除thymeleaf的html严格校验thymeleaf.mode=LEGACYHTML5 cache: false # 是否开启模板缓存,默认true,建议在开发时关闭缓存,不然没法看到实时 -# 将所有数字转为 String 类型返回,避免前端数据精度丢失的问题 + # 将所有数字转为 String 类型返回,避免前端数据精度丢失的问题 jackson: generator: write-numbers-as-strings: true @@ -16,10 +16,6 @@ spring: multipart: max-file-size: 1048576 -#缓存类型,ehcache(默认)、redis -cache: - type: ehcache - mybatis: configuration: #自动将数据库带下划线的表字段值映射到Java类的驼峰字段上 diff --git a/novel-crawl/src/main/resources/ehcache.xml b/novel-crawl/src/main/resources/ehcache.xml deleted file mode 100644 index 6ccf83d..0000000 --- a/novel-crawl/src/main/resources/ehcache.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/novel-front/src/main/resources/ehcache.xml b/novel-front/src/main/resources/ehcache.xml deleted file mode 100644 index 6ccf83d..0000000 --- a/novel-front/src/main/resources/ehcache.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file