| |
| |
| import cn.hutool.extra.spring.SpringUtil; |
| import cn.hutool.http.HttpUtil; |
| import cn.RedisUtil; |
| import com.alibaba.fastjson.JSON; |
| import lombok.extern.slf4j.Slf4j; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Slf4j |
| public class AccessTokenUtil { |
| |
| |
| public static String accessTokenUrl; |
| |
| public static String appId; |
| |
| public static String secret; |
| |
| public static RedisUtil redisUtil= SpringUtil.getBean("redisUtil"); |
| |
| |
| |
| |
| public static String getToken () { |
| |
| String token = (String) redisUtil.get("wechat_token"); |
| if (token == null) { |
| |
| try { |
| |
| String url = accessTokenUrl + "?grant_type=client_credential&appid=" + appId + "&secret=" + secret; |
| |
| String response = HttpUtil.get(url); |
| |
| |
| token = JSON.parseObject(response).getString("access_token"); |
| |
| redisUtil.set("wechat_token" , token , 7100); |
| |
| }catch (Exception e) { |
| log.error("获取token时异常:" + e); |
| } |
| |
| } |
| return token; |
| } |
| } |
| |
| |
| |
| |
| import cn.hutool.core.collection.CollectionUtil; |
| import cn.hutool.core.util.StrUtil; |
| import org.springframework.beans.factory.annotation.Autowired; |
| |
| import org.springframework.data.redis.core.RedisTemplate; |
| import org.springframework.stereotype.Service; |
| import org.springframework.util.CollectionUtils; |
| |
| import java.util.Collection; |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.Set; |
| import java.util.concurrent.TimeUnit; |
| import java.util.stream.Collectors; |
| |
| |
| |
| |
| |
| |
| |
| @Service |
| public class RedisUtil { |
| @Autowired |
| private RedisTemplate<String, Object> redisTemplate; |
| |
| |
| |
| |
| |
| |
| |
| |
| public boolean expire(String key, long time) { |
| try { |
| if (time > 0) { |
| redisTemplate.expire(key, time, TimeUnit.SECONDS); |
| } |
| return true; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public Object get(String key) { |
| return key == null ? null : redisTemplate.opsForValue().get(key); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public boolean set(String key, Object value) { |
| try { |
| redisTemplate.opsForValue().set(key, value); |
| return true; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public boolean set(String key, Object value, long time) { |
| try { |
| if (time > 0) { |
| redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
| } else { |
| set(key, value); |
| } |
| return true; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| @SuppressWarnings("unchecked") |
| public void del(String... key) { |
| if (key != null && key.length > 0) { |
| if (key.length == 1) { |
| redisTemplate.delete(key[0]); |
| } else { |
| redisTemplate.delete(CollectionUtils.arrayToList(key)); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| @SuppressWarnings("unchecked") |
| public void del(Collection keys) { |
| if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(keys)) { |
| redisTemplate.delete(keys); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| public Map<String, Object> getAllKeyValues(String prefix) { |
| Collection<String> allKeys = this.getAllKeys(prefix); |
| HashMap<String, Object> results = CollectionUtil.newHashMap(); |
| for (String key : allKeys) { |
| results.put(key, this.get(key)); |
| } |
| return results; |
| } |
| |
| |
| |
| |
| |
| |
| public Collection<String> getAllKeys(String prefix) { |
| Set<String> keys = redisTemplate.keys(prefix + "*"); |
| if (keys != null) { |
| |
| return keys.stream().map(key -> StrUtil.removePrefix(key, prefix)).collect(Collectors.toSet()); |
| } else { |
| return CollectionUtil.newHashSet(); |
| } |
| } |
| } |
| |
| |