基于SpringBoot+redis实现一个简单的点赞功能
一、Redis 缓存设计及实现
1.1 Redis 安装及运行
linux中的gcc是由GNU推出的一款功能强大的、性能优越的多平台编译器。gcc编译器能将C、C++语言源程序和目标程序编译、连接成可执行文件。
1.2 Redis 与 SpringBoot 项目的整合
1.在 pom.xml 中引入依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>va
2.编写 Redis 配置类
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
1.3 Redis 的数据结构类型
Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。
模拟用户ID:<input type="text" id="userId"><br>
<span th:if="${isLike==false}">
<span class="glyphicon glyphicon-thumbs-up zan" style="font-size: 25px; cursor: pointer" ></span>
</span>
<span th:if="${isLike==true}">
<span class="glyphicon glyphicon-thumbs-up red zan" style="font-size: 25px; cursor: pointer" ></span>
</span>
<span id="count" th:text="${count}"></span>
</div>
<br>
<div id="show"></div>ml
js和css样式
后端控制层Controller
service接口
Long like(Integer userId, Integer newsId); long queryCount(Integer newsId);
service实现类
因为先不写入MySQL数据库,先不操作dao层,后续进行添加
service接口
boolean checkIsLike(int userId, Integer newsId); Set queryLikeUser(Integer newsId);
service实现
