Commit 08392c1d by 袁伟铭

1.0.0

parent 22e0be4a
......@@ -19,11 +19,17 @@ package com.zq.api.cache;
import java.util.Collection;
import java.util.Set;
/**
* 内存缓存接口
*
* @author wilmiam
* @since 2021-07-14 10:00
*/
public interface Cache {
/**
* 设置缓存名称
*
* <p>
* 2015年4月26日 下午8:31:14
*
* @param name
......@@ -33,7 +39,7 @@ public interface Cache {
/**
* 根据key获取缓存数据
*
* <p>
* 2015年4月26日 下午8:31:25
*
* @param key
......@@ -43,7 +49,7 @@ public interface Cache {
/**
* 添加缓存获取
*
* <p>
* 2015年4月26日 下午8:31:46
*
* @param key
......@@ -54,7 +60,7 @@ public interface Cache {
/**
* 移除缓存数据
*
* <p>
* 2015年4月26日 下午8:31:52
*
* @param key
......@@ -64,7 +70,7 @@ public interface Cache {
/**
* 清楚所有数据
*
* <p>
* 2015年4月26日 下午8:31:52
*
* @return
......@@ -73,7 +79,7 @@ public interface Cache {
/**
* 获取缓存数量
*
* <p>
* 2015年4月26日 下午8:31:59
*
* @return
......@@ -82,7 +88,7 @@ public interface Cache {
/**
* 返回数据key列表
*
* <p>
* 2017年1月18日 下午4:24:01
*
* @return
......@@ -91,7 +97,7 @@ public interface Cache {
/**
* 返回数据缓存列表
*
* <p>
* 2015年4月26日 下午8:33:11
*
* @return
......
......@@ -27,38 +27,35 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* 缓存管理接口
* <p>
* 2015年4月26日 下午8:47:45
*
* @author wilmiam
* @since 2021-07-14 10:00
*/
public class CacheManager {
private static ConcurrentHashMap<String, Cache> cacheManager = new ConcurrentHashMap<>();
static ICacheManager _CreateCache;
private static final ConcurrentHashMap<String, Cache> CACHE_MANAGER = new ConcurrentHashMap<>();
static ICacheManager createCache;
protected CacheManager() {
}
static {
_CreateCache = new ICacheManager() {
public Cache getCache() {
return new MemorySerializeCache(SerializerManage.getDefault());
}
};
createCache = () -> new MemorySerializeCache(SerializerManage.getDefault());
}
public static void setCache(ICacheManager thisCache) {
_CreateCache = thisCache;
createCache = thisCache;
}
public static Cache get(String name) {
Cache cache = cacheManager.get(name);
Cache cache = CACHE_MANAGER.get(name);
if (cache == null) {
synchronized (cacheManager) {
cache = cacheManager.get(name);
synchronized (CACHE_MANAGER) {
cache = CACHE_MANAGER.get(name);
if (cache == null) {
cache = _CreateCache.getCache();
cache = createCache.getCache();
cache.name(name);
cacheManager.put(name, cache);
CACHE_MANAGER.put(name, cache);
}
}
}
......@@ -66,15 +63,15 @@ public class CacheManager {
}
public static int size() {
return cacheManager.size();
return CACHE_MANAGER.size();
}
public static Collection<Cache> values() {
return cacheManager.values();
return CACHE_MANAGER.values();
}
public static Set<String> keys() {
return cacheManager.keySet();
return CACHE_MANAGER.keySet();
}
}
\ No newline at end of file
......@@ -16,8 +16,17 @@
package com.zq.api.cache;
/**
* @author wilmiam
* @since 2021-07-14 10:03
*/
public interface ICacheManager {
/**
* 获取缓存
*
* @return
*/
Cache getCache();
}
\ No newline at end of file
package com.zq.api.cache.impl;
import cn.hutool.core.util.StrUtil;
import com.zq.api.cache.Cache;
import com.zq.api.cache.CacheManager;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ApiCache {
private final static String cacheName = "ApiCache";
private static Cache cache;
public static void init() {
if (cache == null) {
log.info("####API Cache初始化......");
cache = CacheManager.get(cacheName);
}
}
public static <T> T getCache(String key) {
if (StrUtil.isEmpty(key)) {
return null;
}
init();
return cache.get(key);
}
@SuppressWarnings("unchecked")
public static <T> T removeCache(String key) {
if (StrUtil.isEmpty(key)) {
return null;
}
init();
return (T) cache.remove(key);
}
public static Cache addCache(String key, Object value) {
if (StrUtil.isEmpty(key)) {
return null;
}
init();
cache.add(key, value);
return cache;
}
}
/**
* Copyright 2015-2025 .
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zq.api.cache.impl;
import com.zq.api.cache.Cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* 内存
* <p>
* 2015年4月26日 下午8:24:11
*/
public class MemoryCache implements Cache {
protected String name;
protected Map<String, Object> map = new ConcurrentHashMap<String, Object>();
public MemoryCache() {
}
public String name() {
return name;
}
public MemoryCache name(String name) {
this.name = name;
return this;
}
public MemoryCache add(String key, Object value) {
map.put(key, value);
return this;
}
@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) map.get(key);
}
public Object remove(String key) {
return map.remove(key);
}
public void clear() {
map.clear();
}
public int size() {
return map.size();
}
public Set<String> keys() {
if (map.size() == 0) {
return null;
}
return map.keySet();
}
@SuppressWarnings("unchecked")
public <T> Collection<T> values() {
if (map.size() == 0) {
return null;
}
Collection<T> list = new ArrayList<T>();
for (Object obj : map.values()) {
list.add((T) obj);
}
return list;
}
}
......@@ -21,23 +21,20 @@ import com.zq.api.utils.serializable.Serializer;
import com.zq.api.utils.serializable.SerializerManage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 内存序列化
* <p>
* 2015年4月26日 下午8:24:23
*
* @author wilmiam
* @since 2021-07-14 10:00
*/
public class MemorySerializeCache implements Cache {
protected Serializer serializer;
protected String name;
protected Map<String, byte[]> map = new ConcurrentHashMap<String, byte[]>();
protected Serializer serializer;
protected Map<String, byte[]> map = new ConcurrentHashMap<>();
public MemorySerializeCache() {
this.serializer = SerializerManage.getDefault();
......@@ -51,70 +48,69 @@ public class MemorySerializeCache implements Cache {
return name;
}
@Override
public MemorySerializeCache name(String name) {
this.name = name;
return this;
}
@Override
public MemorySerializeCache add(String key, Object value) {
try {
map.put(key, serializer.serialize(value));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
@SuppressWarnings("unchecked")
@Override
public <T> T get(String key) {
try {
return (T) serializer.deserialize(map.get(key));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return serializer.deserialize(map.get(key));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object remove(String key) {
return map.remove(key);
}
@Override
public void clear() {
map.clear();
}
@Override
public int size() {
return map.size();
}
@Override
public Set<String> keys() {
if (map.size() == 0) {
return null;
return new HashSet<>();
}
return map.keySet();
}
@SuppressWarnings("unchecked")
@Override
public <T> Collection<T> values() {
Collection<T> list = new ArrayList<>();
if (map.size() == 0) {
return null;
return list;
}
Collection<T> list = new ArrayList<T>();
for (byte[] obj : map.values()) {
try {
list.add((T) serializer.deserialize(obj));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
list.add(serializer.deserialize(obj));
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
......
......@@ -6,14 +6,19 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
/**
* @author wilmiam
* @since 2021-07-13 09:50
*/
@Slf4j
@Configuration
public class ApiCommonConfig implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
log.info(">> 参数配置Cache初始化");
ConfigCache.init();
log.info("------完成初始化系统配置表-------");
log.info("<< 完成初始化系统配置表");
}
}
......@@ -5,17 +5,16 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author wilmiam
* @since 2021-07-13 09:54
*/
@Configuration
public class ApiInterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ApiInterceptor()).addPathPatterns("/api/**");
// .excludePathPatterns("/static/**")
// .excludePathPatterns("/login")
// .excludePathPatterns("/logout")
// .excludePathPatterns("/getImage")
// .excludePathPatterns("/do_login")
// .excludePathPatterns("/index");
}
}
......@@ -15,13 +15,14 @@ import java.util.Map;
/**
* 配置管理缓存
* <p>
* 2016年12月17日 下午11:26:25
*
* @author wilmiam
* @since 2021-07-14 10:42
*/
@Slf4j
public class ConfigCache {
private final static String cacheName = "ConfigCache";
private final static String CACHE_NAME = "ConfigCache";
private static Cache cache;
private ConfigCache() {
......@@ -29,9 +30,8 @@ public class ConfigCache {
public static void init() {
if (cache == null) {
cache = CacheManager.get(cacheName);
cache = CacheManager.get(CACHE_NAME);
}
log.info("####参数配置Cache初始化......");
Map<String, SysConfig> cacheMap = new HashMap<>();
SysConfigDao configDao = SpringContextHolder.getBean(SysConfigDao.class);
List<SysConfig> sysConfigList = configDao.selectList(Wrappers.lambdaQuery(null));
......
......@@ -7,10 +7,20 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "USER-SERVER", configuration = FeignConfig.class) //指定调用哪个微服务
/**
* @author wilmiam
* @since 2021-07-13 09:56
*/
@FeignClient(name = "USER-SERVER", configuration = FeignConfig.class)
@RequestMapping("/user/app")
public interface UserFeign {
/**
* 发送手机验证码
*
* @param phone
* @return
*/
@GetMapping(value = "/sendCode")
ResultVo sendCode(@RequestParam String phone);
......
......@@ -17,8 +17,9 @@ import java.util.TreeMap;
/**
* api 基础form
* <p>
* 2016年9月29日 上午10:29:27
*
* @author wilmiam
* @since 2021-07-13 09:59
*/
@Slf4j
@Data
......@@ -42,7 +43,8 @@ public class ApiForm {
public boolean parseBizContent() {
try {
boolean flag = ConfigCache.getValueToBoolean("API.PARAM.ENCRYPT"); // API参数是否加密
// API参数是否加密
boolean flag = ConfigCache.getValueToBoolean("API.PARAM.ENCRYPT");
if (StrUtil.isNotBlank(bizContent) && flag) {
bizContent = ApiUtils.decode(bizContent);
}
......
......@@ -16,6 +16,10 @@ import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
/**
* @author wilmiam
* @since 2021-07-14 10:46
*/
@Slf4j
@Component
public class ApiInterceptor extends HandlerInterceptorAdapter {
......@@ -69,13 +73,11 @@ public class ApiInterceptor extends HandlerInterceptorAdapter {
// 调试日志
if (ApiUtils.DEBUG) {
log.info("API DEBUG INTERCEPTOR \n[path=" + uri + "/" + queryString + "]" //
+ "[from:" + form.toString() + "]" //
log.info("API DEBUG INTERCEPTOR \n[path=" + uri + "/" + queryString + "]"
+ "[from:" + form + "]"
+ "\n[time=" + (System.currentTimeMillis() - start) + "ms]");
}
return true;
}
......
......@@ -5,6 +5,10 @@ import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author wilmiam
* @since 2021-07-13 09:54
*/
@Component
@RequiredArgsConstructor
public class ConfigTask {
......@@ -12,7 +16,7 @@ public class ConfigTask {
@Scheduled(cron = "0/5 * * * * ?")
public void updateConfig() {
ConfigCache.init();
ConfigCache.update();
}
}
......@@ -16,14 +16,24 @@
package com.zq.api.utils.serializable;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
/**
* @author wilmiam
* @since 2021-07-14 10:06
*/
@Slf4j
public class JavaSerializer implements Serializer {
@Override
public String name() {
return "java";
}
@Override
public byte[] serialize(Object obj) throws IOException {
ObjectOutputStream oos = null;
try {
......@@ -32,33 +42,27 @@ public class JavaSerializer implements Serializer {
oos.writeObject(obj);
return baos.toByteArray();
} finally {
if (oos != null)
try {
oos.close();
} catch (IOException e) {
}
IoUtil.close(oos);
}
}
@Override
@SuppressWarnings("unchecked")
public <T> T deserialize(byte[] bits) throws IOException {
if (bits == null || bits.length == 0)
if (bits == null || bits.length == 0) {
return null;
}
ObjectInputStream ois = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bits);
ois = new ObjectInputStream(bais);
return (T) ois.readObject();
} catch (ClassNotFoundException e) {
throw new IOException("没有找到类型", e);
log.error("内存反序列化没有找到类型");
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
throw new IOException("deserialize关闭失败", e);
}
IoUtil.close(ois);
}
return null;
}
}
......@@ -18,12 +18,36 @@ package com.zq.api.utils.serializable;
import java.io.IOException;
/**
* @author wilmiam
* @since 2021-07-14 10:04
*/
public interface Serializer {
/**
* 获取序列化名称
*
* @return
*/
String name();
/**
* 序列化
*
* @param obj
* @return
* @throws IOException
*/
byte[] serialize(Object obj) throws IOException;
/**
* 反序列化
*
* @param bytes
* @param <T>
* @return
* @throws IOException
*/
<T> T deserialize(byte[] bytes) throws IOException;
}
......@@ -16,12 +16,17 @@
package com.zq.api.utils.serializable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author wilmiam
* @since 2021-07-14 10:03
*/
public class SerializerManage {
private static final Map<String, Serializer> map = new HashMap<>();
private static final Map<String, Serializer> MAP = new HashMap<>();
private static String DEFAULT_KEY;
static {
......@@ -43,26 +48,26 @@ public class SerializerManage {
* @param serializer
*/
public static void add(String key, Serializer serializer) {
map.put(key, serializer);
MAP.put(key, serializer);
}
public static void add(Serializer serializer) {
map.put(serializer.name(), serializer);
MAP.put(serializer.name(), serializer);
}
public static Serializer get(String key) {
return map.get(key);
return MAP.get(key);
}
public static Serializer getDefault() {
return map.get(DEFAULT_KEY);
return MAP.get(DEFAULT_KEY);
}
public static byte[] serialize(Object obj) throws Exception {
public static byte[] serialize(Object obj) throws IOException {
return getDefault().serialize(obj);
}
public static Object deserialize(byte[] bytes) throws Exception {
public static Object deserialize(byte[] bytes) throws IOException {
return getDefault().deserialize(bytes);
}
}
......@@ -37,7 +37,7 @@ public class SecureRandomStringUtils {
/**
* 随机对象帮助类
* Get strong enough SecureRandom instance and of the checked exception.
* TODO Try {@code NativePRNGNonBlocking} and failover to default SHA1PRNG until Java 9.
* Try {@code NativePRNGNonBlocking} and failover to default SHA1PRNG until Java 9.
*
* @return the strong instance
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment