fix(添加本地缓存和mybatisplus 配置文件):
This commit is contained in:
parent
c923155638
commit
3a20f9b39d
|
@ -0,0 +1,142 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
|
||||
|
||||
<!-- 磁盘缓存位置 -->
|
||||
<diskStore path="java.io.tmpdir"/>
|
||||
|
||||
<!-- 缓存配置
|
||||
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: 内存数量最大时是否清除。
|
||||
-->
|
||||
|
||||
<!-- 默认缓存 -->
|
||||
<defaultCache
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="120"
|
||||
timeToLiveSeconds="120"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</defaultCache>
|
||||
|
||||
<!-- 临时cache -->
|
||||
<cache name="temp_cache"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="1800"
|
||||
timeToLiveSeconds="1800"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 持久化cache -->
|
||||
<cache name="eternal_cache"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="true"
|
||||
timeToIdleSeconds="120"
|
||||
timeToLiveSeconds="120"
|
||||
overflowToDisk="true"
|
||||
diskPersistent="true"
|
||||
diskExpiryThreadIntervalSeconds="10">
|
||||
</cache>
|
||||
|
||||
<!-- 字典cache -->
|
||||
<cache name="sys_dict"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="0"
|
||||
timeToLiveSeconds="0"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 参数cache -->
|
||||
<cache name="sys_config"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="0"
|
||||
timeToLiveSeconds="0"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 判断请求重复缓存cache -->
|
||||
<cache name="repeat_submit"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="5"
|
||||
timeToLiveSeconds="5"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 验证码缓存cache -->
|
||||
<cache name="captcha_codes"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="180"
|
||||
timeToLiveSeconds="180"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 用户缓存cache -->
|
||||
<cache name="login_tokens"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="1800"
|
||||
timeToLiveSeconds="1800"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 限流缓存cache -->
|
||||
<cache name="rate_limit"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="180"
|
||||
timeToLiveSeconds="180"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
<!-- 密码错误次数缓存cache -->
|
||||
<cache name="pwd_err_cnt"
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="1800"
|
||||
timeToLiveSeconds="1800"
|
||||
maxElementsOnDisk="10000000"
|
||||
diskExpiryThreadIntervalSeconds="120"
|
||||
memoryStoreEvictionPolicy="LRU">
|
||||
<persistence strategy="localTempSwap"/>
|
||||
</cache>
|
||||
|
||||
</ehcache>
|
|
@ -0,0 +1,200 @@
|
|||
package com.ruoyi.common.utils;
|
||||
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.ehcache.EhCacheCache;
|
||||
import org.springframework.cache.transaction.TransactionAwareCacheDecorator;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CacheUtils
|
||||
{
|
||||
/**
|
||||
* 使用redis时对redis进行单独特殊操作需要使用
|
||||
*
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
* @return
|
||||
*/
|
||||
public static <K, V> RedisTemplate<K, V> getRedisTemplate()
|
||||
{
|
||||
return SpringUtils.getBean("redisTemplate");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取CacheManager
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static CacheManager getCacheManager()
|
||||
{
|
||||
return SpringUtils.getBean(CacheManager.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据cacheName从CacheManager中获取cache
|
||||
*
|
||||
* @param cacheName
|
||||
* @return
|
||||
*/
|
||||
public static Cache getCache(String cacheName)
|
||||
{
|
||||
return getCacheManager().getCache(cacheName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的所有key值(由于springcache不支持获取所有key,只能根据cache类型来单独获取)
|
||||
*
|
||||
* @param cacheName
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings(value = { "unchecked" })
|
||||
public static Set<String> getkeys(String cacheName)
|
||||
{
|
||||
Cache cache = getCacheManager().getCache(cacheName);
|
||||
Set<String> keyset = new HashSet<>();
|
||||
if (cache instanceof EhCacheCache)
|
||||
{
|
||||
EhCacheCache ehcache = (EhCacheCache) cache;
|
||||
keyset = new HashSet<>(ehcache.getNativeCache().getKeys());
|
||||
}
|
||||
else if (cache instanceof TransactionAwareCacheDecorator)
|
||||
{
|
||||
Set<Object> keysets = getRedisTemplate().keys(cache.getName() + "*");
|
||||
for (Object s : keysets)
|
||||
{
|
||||
keyset.add(StringUtils.replace(s.toString(), cache.getName() + ":", ""));
|
||||
}
|
||||
}
|
||||
return keyset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据cacheName,key缓存数据
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param value
|
||||
* @param <T>
|
||||
*/
|
||||
public static <T> void put(String cacheName, String key, T value)
|
||||
{
|
||||
put(cacheName, key, value, 0, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果没有则进行缓存,根据cacheName,key缓存数据
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param value
|
||||
* @param <T>
|
||||
*/
|
||||
public static <T> void putIfAbsent(String cacheName, String key, T value)
|
||||
{
|
||||
if (ObjectUtils.isEmpty(get(cacheName, key)))
|
||||
{
|
||||
put(cacheName, key, value, 0, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据cacheName,key和缓存过期时间进行缓存数据,使用各种不同缓存可以单独进行操作
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param value
|
||||
* @param timeout
|
||||
* @param unit
|
||||
* @param <T>
|
||||
*/
|
||||
public static <T> void put(String cacheName, String key, T value, long timeout, TimeUnit unit)
|
||||
{
|
||||
Cache cache = getCacheManager().getCache(cacheName);
|
||||
if (cache instanceof EhCacheCache)
|
||||
{
|
||||
EhCacheCache ehcache = (EhCacheCache) cache;
|
||||
ehcache.put(key, value);
|
||||
}
|
||||
else if (cache instanceof TransactionAwareCacheDecorator)
|
||||
{
|
||||
if (timeout != 0 && unit != null)
|
||||
{
|
||||
getRedisTemplate().opsForValue().set(cacheName + ":" + key, value, timeout, unit);
|
||||
}
|
||||
else
|
||||
{
|
||||
getRedisTemplate().opsForValue().set(cacheName + ":" + key, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cache.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Cache.ValueWrapper get(String cacheName, String key)
|
||||
{
|
||||
return getCacheManager().getCache(cacheName).get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取数据
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @param type
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T get(String cacheName, String key, @Nullable Class<T> type)
|
||||
{
|
||||
return getCacheManager().getCache(cacheName).get(key, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除缓存数据
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
*/
|
||||
public static void remove(String cacheName, String key)
|
||||
{
|
||||
getCacheManager().getCache(cacheName).evict(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果存在则移除缓存数据
|
||||
*
|
||||
* @param cacheName
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static boolean removeIfPresent(String cacheName, String key)
|
||||
{
|
||||
remove(cacheName, key);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存名称为cacheName的所有缓存数据
|
||||
*
|
||||
* @param cacheName
|
||||
*/
|
||||
public static void clear(String cacheName)
|
||||
{
|
||||
getCacheManager().getCache(cacheName).clear();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.ruoyi.framework.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* Mybatis Plus 配置
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@EnableTransactionManagement(proxyTargetClass = true)
|
||||
@Configuration
|
||||
public class MybatisPlusConfig
|
||||
{
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor()
|
||||
{
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// 分页插件
|
||||
interceptor.addInnerInterceptor(paginationInnerInterceptor());
|
||||
// 乐观锁插件
|
||||
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
|
||||
// 阻断插件
|
||||
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
|
||||
*/
|
||||
public PaginationInnerInterceptor paginationInnerInterceptor()
|
||||
{
|
||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
||||
// 设置数据库类型为mysql
|
||||
paginationInnerInterceptor.setDbType(DbType.MYSQL);
|
||||
// 设置最大单页限制数量,默认 500 条,-1 不受限制
|
||||
paginationInnerInterceptor.setMaxLimit(-1L);
|
||||
return paginationInnerInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
|
||||
*/
|
||||
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
|
||||
{
|
||||
return new OptimisticLockerInnerInterceptor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
|
||||
*/
|
||||
public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
|
||||
{
|
||||
return new BlockAttackInnerInterceptor();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue