Skip to content

Commit fa2657b

Browse files
author
xuebing feng
committed
自己写的工具类
1 parent 2974959 commit fa2657b

File tree

5 files changed

+759
-0
lines changed

5 files changed

+759
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
package com.example.awaysuse.util;
2+
3+
4+
import com.alibaba.fastjson.JSONArray;
5+
import com.alibaba.fastjson.JSONObject;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.stereotype.Component;
8+
import uyun.whale.common.encryption.support.HttpUtils;
9+
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
import java.util.Timer;
14+
import java.util.TimerTask;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
import java.util.function.Function;
17+
import java.util.stream.Collectors;
18+
19+
/**
20+
* <pre>
21+
* 基于concurrentHash的本地缓存工具类
22+
* 缓存删除基于timer定时器
23+
* <pre>
24+
* @author hejianfeng
25+
* @date 2019/10/5
26+
* @param
27+
* @return
28+
* <pre>
29+
* 修改记录
30+
* 版本号 修订日期 修改人 bug编号 修改内容
31+
* 1.0.0 2019/10/5 hejianfeng 新建
32+
* </pre>
33+
*/
34+
@Component
35+
public class CacheUtil {
36+
37+
//默认大小
38+
private static final int DEFAULT_CAPACITY = 1024;
39+
40+
// 最大缓存大小
41+
private static final int MAX_CAPACITY = 10000;
42+
43+
//默认缓存过期时间
44+
private static final long DEFAULT_TIMEOUT = 3600;
45+
46+
//1000毫秒
47+
private static final long SECOND_TIME = 1000;
48+
49+
//存储缓存的Map
50+
private static final ConcurrentHashMap<String, Object> map;
51+
52+
private static final Timer timer;
53+
54+
static {
55+
map = new ConcurrentHashMap<>(DEFAULT_CAPACITY);
56+
timer = new Timer();
57+
}
58+
59+
//私有化构造方法
60+
private CacheUtil() {
61+
62+
}
63+
64+
/**
65+
* <pre>
66+
* 缓存任务清除类
67+
* <pre>
68+
* @author hejianfeng
69+
* @date 2019/10/5
70+
* @param
71+
* @return
72+
* <pre>
73+
* 修改记录
74+
* 版本号 修订日期 修改人 bug编号 修改内容
75+
* 1.0.0 2019/10/5 hejianfeng 新建
76+
* </pre>
77+
*/
78+
static class ClearTask extends TimerTask {
79+
private String key;
80+
81+
public ClearTask(String key) {
82+
this.key = key;
83+
}
84+
85+
@Override
86+
public void run() {
87+
CacheUtil.remove(key);
88+
}
89+
90+
}
91+
92+
//==================缓存的增删改查
93+
94+
/**
95+
* <pre>
96+
* 添加缓存
97+
* <pre>
98+
* @author hejianfeng
99+
* @date 2019/10/5
100+
* @param
101+
* @param
102+
* @return void
103+
* <pre>
104+
* 修改记录
105+
* 版本号 修订日期 修改人 bug编号 修改内容
106+
* 1.0.0 2019/10/5 hejianfeng 新建
107+
* </pre>
108+
*/
109+
public boolean put(String key,Object object) {
110+
if (checkCapacity()) {
111+
/* String res = HttpUtils.doGet(uyun_alert_url, null);
112+
JSONObject jsonObject=JSONObject.parseObject(res);
113+
JSONArray recordsArray = jsonObject.getJSONArray("records");
114+
Map<String,Object> alertMap=recordsArray.stream().collect(Collectors.toMap(item ->((JSONObject)item).getString("name"), Function.identity(), (item, item1) -> item));
115+
*/
116+
117+
//默认缓存时间
118+
timer.schedule(new ClearTask(key), DEFAULT_TIMEOUT);
119+
return true;
120+
}
121+
return false;
122+
}
123+
124+
/**
125+
* <pre>
126+
* 添加缓存
127+
* <pre>
128+
* @author hejianfeng
129+
* @date 2019/10/5
130+
* @param key
131+
* @param object
132+
* @param time_out :缓存过期时间:单位秒
133+
* @return void
134+
* <pre>
135+
* 修改记录
136+
* 版本号 修订日期 修改人 bug编号 修改内容
137+
* 1.0.0 2019/10/5 hejianfeng 新建
138+
* </pre>
139+
*/
140+
public static boolean put(String key, Object object, int time_out) {
141+
if (checkCapacity()) {
142+
map.put(key, object);
143+
//默认缓存时间
144+
timer.schedule(new ClearTask(key), time_out * SECOND_TIME);
145+
}
146+
return false;
147+
}
148+
149+
150+
/**
151+
* <pre>
152+
* 判断容量大小
153+
* <pre>
154+
* @author hejianfeng
155+
* @date 2019/10/5
156+
* @param
157+
* @return boolean
158+
* <pre>
159+
* 修改记录
160+
* 版本号 修订日期 修改人 bug编号 修改内容
161+
* 1.0.0 2019/10/5 hejianfeng 新建
162+
* </pre>
163+
*/
164+
public static boolean checkCapacity() {
165+
return map.size() < MAX_CAPACITY;
166+
}
167+
168+
/**
169+
* <pre>
170+
* 批量增加缓存
171+
* <pre>
172+
* @author hejianfeng
173+
* @date 2019/10/5
174+
* @param m
175+
* @param time_out
176+
* @return void
177+
* <pre>
178+
* 修改记录
179+
* 版本号 修订日期 修改人 bug编号 修改内容
180+
* 1.0.0 2019/10/5 hejianfeng 新建
181+
* </pre>
182+
*/
183+
public static boolean put(Map<String, Object> m, int time_out) {
184+
if (map.size() + m.size() <= MAX_CAPACITY) {
185+
map.putAll(map);
186+
for (String key : m.keySet()) {
187+
timer.schedule(new ClearTask(key), time_out * SECOND_TIME);
188+
}
189+
return true;
190+
}
191+
return false;
192+
}
193+
194+
/**
195+
* <pre>
196+
* 删除缓存
197+
* <pre>
198+
* @author hejianfeng
199+
* @date 2019/10/5
200+
* @param key
201+
* @return void
202+
* <pre>
203+
* 修改记录
204+
* 版本号 修订日期 修改人 bug编号 修改内容
205+
* 1.0.0 2019/10/5 hejianfeng 新建
206+
* </pre>
207+
*/
208+
public static void remove(String key) {
209+
map.remove(key);
210+
}
211+
212+
/**
213+
* <pre>
214+
* 清除所有缓存
215+
* <pre>
216+
* @author hejianfeng
217+
* @date 2019/10/5
218+
* @param
219+
* @return void
220+
* <pre>
221+
* 修改记录
222+
* 版本号 修订日期 修改人 bug编号 修改内容
223+
* 1.0.0 2019/10/5 hejianfeng 新建
224+
* </pre>
225+
*/
226+
public void clearAll() {
227+
if (map.size() > 0) {
228+
map.clear();
229+
}
230+
timer.cancel();
231+
}
232+
233+
/**
234+
* <pre>
235+
* 获取缓存
236+
* <pre>
237+
* @author hejianfeng
238+
* @date 2019/10/5
239+
* @param key
240+
* @return java.lang.Object
241+
* <pre>
242+
* 修改记录
243+
* 版本号 修订日期 修改人 bug编号 修改内容
244+
* 1.0.0 2019/10/5 hejianfeng 新建
245+
* </pre>
246+
*/
247+
public static Object get(String key) {
248+
return map.get(key);
249+
}
250+
251+
/**
252+
* <pre>
253+
* 是否包含某个缓存
254+
* <pre>
255+
* @author hejianfeng
256+
* @date 2019/10/5
257+
* @param key
258+
* @return boolean
259+
* <pre>
260+
* 修改记录
261+
* 版本号 修订日期 修改人 bug编号 修改内容
262+
* 1.0.0 2019/10/5 hejianfeng 新建
263+
* </pre>
264+
*/
265+
public static boolean isContain(String key) {
266+
return map.contains(key);
267+
}
268+
269+
270+
public static void main(String[] args) {
271+
/* Map<String,Object> mp=new HashMap<>();
272+
mp.put("应用",0);
273+
mp.put("中间件",0);
274+
mp.put("数据库",0);
275+
mp.put("服务器",0);
276+
mp.put("网络设备",0);
277+
mp.put("全流程",0);
278+
mp.put("云管",0);
279+
mp.put("alertTotal",0);
280+
mp.put("安全事件",0);
281+
map.put("alertInfo", mp);
282+
CacheUtil.put("alertInfo",mp,20);*/
283+
284+
285+
286+
Object alertInfo = CacheUtil.get("alertInfo");
287+
System.out.println(alertInfo);
288+
289+
}
290+
291+
292+
293+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.example.awaysuse.util;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import lombok.extern.slf4j.Slf4j;
6+
import uyun.whale.common.encryption.support.HttpUtils;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Optional;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.Future;
14+
15+
//异步调用接口请求数据
16+
@Slf4j
17+
public class FutureReqUtil {
18+
19+
public void getFuture(){
20+
21+
JSONArray allJsonArray = new JSONArray();
22+
ExecutorService executorService = Executors.newFixedThreadPool(10);
23+
try {
24+
// 通过异步的方式调用接口获取数据
25+
List<Future> allTask = new ArrayList<>();
26+
for (int i = 1; i <=Integer.MIN_VALUE; i++) {
27+
allTask.add(this.queryDetailAsync(executorService));
28+
}
29+
for (Future task : allTask) {
30+
Optional.ofNullable(task.get()).ifPresent(r -> allJsonArray.addAll((JSONArray) r));
31+
}
32+
allTask.clear();
33+
for (int i = 0; i < allJsonArray.size(); i++) {
34+
JSONObject ticketObj = allJsonArray.getJSONObject(i);
35+
allTask.add(executorService.submit(() -> {
36+
String ticketId = ticketObj.getString("ticketId");
37+
JSONObject defaultValue = ticketObj.getJSONObject("defaultValue");
38+
String classfy = "meiyou";
39+
if (defaultValue.getString("classify") != null) {
40+
classfy = defaultValue.getString("classify");
41+
}
42+
}));
43+
}
44+
for (Future task : allTask) {
45+
task.get();
46+
}
47+
allTask.clear();
48+
log.info("结束数据详细信息");
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
} finally {
52+
executorService.shutdown();
53+
}
54+
}
55+
56+
57+
private Future<JSONArray> queryDetailAsync(ExecutorService executorService){
58+
return executorService.submit(() -> {
59+
String result1 = HttpUtils.doGet("请求的url", null);
60+
JSONObject jsonObject = JSONObject.parseObject(result1);
61+
String ticketDetail_list = jsonObject.get("ticketDetail_list").toString();
62+
JSONArray jsonArray = JSONArray.parseArray(ticketDetail_list);
63+
if (jsonArray.size() <= 0){
64+
return null;
65+
}
66+
System.out.println(Thread.currentThread().getName() + " finished");
67+
return jsonArray;
68+
});
69+
}
70+
71+
72+
}

0 commit comments

Comments
 (0)