做网站要学什么软件,wordpress分页出现404,济南智能网站建设流程,潍坊兆通网站建设问题引出#xff1a; 由于数据库的自增的id很有规律#xff0c;会让人猜到后续id 由于一个单表存储的数量有限#xff0c;那么就会用多个表存储#xff0c;每个表的id都是自增#xff0c;这样导致id不唯一的情况。
引进新的方法#xff1a; 具体解决方法#xff1a;使用…问题引出 由于数据库的自增的id很有规律会让人猜到后续id 由于一个单表存储的数量有限那么就会用多个表存储每个表的id都是自增这样导致id不唯一的情况。
引进新的方法 具体解决方法使用redis的string自增方法 代码
package com.hmdp.utils;import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.format.DateTimeFormatter;import java.time.LocalDateTime;
import java.time.ZoneOffset;Component
public class RedisIdWorker {private StringRedisTemplate stringRedisTemplate;private final long BEGIN_TIME 110822509L;private final int COUNT_BIT 32;public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate stringRedisTemplate;}public long generateWorkId(String prefixKey) {//1.先生成时间戳LocalDateTime now LocalDateTime.now();//这个获取的是十进制的10位二进制的32位这个时间可以延续到2038年long nowSecond now.toEpochSecond(ZoneOffset.UTC);long timestamp nowSecond - BEGIN_TIME;//2.通过redis生成自增序列号String format now.format(DateTimeFormatter.ofPattern(yyyy:MM:dd));Long increment stringRedisTemplate.opsForValue().increment(order: prefixKey : format);//3.合并返回//虽然redis返回的数字是一个很小的数据但是当时间戳向左移位32位之后//后面空着的位数都是redis的自增数字序列虽然redis返回的数字很小但是很多都是0补位return timestamp COUNT_BIT | increment;}}