并发安全的KEY
static ConcurrentHashMap<String, Long> overdues = new ConcurrentHashMap<String, Long>();
public R list(@RequestParam Map<String, Object> params) {
Map<String, Object> map = new HashMap<>();
try {
long t1 = System.currentTimeMillis();
// 上一次调用3秒内不调用
if ( overdues.containsKey(json.toString())) {
long pass = t1 - overdues.get(json.toString());
if (pass >= 2000) {
overdues.remove(json.toString());
overdues.put(json.toString(), System.currentTimeMillis());
} else {
return R.error("保存太频繁,请稍后再试!");
}
}else {
overdues.put(json.toString(), System.currentTimeMillis());
}
// 正常返回数据后,应该删除缓存里的过期时间
String urlparam = url+"/order/save";
JSONObject orderShow = Optional.ofNullable(postData(jsonObject,urlparam)).orElse(null);
if (orderShow.getIntValue("code") == 0){
overdues.remove(json.toString());//删除定时缓存
return R.ok("操作成功");
}
另一种就是redis 保证过期时间来防止重复提交,只是改用第三方中间件实现,3秒过期,如果能返回数据就是没有过期,
ValueOperations<String, String> properation = redisTemplate.opsForValue(); String str = properation.get(params.toString()); if (StringUtils.isBlank(str)) { properation.set(params.toString(), params.toString(), 3, TimeUnit.SECONDS); }else{ return R.ok("调用太频繁,请休息一下"); }