CertPhotoSwitchComponent.java
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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);
}
}