| | |
| | | package com.stylefeng.guns.modular.system.util; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.util.StringUtils; |
| | | import java.util.*; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Component |
| | | public class RedisUtil { |
| | | |
| | | |
| | | @Autowired |
| | | private RestTemplate internalRestTemplate; |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | |
| | | private final Map<String, ScheduledExecutorService> LOCK_TASK_MAP = new HashMap<>(); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向redis中存储字符串没有过期时间 |
| | | * @param key |
| | | * @param value |
| | | */ |
| | | public void setStrValue(String key, String value){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | //发送验证码短信 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | // 以表单的方式提交 |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | //将请求头部和参数合成一个请求 |
| | | MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); |
| | | params.add("key", key); |
| | | params.add("value", value); |
| | | HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers); |
| | | String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/setValue_", requestEntity, String.class); |
| | | JSONObject jsonObject = JSON.parseObject(s, JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用redis出错了"); |
| | | } |
| | | if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ |
| | | redisTemplate.opsForValue().set(key, value); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 以分钟为单位设置存储值(设置过期时间) |
| | | * @param key |
| | |
| | | * @param time 秒 |
| | | */ |
| | | public void setStrValue(String key, String value, int time){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | //发送验证码短信 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | // 以表单的方式提交 |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | //将请求头部和参数合成一个请求 |
| | | MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
| | | params.add("key", key); |
| | | params.add("value", value); |
| | | params.add("time", String.valueOf(time)); |
| | | HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers); |
| | | String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/setValue", requestEntity, String.class); |
| | | JSONObject jsonObject = JSON.parseObject(s, JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用redis出错了"); |
| | | } |
| | | if(ToolUtil.isNotEmpty(key) && ToolUtil.isNotEmpty(value)){ |
| | | redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 从redis中获取值 |
| | | * @param key |
| | |
| | | */ |
| | | public String getValue(String key){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | // 以表单的方式提交 |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | //将请求头部和参数合成一个请求 |
| | | MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); |
| | | params.add("key", key); |
| | | HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers); |
| | | String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/getValue",requestEntity , String.class); |
| | | JSONObject jsonObject = JSON.parseObject(s, JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用redis出错了"); |
| | | } |
| | | return jsonObject.getString("data"); |
| | | String data = (String) redisTemplate.opsForValue().get(key); |
| | | return data; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 删除key |
| | | * @param key |
| | | */ |
| | | public void remove(String key){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | redisTemplate.delete(key); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 删除key |
| | | * 加锁 |
| | | * @param key |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public String remove(String key){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | // 以表单的方式提交 |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | //将请求头部和参数合成一个请求 |
| | | MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); |
| | | params.add("key", key); |
| | | HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers); |
| | | String s = internalRestTemplate.postForObject("http://zuul-gateway/redis/remove",requestEntity , String.class); |
| | | JSONObject jsonObject = JSON.parseObject(s, JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用redis出错了"); |
| | | } |
| | | return jsonObject.getString("data"); |
| | | public boolean lock(String key, long keepTime, long time){ |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null == scheduledExecutorService || scheduledExecutorService.isTerminated()){ |
| | | unlock(key); |
| | | } |
| | | return null; |
| | | keepTime *= 1000; |
| | | Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); |
| | | if(ifAbsent){ |
| | | return addLockMap(key, time); |
| | | } |
| | | long timeMillis = System.currentTimeMillis(); |
| | | while (timeMillis + keepTime > System.currentTimeMillis()){ |
| | | ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, UUIDUtil.getRandomCode()); |
| | | if(ifAbsent){ |
| | | return addLockMap(key, time); |
| | | } |
| | | try { |
| | | Thread.sleep(100); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | private boolean addLockMap(String key, long time){ |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null == scheduledExecutorService){ |
| | | scheduledExecutorService = Executors.newScheduledThreadPool(1); |
| | | } |
| | | LOCK_TASK_MAP.put(key, scheduledExecutorService); |
| | | scheduledExecutorService.schedule(()->{ |
| | | redisTemplate.delete(key); |
| | | LOCK_TASK_MAP.remove(key); |
| | | }, time, TimeUnit.SECONDS); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * redis释放锁 |
| | | * @return |
| | | */ |
| | | public boolean unlock(String key){ |
| | | redisTemplate.delete(key); |
| | | ScheduledExecutorService scheduledExecutorService = LOCK_TASK_MAP.get(key); |
| | | if(null != scheduledExecutorService){ |
| | | scheduledExecutorService.shutdownNow(); |
| | | } |
| | | LOCK_TASK_MAP.remove(key); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 添加地理空间索引坐标 |
| | | * @param k |
| | | * @param lon |
| | | * @param lat |
| | | * @param object |
| | | */ |
| | | public void addGeo(String k, Double lon, Double lat, String object){ |
| | | Point point = new Point(lon, lat); |
| | | redisTemplate.opsForGeo().add(k, point, object); |
| | | } |
| | | } |