Springboot Application integration Redis

Tech whiz

  • redis
  • spring
  • configuration
  • string
  • Annotation
  • class
  • test
  • Other

Abstract: SpringBoot application integration Redis

Writing in the front

Redis is an open source, network-enabled, memory-based and persistent logging, key-value database written in ANSI C, BSD compliant, and provides multiple language apis.

It is often called a data structure server because values can be of the types String, hash, list, set, and sorted sets. How does the SpringBoot project integrate with Redis? With this question in mind we continue today’s tutorial

code

Pom.xml adds the Redis dependency package

<! > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>Copy the code

Configure redis information

/** * MIT License * Copyright (c) 2018 haihua.liu * Permission is hereby granted, free of charge, To any person obtaining a copy * of this software and associated documentation files (the "software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the * THE Software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package cn.liuhaihua.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericToStringSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; /** * @ClassName: RedisConfiguration * @Description: @author Liuhaihua * @date June 27, 2018 ** / @configuration @Component Public class RedisConfiguration {/** *  @Title: redisTemplate * @Description: Initialize a RedisTemplate<String, Object> bean, @param@param Factory * @param@return parameter * @return RedisTemplate<String,Object> Return type * @throws */ @bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); template.setHashValueSerializer(new JdkSerializationRedisSerializer()); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); return template; } /** * * @Title: createTemplate * @Description: Initialize a RedisTemplate<String, Long>, * @param@param Factory * @param@return parameter * @return RedisTemplate<String,Long> Return type * @throws */ @bean public RedisTemplate<String, Long> createTemplate(RedisConnectionFactory factory) { final RedisTemplate<String, Long> redisTemplate = new RedisTemplate<String, Long>(); redisTemplate.setConnectionFactory(factory); redisTemplate.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class)); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class)); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; }}Copy the code

Configure the REDis server address

# # # # # # # # # # # # # # # # # # # # # # # redis# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # spring. Redis. Database = 0 spring. Redis. Host = 115.29.112.65 spring.redis.password=foobaredredis spring.redis.pool.max-active=100 spring.redis.pool.max-idle=8 spring.redis.pool.max-wait=-1 spring.redis.pool.min-idle=1 spring.redis.port=6379Copy the code

test

/** * MIT License * Copyright (c) 2018 haihua.liu * Permission is hereby granted, free of charge, To any person obtaining a copy * of this software and associated documentation files (the "software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the * THE Software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package cn.liuhaihua.web.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; /** * @ClassName: CacheInit * @Description: @author Liuhaihua * @date 2018年6月27 月 * */ @Component @order (1) public class CacheInit implements CommandLineRunner { private static final Logger log = LoggerFactory.getLogger(CacheInit.class); @Autowired private RedisTemplate<String,Object> redisTemplate; /** * @param args * @throws Exception * @see org.springframework.boot.CommandLineRunner#run(java.lang.String[]) */ @ Override public void run (String... Args) throws the Exception {log. The info (" > > > > > > > > > > > > > load cached data to the < < < < < < < < < < < < < < < < < < < < < < "); RedisTemplate. OpsForValue (). The set (" test ", "test"); Object test = redisTemplate. OpsForValue (). The get (" test "); Log.info (" Fetch cached data: "+ test.tostring ()); The info (" > > > > > > > > > > > > > cache data to initialize end < < < < < < < < < < < < < < < < < < < < < < "); }}Copy the code



This article is from “Java Miscellanea”, a partner of the cloud community. For more information, follow “Java Miscellanea”.

Use the cloud habitat community APP, comfortable ~

For details, please click
Comments (0)

To:


Related articles

  • Springboot integration log4j2
  • How do Java programmers stress test code? 【 JWordPress…
  • Year-end summary
  • SpringBoot integrates Redis to implement the caching technology solution
  • A most relevant Java learning route + resource sharing (no stupid sharing)
  • Springboot: Our first open source software
  • SpringBoot enterprise class framework
  • SpringBoot integration with SpringDataRedis
  • Docker deployment SpringBoot project integration Re…
  • Spring Boot 1.5.4 Integrate Redis, Intercept…

The net friend comment on