SpringBoot+Redis基本操作,实现排行榜功能(1)

本文详细解析了Java面试中可能遇到的Redis配置问题,Spring集成RedisTemplate的方法,以及使用ZSetOperations进行数据操作。作者分享了自己的面试经历和学习心得,鼓励读者做好面试准备,提升技术水平。
摘要由CSDN通过智能技术生成

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

org.apache.commons

commons-lang3

3.8.1

com.google.guava

guava

27.0.1-jre

配置application.yml


spring:

redis:

database: 0

host: localhost

port: 6379

password: root

pool:

max-active: 8

max-wait: -1

max-idle: 8

min-idle: 0

timeout: 5000

server:

port: 8081

注入RedisTemplate,还要有初始化RedisConnectionFactory


package com.rosam.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;

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.Jackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

/**

  • 自己的redisTemplate

  • @author Song.aw

  • @create 2017-12-07 9:37

**/

@Configuration

public class RedisConfig{

@Autowired

RedisConnectionFactory redisConnectionFactory;

@Bean

public RedisTemplate<String, Object> functionDomainRedisTemplate() {

RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();

initDomainRedisTemplate(redisTemplate, redisConnectionFactory);

return redisTemplate;

}

private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setHashKeySerializer(new StringRedisSerializer());

redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

redisTemplate.setConnectionFactory(factory);

}

}

在Controller中的使用


package com.rosam.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ZSetOperations;

import org.springframework.data.redis.support.atomic.RedisAtomicLong;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.TimeUnit;

@Controller

@RequestMapping(“/user”)

public class UserController {

@Autowired

private RedisTemplate redisTemplate;

/**

  • 登录后保存用户名为userName,value为password

  • @param userName

  • @param password

  • @return

*/

@GetMapping(“/login”)

@ResponseBody

public String login(@RequestParam(“userName”)String userName, @RequestParam(“password”)String password){

redisTemplate.opsForValue().set(userName, password);

redisTemplate.expire(userName,10,TimeUnit.SECONDS);

return “success”;

}

/**

  • 原子自增,设置过期时间

  • @param params

  • @return

*/

@RequestMapping(value = “/incrementScore”, method = RequestMethod.POST)

@ResponseBody

public String incrementScore(@RequestBody Map<String, Object> params) {

String key = params.get(“key”).toString();

Long value =(Long) redisTemplate.opsForValue().get(key);

RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());

entityIdCounter.expire(3, TimeUnit.SECONDS);//过期时间为3秒

Long increment = entityIdCounter.getAndIncrement();

redisTemplate.opsForValue().set(key,increment+value);

return “success”;

}

/**

  • 实现排序,热度,积分榜等功能,更多方法可以可以搜索zSetOperations

  • @return

*/

@GetMapping(value = “rankScore”)

@ResponseBody

public String rank(){

ZSetOperations zSetOperations = redisTemplate.opsForZSet();

zSetOperations.add(“score”,“one”,1);

zSetOperations.add(“score”,“four”,4);

zSetOperations.add(“score”,“three”,110);

zSetOperations.add(“score”,“five”,5);

zSetOperations.add(“score”,“six”,6);

//从子集中找到Smin<=score<=Smax的元素集合

//value在此区间中的。

Set set = zSetOperations.rangeByScore(“score”, 100,120);

System.out.println(“打印v1的值在100-120区间的”+set.size());

//索引start<=index<=end的元素子集,返回泛型接口(包括score和value),正序

//返回score和value,set中的前两个

Set set1 = zSetOperations.rangeWithScores(“score”,0,1);

//键为K的集合,索引start<=index<=end的元素子集,正序

//返回排序后的value中的前两个

Set set2 = zSetOperations.range(“score”,0,1);

//键为K的集合,索引start<=index<=end的元素子集,倒序

//返回排序后的最后两个

Set set3 = zSetOperations.reverseRange(“score”,0,1);

感受:

其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。

特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。

也祝愿各位同学,都能找到自己心动的offer。

分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档

拿到字节跳动offer后,简历被阿里捞了起来,二面迎来了P9"盘问"

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。

也祝愿各位同学,都能找到自己心动的offer。

分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档

[外链图片转存中…(img-2G4S5Cid-1714665083191)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 29
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值