springboot使用Redis作缓存使用入门教程
1.依赖与数据库设置
org.springframework.boot spring-boot-starter-data-redisorg.apache.commons commons-pool2org.springframework.boot spring-boot-starter-weborg.springframework.session spring-session-data-redisorg.springframework.boot spring-boot-starter-testtest
spring.redis.database=0spring.redis.host=localhostspring.redis.port=6379spring.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() {@Overridepublic 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="https://www.it610.com/article/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缓存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- Activiti(一)SpringBoot2集成Activiti6
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程