In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "springboot how to use Redis for cache", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "springboot how to use Redis as cache"!
1. Dependency and database settings
Org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 org.springframework.boot spring-boot-starter- Web org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-test test Spring.redis.database=0 spring.redis.host=localhostspring.redis.port=6379 spring.redis.password=123 # your own password 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 and session configuration
@ 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. Entity and controller layer
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 ("Test Cache"); 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. Running
@ SpringBootApplicationpublic class RedisApplication {public static void main (String [] args) {SpringApplication.run (RedisApplication.class, args);}}
Running result:
At the same time, you can also use special graphical interface tools to view:
Thank you for your reading, the above is the content of "how springboot uses Redis for caching". After the study of this article, I believe you have a deeper understanding of how springboot uses Redis for caching, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.