This commit is contained in:
wucongxing8150 2025-06-26 17:15:14 +08:00
parent 9b0a0639a9
commit 458089fa8c
3 changed files with 211 additions and 208 deletions

View File

@ -7,8 +7,8 @@ import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.common.common.exception.BusinessException; import net.lab1024.sa.common.common.exception.BusinessException;
import net.lab1024.sa.common.module.support.redis.RedisService;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
@ -28,7 +28,7 @@ public class Base {
private static String clientUrl; private static String clientUrl;
@Resource @Resource
private RedisService redisService; private RedisTemplate<String, String> redisTemplate;
/** /**
* 发送 POST JSON 请求 * 发送 POST JSON 请求
@ -40,7 +40,7 @@ public class Base {
public String postJson(String url, String jsonData, Map<String, String> headers) { public String postJson(String url, String jsonData, Map<String, String> headers) {
// 获取token // 获取token
String tokenKey = "fangxinqian:" + clientId; String tokenKey = "fangxinqian:" + clientId;
String token = redisService.get(tokenKey); String token = redisTemplate.opsForValue().get(tokenKey);
if (token == null) { if (token == null) {
token = getAccessToken(); token = getAccessToken();
} }
@ -100,7 +100,7 @@ public class Base {
} }
String tokenKey = "fangxinqian:" + clientId; String tokenKey = "fangxinqian:" + clientId;
redisService.set(tokenKey,accessToken,(expiresIn - 120)); redisTemplate.opsForValue().set(tokenKey,accessToken,(expiresIn - 120));
return accessToken; return accessToken;
} }

View File

@ -1,6 +1,7 @@
package net.lab1024.sa.admin.extend.fangxinqian; package net.lab1024.sa.admin.extend.fangxinqian.company;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.extend.fangxinqian.Base;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -9,5 +10,4 @@ import org.springframework.stereotype.Component;
@Slf4j @Slf4j
@Component @Component
public class Company extends Base { public class Company extends Base {
} }

View File

@ -9,6 +9,7 @@ import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*; import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -19,205 +20,207 @@ import java.time.temporal.ChronoUnit;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
//
/** ///**
* redis 一顿操作 // * redis 一顿操作
* // *
* @Author 1024创新实验室: 罗伊 // * @Author 1024创新实验室: 罗伊
* @Date 2020/8/25 21:57 // * @Date 2020/8/25 21:57
* @Wechat zhuoda1024 // * @Wechat zhuoda1024
* @Email lab1024@163.com // * @Email lab1024@163.com
* @Copyright 1024创新实验室 https://1024lab.net // * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
*/ // */
//@Component //@Service
public class RedisService { //public class RedisService {
//
private static final Logger log = org.slf4j.LoggerFactory.getLogger(RedisService.class); // private static final Logger log = org.slf4j.LoggerFactory.getLogger(RedisService.class);
//
@Resource // @Resource
private StringRedisTemplate stringRedisTemplate; // private StringRedisTemplate stringRedisTemplate;
//
@Resource // @Resource
private RedisTemplate<String, Object> redisTemplate; // private RedisTemplate<String, Object> redisTemplate;
//
@Resource // @Resource
private ValueOperations<String, String> redisValueOperations; // private ValueOperations<String, String> redisValueOperations;
//
@Resource // @Resource
private HashOperations<String, String, Object> redisHashOperations; // private HashOperations<String, String, Object> redisHashOperations;
//
@Resource // @Resource
private ListOperations<String, Object> redisListOperations; // private ListOperations<String, Object> redisListOperations;
//
@Resource // @Resource
private SetOperations<String, Object> redisSetOperations; // private SetOperations<String, Object> redisSetOperations;
//
@Resource // @Resource
private SystemEnvironment systemEnvironment; // private SystemEnvironment systemEnvironment;
//
//
/** // /**
* 生成redis key // * 生成redis key
* @param prefix // * @param prefix
* @param key // * @param key
* @return // * @return
*/ // */
public String generateRedisKey(String prefix, String key) { // public String generateRedisKey(String prefix, String key) {
SystemEnvironmentEnum currentEnvironment = systemEnvironment.getCurrentEnvironment(); // SystemEnvironmentEnum currentEnvironment = systemEnvironment.getCurrentEnvironment();
return systemEnvironment.getProjectName() + RedisKeyConst.SEPARATOR + currentEnvironment.getValue() + RedisKeyConst.SEPARATOR + prefix + key; // return systemEnvironment.getProjectName() + RedisKeyConst.SEPARATOR + currentEnvironment.getValue() + RedisKeyConst.SEPARATOR + prefix + key;
} // }
//
/** // /**
* redis key 解析成真实的内容 // * redis key 解析成真实的内容
* @param redisKey // * @param redisKey
* @return // * @return
*/ // */
public static String redisKeyParse(String redisKey) { // public static String redisKeyParse(String redisKey) {
if(SmartStringUtil.isBlank(redisKey)){ // if(SmartStringUtil.isBlank(redisKey)){
return ""; // return "";
} // }
int index = redisKey.lastIndexOf(RedisKeyConst.SEPARATOR); // int index = redisKey.lastIndexOf(RedisKeyConst.SEPARATOR);
if(index < 1){ // if(index < 1){
return redisKey; // return redisKey;
} // }
return redisKey.substring(index); // return redisKey.substring(index);
} // }
//
public boolean getLock(String key, long expire) { // public boolean getLock(String key, long expire) {
return redisValueOperations.setIfAbsent(key, String.valueOf(System.currentTimeMillis()), expire, TimeUnit.MILLISECONDS); // return redisValueOperations.setIfAbsent(key, String.valueOf(System.currentTimeMillis()), expire, TimeUnit.MILLISECONDS);
} // }
//
public void unLock(String key) { // public void unLock(String key) {
redisValueOperations.getOperations().delete(key); // redisValueOperations.getOperations().delete(key);
} // }
//
/** // /**
* 指定缓存失效时间 // * 指定缓存失效时间
* // *
* @param key // * @param key
* @param time 时间() // * @param time 时间()
* @return // * @return
*/ // */
public boolean expire(String key, long time) { // public boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS); // return redisTemplate.expire(key, time, TimeUnit.SECONDS);
} // }
//
/** // /**
* 获取当天剩余的秒数 // * 获取当天剩余的秒数
* // *
* @return // * @return
*/ // */
public static long currentDaySecond() { // public static long currentDaySecond() {
return ChronoUnit.SECONDS.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.MAX)); // return ChronoUnit.SECONDS.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
} // }
//
/** // /**
* 根据key 获取过期时间 // * 根据key 获取过期时间
* // *
* @param key 不能为null // * @param key 不能为null
* @return 时间() 返回0代表为永久有效 // * @return 时间() 返回0代表为永久有效
*/ // */
public long getExpire(String key) { // public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS); // return redisTemplate.getExpire(key, TimeUnit.SECONDS);
} // }
//
/** // /**
* 判断key是否存在 // * 判断key是否存在
* // *
* @param key // * @param key
* @return true 存在 false不存在 // * @return true 存在 false不存在
*/ // */
public boolean hasKey(String key) { // public boolean hasKey(String key) {
return redisTemplate.hasKey(key); // return redisTemplate.hasKey(key);
} // }
//
/** // /**
* 删除缓存 // * 删除缓存
* // *
* @param key 可以传一个值 或多个 // * @param key 可以传一个值 或多个
*/ // */
@SuppressWarnings("unchecked") // @SuppressWarnings("unchecked")
public void delete(String... key) { // public void delete(String... key) {
if (key != null && key.length > 0) { // if (key != null && key.length > 0) {
if (key.length == 1) { // if (key.length == 1) {
redisTemplate.delete(key[0]); // redisTemplate.delete(key[0]);
} else { // } else {
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key)); // redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
} // }
} // }
} // }
//
/** // /**
* 删除缓存 // * 删除缓存
* // *
* @param keyList // * @param keyList
*/ // */
public void delete(List<String> keyList) { // public void delete(List<String> keyList) {
if (CollectionUtils.isEmpty(keyList)) { // if (CollectionUtils.isEmpty(keyList)) {
return; // return;
} // }
redisTemplate.delete(keyList); // redisTemplate.delete(keyList);
} // }
//
//============================String============================= // //============================String=============================
//
/** // /**
* 普通缓存获取 // * 普通缓存获取
* // *
* @param key // * @param key
* @return // * @return
*/ // */
public String get(String key) { // public String get(String key) {
return key == null ? null : redisValueOperations.get(key); // return key == null ? null : redisValueOperations.get(key);
} // }
//
public <T> T getObject(String key, Class<T> clazz) { // public <T> T getObject(String key, Class<T> clazz) {
Object json = this.get(key); // Object json = this.get(key);
if (json == null) { // if (json == null) {
return null; // return null;
} // }
T obj = JSON.parseObject(json.toString(), clazz); // T obj = JSON.parseObject(json.toString(), clazz);
return obj; // return obj;
} // }
//
//
/** // /**
* 普通缓存放入 // * 普通缓存放入
*/ // */
public void set(String key, String value) { // public void set(String key, String value) {
redisValueOperations.set(key, value); // redisValueOperations.set(key, value);
} // }
public void set(Object key, Object value) { // public void set(Object key, Object value) {
String jsonString = JSON.toJSONString(value); // String jsonString = JSON.toJSONString(value);
redisValueOperations.set(key.toString(), jsonString); // redisValueOperations.set(key.toString(), jsonString);
} // }
//
/** // /**
* 普通缓存放入 // * 普通缓存放入
*/ // */
public void set(String key, String value, long second) { // public void set(String key, String value, long second) {
redisValueOperations.set(key, value, second, TimeUnit.SECONDS); // redisValueOperations.set(key, value, second, TimeUnit.SECONDS);
} // }
//
/** // /**
* 普通缓存放入并设置时间 // * 普通缓存放入并设置时间
*/ // */
public void set(Object key, Object value, long time) { // public void set(Object key, Object value, long second) {
String jsonString = JSON.toJSONString(value); // String jsonString = JSON.toJSONString(value);
if (time > 0) { // if (second > 0) {
redisValueOperations.set(key.toString(), jsonString, time, TimeUnit.SECONDS); // redisValueOperations.set(key.toString(), jsonString, second, TimeUnit.SECONDS);
} else { // } else {
set(key.toString(), jsonString); // set(key.toString(), jsonString);
} // }
} // }
//
//============================ map ============================= // //============================ map =============================
public void mset(String key, String hashKey, Object value) { //
redisHashOperations.put(key, hashKey, value); //
} // public void mset(String key, String hashKey, Object value) {
// redisHashOperations.put(key, hashKey, value);
public Object mget(String key, String hashKey) { // }
return redisHashOperations.get(key, hashKey); //
} // public Object mget(String key, String hashKey) {
// return redisHashOperations.get(key, hashKey);
} // }
//
//}