《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
org.apache.commons
commons-lang3
3.8.1
com.google.guava
guava
27.0.1-jre
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);
}
}
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。
分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。
也祝愿各位同学,都能找到自己心动的offer。
分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档
[外链图片转存中…(img-2G4S5Cid-1714665083191)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!