|
|
@@ -1,63 +0,0 @@
|
|
|
-package com.dataeasy.server.utiis;
|
|
|
-
|
|
|
-import org.springframework.cache.Cache;
|
|
|
-import org.springframework.cache.CacheManager;
|
|
|
-
|
|
|
-/**
|
|
|
- * 缓存工具类
|
|
|
- */
|
|
|
-public class CacheUtils {
|
|
|
-
|
|
|
- private static CacheManager cacheManager;
|
|
|
-
|
|
|
- static {
|
|
|
- cacheManager = SpringUtils.getBean(CacheManager.class);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加缓存
|
|
|
- *
|
|
|
- * @param cacheName 缓存名称
|
|
|
- * @param key 缓存key
|
|
|
- * @param value 缓存值
|
|
|
- */
|
|
|
- public static void put(String cacheName, String key, Object value) {
|
|
|
- Cache cache = cacheManager.getCache(cacheName);
|
|
|
- cache.put(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取缓存(泛型)
|
|
|
- *
|
|
|
- * @param cacheName 缓存名称
|
|
|
- * @param key 缓存key
|
|
|
- * @param clazz 缓存类
|
|
|
- * @param <T> 返回值泛型
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> T get(String cacheName, String key, Class<T> clazz) {
|
|
|
- Cache cache = cacheManager.getCache(cacheName);
|
|
|
- if (cache == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- Cache.ValueWrapper wrapper = cache.get(key);
|
|
|
- if (wrapper == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return (T)wrapper.get();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 失效缓存
|
|
|
- *
|
|
|
- * @param cacheName 缓存名称
|
|
|
- * @param key 缓存key
|
|
|
- */
|
|
|
- public static void evict(String cacheName, String key) {
|
|
|
- Cache cache = cacheManager.getCache(cacheName);
|
|
|
- if (cache != null) {
|
|
|
- cache.evict(key);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|