<!-- spring data redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- commons-pool2 对象池依赖 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
srping: redis: # 超时时间 timeout: 10000ms # 服务器地址 host: 192.168.4.13 # 服务器端口 port: 6379 # 数据库 database: 0 # 密码 # password: root lettuce: pool: # 最大连接数 max-active: 1024 # 最大连接阻塞等待时间 max-wait: 10000ms # 最大空闲连接 max-idle: 200 # 最小空闲连接 min-idle: 5
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); //key序列化 redisTemplate.setKeySerializer(new StringRedisSerializer()); //value序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //hash类型key序列化 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //hash类型value序列化 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); //注入连接工厂 redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
//序列化接口Serializable public class User implements Serializable { private static final long serialVersionUID = 1L;
@Data @EqualsAndHashCode(callSuper = false) @TableName("t_user") public class User implements Serializable { private static final long serialVersionUID = 1L; /** * 用户ID,手机号码 */ private Long id; /** * 用户名 */ private String nickname; /** * MD5(MD5(明文+固定salt)+salt) */ private String password; private String slat; /** * 头像 */ private String head; /** * 注册时间 */ private Date registerDate; /** * 注册时间 */ private Date lastLoginDate; /** * 登入次数 */ private Integer loginCount; }
@Autowired private RedisTemplate redisTemplate;
redisTemplate.opsForValue().set("user:"+ticket,user);
完成!!