| | |
| | | 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.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 redis.clients.jedis.Jedis; |
| | | import redis.clients.jedis.JedisPool; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import org.springframework.util.StringUtils; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Component |
| | | public class RedisUtil { |
| | | |
| | | |
| | | |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向redis中存储字符串没有过期时间 |
| | | * @param key |
| | | * @param value |
| | | */ |
| | | public void setStrValue(String key, String value){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | //将请求头部和参数合成一个请求 |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("key", key); |
| | | map.put("value", value); |
| | | String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/setValue_", map); |
| | | 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)){ |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("key", key); |
| | | map.put("value", value); |
| | | map.put("time", String.valueOf(time)); |
| | | String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/setValue", map); |
| | | 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)){ |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("key", key); |
| | | String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValue", map); |
| | | 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; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 从redis中获取值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public String getValues(String key){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("keys", key); |
| | | String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValues", map); |
| | | JSONObject jsonObject = JSON.parseObject(s, JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用redis出错了"); |
| | | } |
| | | return jsonObject.getString("data"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 删除key |
| | | * @param key |
| | | */ |
| | | public String remove(String key){ |
| | | public void remove(String key){ |
| | | if(ToolUtil.isNotEmpty(key)){ |
| | | Map<String,String> map = new HashMap<>(); |
| | | map.put("key", key); |
| | | String s = HttpRequestUtil.postRequest(PushURL.zull_user_url + "/redis/getValue", map); |
| | | JSONObject jsonObject = JSON.parseObject(s, JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用redis出错了"); |
| | | } |
| | | return jsonObject.getString("data"); |
| | | redisTemplate.delete(key); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * redis加锁 |
| | | * @param key |
| | | * @param value |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(String key, String value, int time){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.opsForValue().setIfAbsent(key, value); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取redis锁 |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock("redis", uuid, time); |
| | | } |
| | | |
| | | |
| | | public boolean lock(String key, int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock(key, uuid, time); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * redis释放锁 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public boolean unlock(String key){ |
| | | if(!StringUtils.isEmpty(key)){ |
| | | key += "_lock"; |
| | | return redisTemplate.delete(key); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 删除锁 |
| | | * @return |
| | | */ |
| | | public boolean unlock(){ |
| | | return unlock("redis"); |
| | | } |
| | | } |