- Add Caffeine local cache dependency for high-frequency access scenarios - Configure Spring Cache with Caffeine as primary CacheManager and Redis as secondary - Add @Cacheable/@CacheEvict annotations to SysDictTypeServiceImpl - Add @Cacheable/@CacheEvict annotations to SysConfigServiceImpl - Add @Cacheable/@CacheEvict annotations to SysUserServiceImpl - Create CacheUtils utility class for programmatic cache operations - Extend CacheController with cache management endpoints (list, clear)feature/20260628/refactor-frontend-modernization
parent
99ddd5d781
commit
7f500ae421
@ -0,0 +1,96 @@
|
|||||||
|
package com.ruoyi.common.core.cache;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.Cache;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存工具类
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class CacheUtils
|
||||||
|
{
|
||||||
|
@Autowired
|
||||||
|
private CacheManager cacheManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存
|
||||||
|
*
|
||||||
|
* @param cacheName 缓存名称
|
||||||
|
* @return Cache对象
|
||||||
|
*/
|
||||||
|
public Cache getCache(String cacheName)
|
||||||
|
{
|
||||||
|
return cacheManager.getCache(cacheName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存值
|
||||||
|
*
|
||||||
|
* @param cacheName 缓存名称
|
||||||
|
* @param key 缓存键
|
||||||
|
* @return 缓存值
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T get(String cacheName, Object key)
|
||||||
|
{
|
||||||
|
Cache cache = cacheManager.getCache(cacheName);
|
||||||
|
if (cache != null)
|
||||||
|
{
|
||||||
|
Cache.ValueWrapper wrapper = cache.get(key);
|
||||||
|
if (wrapper != null)
|
||||||
|
{
|
||||||
|
return (T) wrapper.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置缓存值
|
||||||
|
*
|
||||||
|
* @param cacheName 缓存名称
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param value 缓存值
|
||||||
|
*/
|
||||||
|
public void put(String cacheName, Object key, Object value)
|
||||||
|
{
|
||||||
|
Cache cache = cacheManager.getCache(cacheName);
|
||||||
|
if (cache != null)
|
||||||
|
{
|
||||||
|
cache.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除缓存
|
||||||
|
*
|
||||||
|
* @param cacheName 缓存名称
|
||||||
|
* @param key 缓存键
|
||||||
|
*/
|
||||||
|
public void evict(String cacheName, Object key)
|
||||||
|
{
|
||||||
|
Cache cache = cacheManager.getCache(cacheName);
|
||||||
|
if (cache != null)
|
||||||
|
{
|
||||||
|
cache.evict(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空缓存
|
||||||
|
*
|
||||||
|
* @param cacheName 缓存名称
|
||||||
|
*/
|
||||||
|
public void clear(String cacheName)
|
||||||
|
{
|
||||||
|
Cache cache = cacheManager.getCache(cacheName);
|
||||||
|
if (cache != null)
|
||||||
|
{
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue