写点什么

SpringBoot+Redis 基本操作,实现排行榜功能,javasql 优化面试题

  • 2021 年 11 月 10 日
  • 本文字数:2330 字

    阅读完需:约 8 分钟

<artifactId>guava</artifactId>


<version>27.0.1-jre</version>


</dependency>


</dependencies>


配置 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 中的使用




《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码




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);


return null;


}


/**


  • 根据 key 获取 value

  • @param key

  • @return


*/


@GetMapping("/get")


@ResponseBody


public String getRedisVal(@RequestParam String key){


String value = redisTemplate.opsForValue().get(key).toString();


return value;


}


/**

评论

发布
暂无评论
SpringBoot+Redis基本操作,实现排行榜功能,javasql优化面试题