springboot使用Redis作缓存使用入门教程

这篇文章主要介绍了springboot使用Redis作缓存使用入门教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

springboot使用Redis作缓存使用入门教程,恰卡网带你了解更多相关信息。

1.依赖与数据库设置

<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-data-redis</artifactId>		</dependency>		<dependency>			<groupId>org.apache.commons</groupId>			<artifactId>commons-pool2</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-web</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.session</groupId>			<artifactId>spring-session-data-redis</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-test</artifactId>			<scope>test</scope>		</dependency>

spring.redis.database=0  spring.redis.host=localhostspring.redis.port=6379  spring.redis.password=123 #自己的密码spring.redis.lettuce.pool.max-active=8spring.redis.lettuce.pool.max-wait=-1spring.redis.lettuce.pool.max-idle=8spring.redis.lettuce.pool.min-idle=0

2.redis和session配置

@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport{		@Bean	public KeyGenerator keyGenerator() {        return new KeyGenerator() {            @Override            public Object generate(Object target, Method method, Object... params) {                StringBuilder sb = new StringBuilder();                sb.append(target.getClass().getName());                sb.append(method.getName());                for (Object obj : params) {                    sb.append(obj.toString());                }                return sb.toString();            }        };    }}@Configuration@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)public class SessionConfig {}

3.实体与controller层

public class User implements Serializable {	private static final long serialVersionUID = 1L;	private Long id;	private String userName;	private String password;	private String email;	private String nickname;	private String regTime;	public User() {		super();	}	public User(String email, String nickname, String password, String userName, String regTime) {		super();		this.email = email;		this.nickname = nickname;		this.password = password;		this.userName = userName;		this.regTime = regTime;	}	public Long getId() {		return id;	}	public void setId(Long id) {		this.id = id;	}	public String getUserName() {		return userName;	}	public void setUserName(String userName) {		this.userName = userName;	}	public String getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	public String getEmail() {		return email;	}	public void setEmail(String email) {		this.email = email;	}	public String getNickname() {		return nickname;	}	public void setNickname(String nickname) {		this.nickname = nickname;	}	public String getRegTime() {		return regTime;	}	public void setRegTime(String regTime) {		this.regTime = regTime;	}	@Override	public String toString() {		return "User{" +				"id=" + id +				", userName='" + userName + '\'' +				", password='" + password + '\'' +				", email='" + email + '\'' +				", nickname='" + nickname + '\'' +				", regTime='" + regTime + '\'' +				'}';	}}

@RestControllerpublic class UserController {    @RequestMapping("/getUser")    @Cacheable(value="user-key")    public User getUser() {        User user=new User("aa@126.com", "aa", "aa123456", "aa","123");        System.out.println("测试缓存");        return user;    }    @RequestMapping("/uid")    String uid(HttpSession session) {        UUID uid = (UUID) session.getAttribute("uid");        if (uid == null) {            uid = UUID.randomUUID();        }        session.setAttribute("uid", uid);        return session.getId();    }}

4.运行

@SpringBootApplicationpublic class RedisApplication {	public static void main(String[] args) {		SpringApplication.run(RedisApplication.class, args);	}}

运行结果:

springboot使用Redis作缓存使用入门教程

springboot使用Redis作缓存使用入门教程

springboot使用Redis作缓存使用入门教程

同时也可以用专门的图形界面工具查看:

springboot使用Redis作缓存使用入门教程

到此这篇关于springboot使用Redis作缓存使用入门的文章就介绍到这了,更多相关springboot Redis缓存内容请搜索趣讯吧以前的文章或继续浏览下面的相关文章希望大家以后多多支持趣讯吧!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/3755.html

(0)
上一篇 2021-07-25 21:42:11
下一篇 2021-07-25 21:42:13

相关推荐

  • 钱包掉了可以报警吗(失物招领处)

    法律问题分析:可以报警。1、在接通电话之后,一般110警察会给报警人附近派出所的电话或直接转接;2、派出所民警会问其详细地址,随后在5分钟之内赶到现场。3、赶到现场后,一般是两到三名警察,警察会详细询问并了解或知晓的案件大致经过,以及案发现场等情况。

    2022-01-08
    2060
  • 买房定金不退打12345有用吗(定金纠纷哪个部门处理)

    大家在买房的时候就会少不了要缴纳定金,但是在之后可能会因为不想买这套房或者其他因素就会希望能够把定金给退回来,但是大家也要知道定金其实是不能退的,那么买房定金不退打12345有用吗,定金纠纷哪个部门处理,接下来大家就随久久派小编一起了解看看~买房定金不退打12

    2021-09-14
    4980
  • ETF会不会强制赎回(交易规则投资者一定要看清)

    基金类型划分的标准很多,比如可按投资对象、投资策略等进行划分,所以投资者会接触到各种各样的基金名称,其中就有ETF基金。那么,ETF会不会强制赎回呢

    2021-08-28
    4050
  • 为什么夜空中的星星会一闪一闪地眨眼(蚂蚁庄园今日7月25日答案)

    在夏天的时候,我们总是会看到漫天的繁星,有时候当你看到一颗星星的时候,它还会不断地闪烁,那么为什么夜空中的星星会一闪一闪地眨眼?下面趣讯吧网小编带来:蚂蚁庄园今日7月25日答案

    2021-07-24
    2600
  • 小米12智能手机系列将不包括屏下摄像头

    导读本月即将推出的小米12旗舰产品将不包括屏下摄像头。此信息来自中国知名网红数字聊天站。小米有望推出三款手机作为该系列的一部分,即小米12本月即将推出的小米12旗舰产品将不包括屏下摄像头。此信息来自中国知名网红数字聊天站。小米有望推出三款手机作为

    2021-12-27
    2070
  • C/C++(int数与多枚举值互转的实现)

    在C/C++在C/C++的开发中经常会遇到各种数据类型互转的情况,本文主要介绍了C/C++int数与多枚举值互转的实现,具有一定的参考价值,感兴趣的可以了解一下

    2021-08-25
    4520

发表回复

登录后才能评论