All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.cache.DefaultCacheHelper Maven / Gradle / Ivy

package io.github.dengchen2020.cache;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

/**
 * 默认的缓存操作实现
 *
 * @author dengchen
 * @since 2024/5/29
 */
public class DefaultCacheHelper implements CacheHelper {

    private final CacheManager cacheManager;

    public DefaultCacheHelper(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    /**
     * 移除cacheName指定key的缓存
     *
     * @param cacheName 缓存名
     * @param key       key
     */
    @Override
    public void evict(String cacheName, Object key) {
        evict(new String[]{cacheName}, key);
    }

    /**
     * 移除cacheName指定key的缓存
     *
     * @param cacheNames 缓存名
     * @param key        key
     */
    @Override
    public void evict(String[] cacheNames, Object key) {
        for (String s : cacheNames) {
            Cache cache = cacheManager.getCache(s);
            if (cache != null && cache.get(key) != null) cache.evict(key);
        }
    }

    /**
     * 移除cacheName的所有缓存
     *
     * @param cacheName 缓存名
     */
    @Override
    public void clear(String cacheName) {
        clear(new String[]{cacheName});
    }

    /**
     * 移除cacheName的所有缓存
     *
     * @param cacheNames 缓存名
     */
    @Override
    public void clear(String[] cacheNames) {
        for (String s : cacheNames) {
            Cache cache = cacheManager.getCache(s);
            if (cache != null) cache.clear();
        }
    }

    /**
     * 清除所有缓存
     */
    @Override
    public void clearAll() {
        for (String s : cacheManager.getCacheNames()) {
            Cache cache = cacheManager.getCache(s);
            if (cache != null) cache.clear();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy