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

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

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;

}

/**

  • 根据key设置value

  • @param key

  • @param value

  • @return

*/

@GetMapping(“/set”)

@ResponseBody

public String setRedisVal(@RequestParam String key, @RequestParam Integer value){

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

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

Elasticsearch 24 题 +Memcached + Redis 40题:

Spring 26 题+ 微服务 27题+ Linux 45题:

Java面试题合集:

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

Elasticsearch 24 题 +Memcached + Redis 40题:

[外链图片转存中…(img-nLIyjZEU-1714665114812)]

Spring 26 题+ 微服务 27题+ Linux 45题:

[外链图片转存中…(img-e9Hirpr1-1714665114813)]

Java面试题合集:

[外链图片转存中…(img-EVswQGLv-1714665114813)]

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

  • 12
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值