经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
springboot使用Redis作缓存使用入门教程
来源:jb51  时间:2021/7/26 8:45:22  对本文有异议

1.依赖与数据库设置

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.commons</groupId>
  7. <artifactId>commons-pool2</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.session</groupId>
  15. <artifactId>spring-session-data-redis</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>
  1. spring.redis.database=0
  2.  
  3. spring.redis.host=localhost
  4.  
  5. spring.redis.port=6379
  6.  
  7. spring.redis.password=123 #自己的密码
  8.  
  9. spring.redis.lettuce.pool.max-active=8
  10.  
  11. spring.redis.lettuce.pool.max-wait=-1
  12.  
  13. spring.redis.lettuce.pool.max-idle=8
  14.  
  15. spring.redis.lettuce.pool.min-idle=0

2.redis和session配置

  1. @Configuration
  2. @EnableCaching
  3. public class RedisConfig extends CachingConfigurerSupport{
  4. @Bean
  5. public KeyGenerator keyGenerator() {
  6. return new KeyGenerator() {
  7. @Override
  8. public Object generate(Object target, Method method, Object... params) {
  9. StringBuilder sb = new StringBuilder();
  10. sb.append(target.getClass().getName());
  11. sb.append(method.getName());
  12. for (Object obj : params) {
  13. sb.append(obj.toString());
  14. }
  15. return sb.toString();
  16. }
  17. };
  18. }
  19. }
  20.  
  21. @Configuration
  22. @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
  23. public class SessionConfig {
  24. }

3.实体与controller层

  1. public class User implements Serializable {
  2.  
  3. private static final long serialVersionUID = 1L;
  4. private Long id;
  5. private String userName;
  6. private String password;
  7. private String email;
  8. private String nickname;
  9. private String regTime;
  10.  
  11. public User() {
  12. super();
  13. }
  14. public User(String email, String nickname, String password, String userName, String regTime) {
  15. super();
  16. this.email = email;
  17. this.nickname = nickname;
  18. this.password = password;
  19. this.userName = userName;
  20. this.regTime = regTime;
  21. }
  22.  
  23. public Long getId() {
  24. return id;
  25. }
  26.  
  27. public void setId(Long id) {
  28. this.id = id;
  29. }
  30.  
  31. public String getUserName() {
  32. return userName;
  33. }
  34.  
  35. public void setUserName(String userName) {
  36. this.userName = userName;
  37. }
  38.  
  39. public String getPassword() {
  40. return password;
  41. }
  42.  
  43. public void setPassword(String password) {
  44. this.password = password;
  45. }
  46.  
  47. public String getEmail() {
  48. return email;
  49. }
  50.  
  51. public void setEmail(String email) {
  52. this.email = email;
  53. }
  54.  
  55. public String getNickname() {
  56. return nickname;
  57. }
  58.  
  59. public void setNickname(String nickname) {
  60. this.nickname = nickname;
  61. }
  62.  
  63. public String getRegTime() {
  64. return regTime;
  65. }
  66.  
  67. public void setRegTime(String regTime) {
  68. this.regTime = regTime;
  69. }
  70.  
  71. @Override
  72. public String toString() {
  73. return "User{" +
  74. "id=" + id +
  75. ", userName='" + userName + '\'' +
  76. ", password='" + password + '\'' +
  77. ", email='" + email + '\'' +
  78. ", nickname='" + nickname + '\'' +
  79. ", regTime='" + regTime + '\'' +
  80. '}';
  81. }
  82. }
  1. @RestController
  2. public class UserController {
  3.  
  4. @RequestMapping("/getUser")
  5. @Cacheable(value="user-key")
  6. public User getUser() {
  7. User user=new User("aa@126.com", "aa", "aa123456", "aa","123");
  8. System.out.println("测试缓存");
  9. return user;
  10. }
  11.  
  12.  
  13. @RequestMapping("/uid")
  14. String uid(HttpSession session) {
  15. UUID uid = (UUID) session.getAttribute("uid");
  16. if (uid == null) {
  17. uid = UUID.randomUUID();
  18. }
  19. session.setAttribute("uid", uid);
  20. return session.getId();
  21. }
  22. }

4.运行

  1. @SpringBootApplication
  2. public class RedisApplication {
  3.  
  4. public static void main(String[] args) {
  5. SpringApplication.run(RedisApplication.class, args);
  6. }
  7. }

运行结果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号