package com.qmth.boot.core.cache.service.impl;

import com.qmth.boot.core.cache.service.CacheService;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.Collection;

public class CacheServiceImpl implements CacheService {

    private CacheManager cacheManager;

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

    @Override
    public Collection<String> endpoints() {
        return cacheManager.getCacheNames();
    }

    @Override
    public Object get(String endpoint, String key) {
        Cache.ValueWrapper wrapper = cacheManager.getCache(endpoint).get(key);
        return wrapper != null ? wrapper.get() : null;
    }

    @Override
    public void put(String endpoint, String key, Object value) {
        cacheManager.getCache(endpoint).put(key, value);
    }

    @Override
    public void evict(String endpoint, String key) {
        cacheManager.getCache(endpoint).evict(key);
    }

    @Override
    public void clear(String endpoint) {
        cacheManager.getCache(endpoint).clear();
    }
}
