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:
Persist the session into the database. But the disadvantage is that the cost of reading and writing is too high.
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!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools