Redis高效点赞与取消功能
到此 查看全文内容
数据格式选择
Redis有5种数据结构分别为:String(字符串)、List(列表)、Set(集合)、Hash(散列)、Zset(有序集合)。
整合SpringBoot
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
基础配置
spring: redis: database: 0 host: 127.0.0.1 port: 6379 jedis: pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0
配置类
package cn.goitman.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; import java.sql.SQLException; /** * @author Nicky * @version 1.0 * @className RedisConfig * @blog goitman.cn | blog..net/minkeyto * @description Redis配置类 * @date 2022/5/16 14:40 */ @Configuration public class RedisConfig { /** * 凡事使用到template的redis操作都必须走@Transanctional注解式事务,要不然会导致连接一直占用,不关闭 */ @Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate template = new RedisTemplate(); // 改变redisTemplate的序列化方式,key为字符串格式,value为json格式 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // HashKey 和 HashValue 为json格式 template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); // 开启事务支持 template.setEnableTransactionSupport(true); return template; } /** * 配置事务管理器 */ @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) throws SQLException { return new DataSourceTransactionManager(dataSource); } }
如果redisTemplate没有序列化,在可视化工具中看到的数据为乱码,获取数据时也可能为空,模糊查询(下文有叙述)功能也使用不了
Redis接口
搞掂,就是这么简单高效,在Redis内,存在相同数据只会修改value,并且Redis默认RDB持久化数据。
到此 查看全文内容
上一篇:
IDEA上Java项目控制台中文乱码