CertPhotoSwitchComponent.java 1.95 KB
package com.yohoufo.user.component;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yoho.core.cache.LocalCache;
import com.yoho.core.config.ConfigReader;
import com.yoho.core.config.ZKConfigWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 调用百度云实名认证接口的开发
 */
@Component
public class CertPhotoSwitchComponent {
    private final Logger logger = LoggerFactory.getLogger(CertPhotoSwitchComponent.class);

    @Autowired
    private ConfigReader configReader;

    @Autowired
    private ZKConfigWriter zkConfigWriter;

    //缓存
    private Cache<String,Boolean> localCache =  CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(2, TimeUnit.MINUTES).build();
    private  final String SWITCH_CACHE_KEY = "certPhotoSwitchCacheKey";
    private  final String zkValue = "ufo.order.certPhotoSwitchCacheKey";
    private Boolean defaultSwitch = Boolean.TRUE;

    public boolean getCertPhotoSwitch(){
        try{
            return localCache.get(SWITCH_CACHE_KEY, new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    return configReader.getBoolean(zkValue,defaultSwitch);
                }
            });
        }catch (ExecutionException e){
            logger.warn("StoreSellerServiceImpl getCertPhotoSwitch error ", e);
            return defaultSwitch;
        }
    }

    public void writeZkValueToFalse(){
        logger.info("StoreSellerServiceImpl writeZkValueToFalse");
        zkConfigWriter.setConfig(zkValue,"false");
        localCache.invalidate(SWITCH_CACHE_KEY);
    }
}