parent
83de265d7e
commit
9f8d19c5c7
@ -0,0 +1,52 @@
|
||||
package net.educoder.config;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.educoder.constant.CommonConstants;
|
||||
import net.educoder.util.JCloudUtil;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author: youys
|
||||
* @Date: 2022/8/17
|
||||
* @Description: 缓存配置类
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class CacheConfig {
|
||||
|
||||
@Value("${jcloud.password}")
|
||||
private String username;
|
||||
|
||||
@Value("${jcloud.password}")
|
||||
private String password;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Cache<String, String> cache = guavaCache();
|
||||
String apiToken = JCloudUtil.getApiToken(username, password);
|
||||
log.info("token init--------{}", apiToken);
|
||||
cache.put(CommonConstants.API_TOKEN, apiToken);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Cache<String, String> guavaCache() {
|
||||
|
||||
return CacheBuilder.newBuilder()
|
||||
.concurrencyLevel(8)
|
||||
.initialCapacity(100)
|
||||
.maximumSize(100)
|
||||
.expireAfterWrite(1, TimeUnit.HOURS)
|
||||
.removalListener(notify -> {
|
||||
log.info("缓存key:{}, value:{}, 被移除原因:{}", notify.getKey(), notify.getValue(), notify.getCause());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package net.educoder.constant;
|
||||
|
||||
/**
|
||||
* @Author: youys
|
||||
* @Date: 2022/8/17
|
||||
* @Description: 常量
|
||||
*/
|
||||
public class CommonConstants {
|
||||
|
||||
public static final String SUCCESS = "success";
|
||||
|
||||
public static final String FAILED = "failed";
|
||||
|
||||
public static final String API_TOKEN = "api_token";
|
||||
|
||||
|
||||
public static final String DEFAULT_PASSWORD = "Educoder_123123";
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package net.educoder.util;
|
||||
|
||||
import lombok.Data;
|
||||
import net.educoder.exception.BusinessException;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author: yys
|
||||
* @Date: 2018/6/4
|
||||
* @description:
|
||||
*/
|
||||
@Data
|
||||
public class ResponseResult<T> implements Serializable {
|
||||
|
||||
private int code;
|
||||
private String desc;
|
||||
private T results;
|
||||
|
||||
public ResponseResult() {
|
||||
this.code = 1;
|
||||
this.desc = "成功";
|
||||
}
|
||||
|
||||
public ResponseResult(int code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public ResponseResult(int code, String desc, T results) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
public static ResponseResult success() {
|
||||
return new ResponseResult();
|
||||
}
|
||||
|
||||
public static <T> ResponseResult success(T obj) {
|
||||
return new ResponseResult(1, "成功", obj);
|
||||
}
|
||||
|
||||
public static ResponseResult failed(int code, String desc) {
|
||||
return new ResponseResult(code, desc);
|
||||
}
|
||||
|
||||
|
||||
public static ResponseResult failed(BusinessException be) {
|
||||
return new ResponseResult(be.getCode(), be.getMsg());
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue