|
@@ -0,0 +1,58 @@
|
|
|
+package com.ozs.common.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.ozs.common.core.redis.RedisCache;
|
|
|
+import com.ozs.common.utils.uuid.IdUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 幂等性处理工具类
|
|
|
+ *
|
|
|
+ * @author Administrator
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class IdempotenceUtils {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取幂等性信息
|
|
|
+ *
|
|
|
+ * @return 幂等性Redis存储的key
|
|
|
+ */
|
|
|
+ public String getIdempotence(String code) {
|
|
|
+ // 获取请求携带的令牌
|
|
|
+ if (StringUtils.isNotEmpty(code)) {
|
|
|
+ try {
|
|
|
+ // 解析对应的权限
|
|
|
+ return redisCache.getCacheObject(code);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 幂等性存储信息
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void createIdempotence(String code) {
|
|
|
+ redisCache.setCacheObject(code, IdUtils.fastUUID(), 2, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除幂等性信息
|
|
|
+ * @param code
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean deleteIdempotence(String code) {
|
|
|
+ return redisCache.deleteObject(code);
|
|
|
+ }
|
|
|
+}
|