(1) The Pom.xml file introduces the jar package as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-data-redis</artifactid></dependency>
(2) The Redis connection information is configured in the Application.properties file as follows:
# Redis Database index (default = 0) spring.redis.database=0# Redis server address spring.redis.host=172.31.19.222# Redis Server connection Port Spring.redis.port=6379# Redis Server connection password (default is empty) Spring.redis.password=# Connection pool Maximum number of connections (using negative values for No limit) Spring.redis.pool.max-active=8# Connection pool maximum blocking wait time (with negative values for No limit) Spring.redis.pool.max- Wait=-1# Maximum idle connection in connection pool Spring.redis.pool.max-idle=8# Minimum idle connection in connection pool spring.redis.pool.min-idle=0# Connection time-out (ms) Spring.redis.timeout=0
(3) Testing the Redis cache
PackageSpringboot.web;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.StringRedisTemplate;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classHellocontroller {@AutowiredPrivatestringredistemplate stringredistemplate; @RequestMapping ("/redishandler") PublicString Redishandler () {Stringredistemplate.opsforvalue (). Set ("K5", "Springboot Redis"); returnStringredistemplate.opsforvalue (). Get ("K5"); }}
(4) Start the project, call the Reidshandler method, query the Redis server information, as follows:
Springboot using the Redis database