Home  >  Article  >  Database  >  How to use Spring boot to quickly build a distributed session cache based on Redis

How to use Spring boot to quickly build a distributed session cache based on Redis

WBOY
WBOYforward
2023-05-27 20:49:27881browse

Use Spring boot to quickly build a distributed session cache solution based on Redis

In large-scale web applications, session management is very important, which cannot be satisfied by single-machine storage.

Usually the following are One method:

  1. Persist the session into the database. But the disadvantage is that the cost of reading and writing is too high.

  2. De-session, such as Information is stored in cookies. But the disadvantage is that the size is limited and it is not safe.

3. Store the session in a distributed nosql database, such as Redis.

Step one: Build the Spring Boot development environment

Add the maven core dependency spring-session-data-redis

<!--基于redis的分布式session存储-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

Configure the redis connection properties in the application.properties file

######   Redis config start ######
spring.redis.database=0
spring.redis.host=192.168.41.60
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=20
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
######   Redis config end ######

Step 2: Open the @EnableRedisHttpSession annotation

@Configuration
@EnableRedisHttpSession(redisNamespace = "mysession",maxInactiveIntervalInSeconds = 60*60*24)
public class SessionConfig

The source code of the @EnableRedisHttpSession annotation is as follows. You can see that three parameters can be configured:

  • maxInactiveIntervalInSeconds: The expiration time of the data in the session (not the expiration time of the session in redis)

  • In my configuration, I specified a namespace named "mysession" , used to identify the current application

  • redisFlushMode: the way redis saves the session, the default ON_SAVE
    has two ways: IMMEDIATE: Once the session is created Just save it immediately.ON_SAVE: It will not be saved when creating the session, but it will be saved when adding data to the session

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ java.lang.annotation.ElementType.TYPE })
@Documented
@Import(RedisHttpSessionConfiguration.class)
@Configuration
public @interface EnableRedisHttpSession
    int maxInactiveIntervalInSeconds() default 1800;

    String redisNamespace() default "";

    RedisFlushMode redisFlushMode() default

Step 3: Test

Write a controller

@RestController
public class HelloController

    @RequestMapping("/hello/{username}")
    public String hello(HttpSession session, @PathVariable(value = "username") String username) {
        session.setAttribute("username", username);
        return "保存session到Redis成功";
    }

    @RequestMapping("/getName")
    public String getUsername(HttpSession session) {
        String username = (String) session.getAttribute("username");
        return

When the browser accesses the link http://localhost:8080/hello/admin, the

hello method will receive the link passed by the browser Parameters and save them in the session, and then save the session to Redis

By viewing the information in redis, you can see the following information:

The spring:session is automatically added, we The created namespace mysession is under this default directory

Next we visit http://localhost:8080/getName, and you can see that the browser will display the admin## we saved in the session just now. #

The above is the detailed content of How to use Spring boot to quickly build a distributed session cache based on Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete