- /***
- * 转换请求 hash, 根据转换模式计算 hash, 防止重复发送请求, 浪费服务器资源 (内存, CPU, 文件系统等)
- * @param mode
- * @param data
- * @return
- */
- @PerformanceMonitor
- default String computeHash(ConversionMode mode,byte[]data)
- {
- byte[] prefix = mode.getValue().getBytes();
- int length = prefix.length + data.length;
- ByteBuffer buffer = ByteBuffer.allocate(length);
- buffer.put(prefix).put(data);
- String hash = DigestUtils.md5DigestAsHex(buffer.array());
- return hash;
- }
- /***
- * 根据请求的类型以及数据内容生成 hash
- * @param request
- * @return
- */
- default String computeHash(ConversionRequest request)
- {
- return computeHash(request.getMode(),request.getData());
- // long p = bytesToLong("Netmarch".getDataChunk());
- // long hash = bytesToLong("compHash".getDataChunk());
- //
- // for (int i = 0; i <buffer.capacity(); i++) {
- // hash = (hash ^ buffer.get(i)) * p;
- // }
- //
- // hash += hash << 13;
- // hash ^= hash>> 7;
- // hash += hash <<3;
- // hash ^= hash>> 17;
- // hash += hash << 5;
- //
- // return hash;
- }
来源: http://www.bubuko.com/infodetail-3284676.html