redisScript = new DefaultRedisScript<>(script, resultCls);
+ return StringRedisTemplate.execute(redisScript, keys, args);
+ }
+
+ private RedisHelper() {
+ // It is ideal to configure a single instance of DefaultRedisScript in your application context to avoid re-calculation of the script’s SHA1 on every script run.
+ StringRedisTemplate = SpringContextHelper.getBean(StringRedisTemplate.class);
+ checkAndSetScript = SpringContextHelper.getBean(BEAN_NAME_CHECK_AND_SET_SCRIPT);
+ }
+
+ public static RedisHelper getInstance() {
+ return RedisHelperInstance.INSTANCE;
+ }
+
+ private static class RedisHelperInstance {
+ /**
+ * The constant INSTANCE.
+ */
+ private static final RedisHelper INSTANCE = new RedisHelper();
+ }
+
+
+ /**
+ * 批量检查某个数值并且减去相应值
+ *
+ * todo 现在参数类型写死的,需使用泛型进行通用处理
+ *
+ * @param paramsMap
+ * @return
+ */
+ public Map batchCheckNum(Map paramsMap) {
+ if (paramsMap.isEmpty()) {
+ return null;
+ }
+
+ // 将map转成list
+ List keys = Lists.newArrayList(paramsMap.keySet());
+ List values = Lists.newArrayList(paramsMap.values());
+
+ String result = StringRedisTemplate.execute(checkAndSetScript, keys, values.toArray());
+ if (StringUtils.isNotBlank(result)) {
+ // todo 此处优化
+ return JSONObject.parseObject(result,new TypeReference