mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
refactor: 移除 Ehcache,默认缓存设置为 Redis
This commit is contained in:
parent
282bc7b707
commit
29d074ec18
@ -200,10 +200,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-cache</artifactId>
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>net.sf.ehcache</groupId>
|
|
||||||
<artifactId>ehcache</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!--war包部署需要-->
|
<!--war包部署需要-->
|
||||||
<!--<dependency>-->
|
<!--<dependency>-->
|
||||||
|
@ -5,11 +5,8 @@ import com.java2nb.common.redis.shiro.RedisCacheManager;
|
|||||||
import com.java2nb.common.redis.shiro.RedisManager;
|
import com.java2nb.common.redis.shiro.RedisManager;
|
||||||
import com.java2nb.common.redis.shiro.RedisSessionDAO;
|
import com.java2nb.common.redis.shiro.RedisSessionDAO;
|
||||||
import com.java2nb.system.shiro.UserRealm;
|
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.mgt.SecurityManager;
|
||||||
import org.apache.shiro.session.SessionListener;
|
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.session.mgt.eis.SessionDAO;
|
||||||
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
||||||
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
||||||
@ -29,6 +26,7 @@ import java.util.LinkedHashMap;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class ShiroConfig {
|
public class ShiroConfig {
|
||||||
|
|
||||||
@Value("${spring.redis.host}")
|
@Value("${spring.redis.host}")
|
||||||
private String host;
|
private String host;
|
||||||
@Value("${spring.redis.password}")
|
@Value("${spring.redis.password}")
|
||||||
@ -38,9 +36,6 @@ public class ShiroConfig {
|
|||||||
@Value("${spring.redis.timeout}")
|
@Value("${spring.redis.timeout}")
|
||||||
private int timeout;
|
private int timeout;
|
||||||
|
|
||||||
@Value("${spring.cache.type}")
|
|
||||||
private String cacheType ;
|
|
||||||
|
|
||||||
@Value("${server.session-timeout}")
|
@Value("${server.session-timeout}")
|
||||||
private int tomcatTimeout;
|
private int tomcatTimeout;
|
||||||
|
|
||||||
@ -92,11 +87,7 @@ public class ShiroConfig {
|
|||||||
//设置realm.
|
//设置realm.
|
||||||
securityManager.setRealm(userRealm());
|
securityManager.setRealm(userRealm());
|
||||||
// 自定义缓存实现 使用redis
|
// 自定义缓存实现 使用redis
|
||||||
if (Constant.CACHE_TYPE_REDIS.equals(cacheType)) {
|
|
||||||
securityManager.setCacheManager(rediscacheManager());
|
securityManager.setCacheManager(rediscacheManager());
|
||||||
} else {
|
|
||||||
securityManager.setCacheManager(ehCacheManager());
|
|
||||||
}
|
|
||||||
securityManager.setSessionManager(sessionManager());
|
securityManager.setSessionManager(sessionManager());
|
||||||
return securityManager;
|
return securityManager;
|
||||||
}
|
}
|
||||||
@ -108,8 +99,7 @@ public class ShiroConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开启shiro aop注解支持.
|
* 开启shiro aop注解支持. 使用代理方式;所以需要开启代码支持;
|
||||||
* 使用代理方式;所以需要开启代码支持;
|
|
||||||
*
|
*
|
||||||
* @param securityManager
|
* @param securityManager
|
||||||
* @return
|
* @return
|
||||||
@ -138,8 +128,7 @@ public class ShiroConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cacheManager 缓存 redis实现
|
* cacheManager 缓存 redis实现 使用的是shiro-redis开源插件
|
||||||
* 使用的是shiro-redis开源插件
|
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -151,8 +140,7 @@ public class ShiroConfig {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RedisSessionDAO shiro sessionDao层的实现 通过redis
|
* RedisSessionDAO shiro sessionDao层的实现 通过redis 使用的是shiro-redis开源插件
|
||||||
* 使用的是shiro-redis开源插件
|
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisSessionDAO redisSessionDAO() {
|
public RedisSessionDAO redisSessionDAO() {
|
||||||
@ -163,11 +151,7 @@ public class ShiroConfig {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SessionDAO sessionDAO() {
|
public SessionDAO sessionDAO() {
|
||||||
if (Constant.CACHE_TYPE_REDIS.equals(cacheType)) {
|
|
||||||
return redisSessionDAO();
|
return redisSessionDAO();
|
||||||
} else {
|
|
||||||
return new MemorySessionDAO();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -184,19 +168,4 @@ public class ShiroConfig {
|
|||||||
return sessionManager;
|
return sessionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public EhCacheManager ehCacheManager() {
|
|
||||||
EhCacheManager em = new EhCacheManager();
|
|
||||||
em.setCacheManager(cacheManager());
|
|
||||||
return em;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("cacheManager2")
|
|
||||||
CacheManager cacheManager(){
|
|
||||||
return CacheManager.create();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ spring:
|
|||||||
redis:
|
redis:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 6379
|
port: 6379
|
||||||
password: test
|
password: test123456
|
||||||
# 连接超时时间(毫秒)
|
# 连接超时时间(毫秒)
|
||||||
timeout: 10000
|
timeout: 10000
|
||||||
jedis:
|
jedis:
|
||||||
|
@ -24,10 +24,7 @@ spring:
|
|||||||
devtools:
|
devtools:
|
||||||
restart:
|
restart:
|
||||||
enabled: true
|
enabled: true
|
||||||
cache:
|
|
||||||
type: ehcache
|
|
||||||
ehcache:
|
|
||||||
config: classpath:ehcache.xml
|
|
||||||
mybatis:
|
mybatis:
|
||||||
configuration:
|
configuration:
|
||||||
#自动将数据库带下划线的表字段值映射到Java类的驼峰字段上
|
#自动将数据库带下划线的表字段值映射到Java类的驼峰字段上
|
||||||
|
@ -32,12 +32,6 @@
|
|||||||
<artifactId>spring-boot-starter-cache</artifactId>
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--ehcache-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>net.sf.ehcache</groupId>
|
|
||||||
<artifactId>ehcache</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!--集成redis-->
|
<!--集成redis-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ 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 lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -12,7 +11,6 @@ import java.util.concurrent.TimeUnit;
|
|||||||
/**
|
/**
|
||||||
* @author xxy
|
* @author xxy
|
||||||
*/
|
*/
|
||||||
@ConditionalOnProperty(prefix = "cache", name = "type", havingValue = "redis")
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Service
|
@Service
|
||||||
public class RedisServiceImpl implements CacheService {
|
public class RedisServiceImpl implements CacheService {
|
||||||
|
@ -9,7 +9,7 @@ spring:
|
|||||||
#Redis服务器连接端口
|
#Redis服务器连接端口
|
||||||
port: 6379
|
port: 6379
|
||||||
#Redis服务器连接密码
|
#Redis服务器连接密码
|
||||||
password: test
|
password: test123456
|
||||||
jedis:
|
jedis:
|
||||||
pool:
|
pool:
|
||||||
#连接池最大连接数(使用负值表示没有限制)
|
#连接池最大连接数(使用负值表示没有限制)
|
||||||
|
@ -16,10 +16,6 @@ spring:
|
|||||||
multipart:
|
multipart:
|
||||||
max-file-size: 1048576
|
max-file-size: 1048576
|
||||||
|
|
||||||
#缓存类型,ehcache(默认)、redis
|
|
||||||
cache:
|
|
||||||
type: ehcache
|
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
configuration:
|
configuration:
|
||||||
#自动将数据库带下划线的表字段值映射到Java类的驼峰字段上
|
#自动将数据库带下划线的表字段值映射到Java类的驼峰字段上
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:noNamespaceSchemaLocation="ehcache.xsd">
|
|
||||||
<!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
|
|
||||||
<!--timeToLiveSeconds 当缓存存活n秒后销毁 -->
|
|
||||||
<!-- 缓存配置 name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。
|
|
||||||
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
|
|
||||||
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
|
|
||||||
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
|
|
||||||
maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk
|
|
||||||
store persists between restarts of the Virtual Machine. The default value
|
|
||||||
is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是
|
|
||||||
LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 maxEntriesLocalHeap="1000"
|
|
||||||
: 堆内存中最大缓存对象数,0没有限制(必须设置) maxEntriesLocalDisk="1000" : 硬盘最大缓存个数。 -->
|
|
||||||
<!-- 磁盘缓存位置 -->
|
|
||||||
<diskStore path="user.dir/cachedata" />
|
|
||||||
|
|
||||||
<!-- 默认缓存 -->
|
|
||||||
<defaultCache maxElementsInMemory="10000" eternal="false"
|
|
||||||
timeToIdleSeconds="120" timeToLiveSeconds="120"
|
|
||||||
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
|
|
||||||
memoryStoreEvictionPolicy="LRU">
|
|
||||||
|
|
||||||
<persistence strategy="localTempSwap" />
|
|
||||||
</defaultCache>
|
|
||||||
|
|
||||||
|
|
||||||
<cache name="util_cache" maxEntriesLocalHeap="0" eternal="true"
|
|
||||||
overflowToDisk="true" diskPersistent="true" />
|
|
||||||
|
|
||||||
</ehcache>
|
|
@ -1,30 +0,0 @@
|
|||||||
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:noNamespaceSchemaLocation="ehcache.xsd">
|
|
||||||
<!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
|
|
||||||
<!--timeToLiveSeconds 当缓存存活n秒后销毁 -->
|
|
||||||
<!-- 缓存配置 name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。
|
|
||||||
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
|
|
||||||
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
|
|
||||||
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
|
|
||||||
maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk
|
|
||||||
store persists between restarts of the Virtual Machine. The default value
|
|
||||||
is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是
|
|
||||||
LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 maxEntriesLocalHeap="1000"
|
|
||||||
: 堆内存中最大缓存对象数,0没有限制(必须设置) maxEntriesLocalDisk="1000" : 硬盘最大缓存个数。 -->
|
|
||||||
<!-- 磁盘缓存位置 -->
|
|
||||||
<diskStore path="user.dir/cachedata" />
|
|
||||||
|
|
||||||
<!-- 默认缓存 -->
|
|
||||||
<defaultCache maxElementsInMemory="10000" eternal="false"
|
|
||||||
timeToIdleSeconds="120" timeToLiveSeconds="120"
|
|
||||||
maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
|
|
||||||
memoryStoreEvictionPolicy="LRU">
|
|
||||||
|
|
||||||
<persistence strategy="localTempSwap" />
|
|
||||||
</defaultCache>
|
|
||||||
|
|
||||||
|
|
||||||
<cache name="util_cache" maxEntriesLocalHeap="0" eternal="true"
|
|
||||||
overflowToDisk="true" diskPersistent="true" />
|
|
||||||
|
|
||||||
</ehcache>
|
|
Loading…
x
Reference in New Issue
Block a user