微信小程序捕获formId实现无限量发送模板信息--------使用Redis存储微信formId和获取

public class FaJsonUtils {
    private static ObjectMapper om = new ObjectMapper();


    public static String objToString(Object obj) {
        String result = "";
        try {
            result = om.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static <T> T parseObj(String json, Class<T> clazz) {
        T t = null;
        if (StringUtils.isNotBlank(json)) {
            try {
                t = om.readValue(json.getBytes(), clazz);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return t;
    }
}

Redis 对象转字符串 和 字符串转对象辅助工具类

 

由于formId默认有效时间为7天,所以我们第一次默认时间7天,以后没取一次,设置的缓存时间为有效时间的formId剩余时间,

这里存储formId的方式为 LinkedHashMap 有序的插入。

public class AppletFromIdUtil {

    static final String APPLET_MEMBER_FORM_ID_KEY = "APPLET_MEMBER_FORM_ID_KEY_";
    static final Integer APPLET_MEMBER_FORM_ID_TIME = 604800;  //缓存默认七天


    static Map<String,Long> saveOrupdateFormId(String  key, String formId) {
        Map<String,Long> fromIdMap = FaJsonUtils.parseObj(JedisClient.get(APPLET_MEMBER_FORM_ID_KEY + key), Map.class);
        if (fromIdMap != null) {
            if (fromIdMap.get(formId) == null) {
                fromIdMap.put(formId, System.currentTimeMillis() + APPLET_MEMBER_FORM_ID_TIME * 1000);
                JedisClient.set(APPLET_MEMBER_FORM_ID_KEY + key, FaJsonUtils.objToString(fromIdMap),APPLET_MEMBER_FORM_ID_TIME);
            }
            return fromIdMap;
        } else {
            Map<String, Long> map = new LinkedHashMap<>();
            map.put(formId, System.currentTimeMillis()+APPLET_MEMBER_FORM_ID_TIME  * 1000);
            JedisClient.set(APPLET_MEMBER_FORM_ID_KEY + "_${_request.memberId}", FaJsonUtils.objToString(map), APPLET_MEMBER_FORM_ID_TIME);
            return map;
        }
    }

    static  String getMemberFormId(String key) {
        Map<String,Long> fromIdMap = FaJsonUtils.parseObj(JedisClient.get(APPLET_MEMBER_FORM_ID_KEY + key), Map.class);
        if (fromIdMap != null) {
            Iterator<Map.Entry<String, Long>> iterator = fromIdMap.entrySet().iterator();
            long nowTime = System.currentTimeMillis();
            Map.Entry<String, Long> tailFirst = null;

            List<String> removerKey = new ArrayList<>();
            for(Map.Entry<String, Long> entry : iterator){
                if( entry.getValue() > nowTime){
                    tailFirst = entry;
                    break;
                }else{
                    removerKey.add(entry.getKey());
                }
            }

            /**
             * 清除过期key
             */
            for(String key:removerKey){
                fromIdMap.remove(key);
            }

            Map.Entry<String, Long> tailLast = null;
            while (iterator.hasNext()) {
                tailLast = iterator.next();
            }
            if (tailLast == null || (tailFirst != null && tailLast != null && tailFirst.getKey() == tailLast.getKey())) {
                JedisClient.del(APPLET_MEMBER_FORM_ID_KEY + key);
            } else {
                fromIdMap.remove(tailFirst.getKey());
                long time = tailLast.getValue();
                if (nowTime > time) {
                    JedisClient.del(APPLET_MEMBER_FORM_ID_KEY + key);
                } else {
                    long cacheTime = time - nowTime;
                    JedisClient.set(APPLET_MEMBER_FORM_ID_KEY + key, FaJsonUtils.objToString(fromIdMap), (int) cacheTime);
                }
            }
            if (tailFirst != null && tailFirst.getValue() > nowTime) {
                return tailFirst.getKey();
            }
            return null;
        } else {
            return null;
        }
    }

 

注: JedisClient 为获取Redis的工具类,这里不详细的介绍。

这里介绍了后台存储和获取formId的方式。这样可以达到变相的为无限制向用户推送模板消息

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值