redis简单使用
- 配置
- 简单应用举例:①今日热门文章推荐 ②文章点赞
配置
MAVEN添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.14.RELEASE</version>
</dependency>
applicationContext.xml,导入配置文件
<context:property-placeholder location="classpath:dbconfig.properties,classpath:redis.properties"/>
<bean id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig">
<!--新版是maxTotal,旧版是maxActive -->
<property name="maxTotal">
<value>${redis.pool.maxTotal}</value>
</property>
<property name="maxIdle">
<value>${redis.pool.maxIdle}</value>
</property>
<property name="minIdle">
<value>${redis.pool.minIdle}</value>
</property>
<property name="maxWaitMillis">
<value> ${redis.pool.maxWaitMillis}</value>
</property>
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true" />
<property name="timeout" value="${redis.timeout}"></property>
<property name="password" value="${redis.password}"></property>
<property name="database" value="${redis.database}"></property>
</bean>
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory"
ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer"
ref="stringRedisSerializer" />
<property name="hashValueSerializer"
ref="stringRedisSerializer" />
</bean>
redis.properties
redis.pool.maxTotal=200
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.maxWaitMillis=20000
#redis.uri = redis://:qingsoft@localhost:6379/0
redis.host = localhost
redis.port = 6379
redis.timeout=30000
redis.password = 123456
redis.database = 0
今日热门文章推荐(按点赞数排序)
jsp
<body>
今日最热:
<c:forEach items="${requestScope.hotQuestions}" var="question">
${question.id}.${question.title}<br>
</c:forEach>
</body>
controller
@Controller
public class CommonController {
@Resource
private QuestionService questionService;
@RequestMapping("")
public String index(ModelMap map)
{
List<Question> hotQuestions = questionService.findTodayHot(5);
map.put("hotQuestions",hotQuestions);
return "index";
}
}
service层
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Override
public List<Question> findTodayHot(int count) {
Set set = redisTemplate.opsForZSet().reverseRange("question:today:praise:zset",0,count-1);
List<Question> questions = new ArrayList<>();
for (Object o : set) {
Integer id = Integer.parseInt((String)o);
Question question = questionMapper.selectOne(id);
questions.add(question);
}
return questions;
}
mapper
<select id="selectOne" parameterType="java.lang.Integer" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>,info,
question_type.title questionTypeTitle,
userinfo.NAME
FROM question JOIN question_type ON question.questionTypeId=question_type.id
JOIN userinfo ON question.userinfoId=userinfo.id
where question.id = #{id,jdbcType=INTEGER}
</select>
定时任务
@Component
public class QuestionJob {
@Resource
private QuestionService questionService;
//每天半夜0点清空今日点赞
@Scheduled(cron = "0 0 0 * * ?")
public void clearTodayHotQuestions()
{
questionService.clearTodayHot();
}
}
service层
@Override
public void clearTodayHot() {
redisTemplate.delete("question:today:praise:zset");
}
文章点赞,不考虑取消点赞等
jsp
<body>
<h2>${requestScope.question.title}</h2>
<h3>
点赞数:<span class="praise">${requestScope.question.praisecount}</span>
</h3>
${requestScope.question.info}
</body>
</html>
<script src="<%=path%>/static/js/jquery-3.3.1.min.js"></script>
<script>
$(function(){
$(".praise").click(function(){
//不能重复点赞 或者取消点赞 不考虑
$.get("<%=path%>/question/praise",{id:${requestScope.question.id}},
function () {
var praiseCount = parseInt($(this).html());
$(this).html(praiseCount+1);
}.bind(this)
);
});
});
</script>
controller
@GetMapping("praise")
@ResponseBody
public void praise(Integer id)
{
questionService.praise(id);
}
service
@Override
public void praise(Integer id) {
//修改数据库的点赞数量 实际上这里应该修改redis的点赞数量
questionMapper.increasePraiseCount(id);
//增加今日点赞数量 修改redis
//如果是今日第一次点赞 设置得分为1,否则如果不是第一次点赞,得分加1
redisTemplate.opsForZSet().incrementScore("question:today:praise:zset",id+"",1);
}
mapper
<update id="increasePraiseCount" parameterType="java.lang.Integer">
update question set praiseCount=praiseCount+1 where id=#{id}
</update>