部分代码重构

This commit is contained in:
xiaoyang
2021-08-16 15:42:56 +08:00
parent 4939bcf418
commit 3520200a87
28 changed files with 234 additions and 263 deletions

View File

@ -16,108 +16,107 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Service
public class EhCacheServiceImpl implements CacheService {
private final CacheManager cacheManager ;
/**
* 获得一个Cache没有则创建一个。
* @return
*/
private Cache getCache(){
Cache cache = cacheManager.getCache("util_cache");
return cache;
}
@Override
public String get(String key) {
Element element = getCache().get(key);
return element==null?null:(String)element.getObjectValue();
}
@Override
public void set(String key, String value) {
Element element = new Element(key, value);
Cache cache = getCache();
//不过期
cache.getCacheConfiguration().setEternal(true);
cache.put(element);
}
@Override
public void set(String key, String value, long timeout) {
Element element = new Element(key, value);
element.setTimeToLive((int) timeout);
Cache cache = getCache();
cache.put(element);
}
@Override
public void del(String key) {
getCache().remove(key);
private final CacheManager cacheManager;
}
/**
* 获得一个Cache没有则创建一个。
*
* @return
*/
private Cache getCache() {
@Override
public boolean contains(String key) {
return getCache().isKeyInCache(key);
}
@Override
public void expire(String key, long timeout) {
Element element = getCache().get(key);
if (element != null) {
Object value = element.getValue();
element = new Element(key, value);
element.setTimeToLive((int)timeout);
Cache cache = getCache();
cache.put(element);
}
}
Cache cache = cacheManager.getCache("util_cache");
return cache;
}
/**
* 根据key获取缓存的Object类型数据
*/
@Override
public Object getObject(String key) {
Element element = getCache().get(key);
return element==null?null:element.getObjectValue();
}
@Override
public String get(String key) {
Element element = getCache().get(key);
return element == null ? null : (String) element.getObjectValue();
}
@Override
public void set(String key, String value) {
Element element = new Element(key, value);
Cache cache = getCache();
//不过期
cache.getCacheConfiguration().setEternal(true);
cache.put(element);
}
@Override
public void set(String key, String value, long timeout) {
Element element = new Element(key, value);
element.setTimeToLive((int) timeout);
Cache cache = getCache();
cache.put(element);
}
@Override
public void del(String key) {
getCache().remove(key);
/**
* 设置Object类型的缓存
*/
@Override
public void setObject(String key, Object value) {
Element element = new Element(key, value);
Cache cache = getCache();
//不过期
cache.getCacheConfiguration().setEternal(true);
cache.put(element);
}
}
@Override
public boolean contains(String key) {
return getCache().isKeyInCache(key);
}
@Override
public void expire(String key, long timeout) {
Element element = getCache().get(key);
if (element != null) {
Object value = element.getValue();
element = new Element(key, value);
element.setTimeToLive((int) timeout);
Cache cache = getCache();
cache.put(element);
}
}
/**
* 设置一个有过期时间的Object类型的缓存,单位秒
*/
@Override
public void setObject(String key, Object value, long timeout) {
Element element = new Element(key, value);
element.setTimeToLive((int) timeout);
Cache cache = getCache();
cache.put(element);
}
/**
* 根据key获取缓存的Object类型数据
*/
@Override
public Object getObject(String key) {
Element element = getCache().get(key);
return element == null ? null : element.getObjectValue();
}
/**
* 设置Object类型的缓存
*/
@Override
public void setObject(String key, Object value) {
Element element = new Element(key, value);
Cache cache = getCache();
//不过期
cache.getCacheConfiguration().setEternal(true);
cache.put(element);
}
/**
* 设置一个有过期时间的Object类型的缓存,单位秒
*/
@Override
public void setObject(String key, Object value, long timeout) {
Element element = new Element(key, value);
element.setTimeToLive((int) timeout);
Cache cache = getCache();
cache.put(element);
}
}

View File

@ -19,7 +19,7 @@ public class BeanUtil {
* @return 新集合
* */
@SneakyThrows
public static <T> List<T> copyList(List source,Class<T> targetClass){
public static <T> List<T> copyList(List<? super T> source,Class<T> targetClass){
List<T> target = new ArrayList<>(source.size());
for( int i = 0 ; i < source.size() ; i++){
Object sourceItem = source.get(i);