Spring Boot + Spring-data-redis

Source: Internet
Author: User

Redis

Redis is a cache, Message Queuing, and multiple types of key-value storage services.

Spring Boot

Spring boot provides automatic injection configuration for lettcue and Jedis clients, and provides abstract interfaces through Spring-data-redis

Configure the Connection Redis service and interface call 1. Join the dependency

Dependencies are pom.xml added to the dependency collection org.springframework.boot:spring-boot-starter-data-reids , which is configured as follows

    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!-- 里面依赖了spring-data-redis -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>        </dependency>    </dependencies>

Default use Lettuce as Client

2. Modify the configuration file

Add Redis-related configuration to the spring boot configuration file as application.yaml An example (other format profiles, self-converting)

spring:  redis:    # 其他配置信息有缺省    host: localhost    port: 6379    timeout: 500    pool:      min-idle: 1      max-idle: 8      max-active: 8
3. Bean Injection using

Spring Boot automatically injects management as soon as the configuration is complete. RedisTemplate you can manipulate Redis with this object.

In accordance with my previous concise approach, I 在RedisTemple was encapsulated into a straightforward operation. Manage below

RedisManager.java

package info.chiwm.boot.manager;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.data.redis.core.stringredistemplate;import Org.springframework.stereotype.component;import javax.annotation.postconstruct;/** * @author [email protected] * @ClassName: Redismanager * @Description: * @date 20 18/1/10 pm 3:40 */@Componentpublic class Redismanager {@Autowired private stringredistemplate redistemplate; private static Redismanager Redismanager; @PostConstruct public void, init () {Redismanager = this; /** * Redis Set String Ops * * @param key * @param value */public static void Set (String key, String value) {RedisManager.redisTemplate.opsForValue (). Set (key, value); /** * Redis Get string Ops * @param key * @return */public static string get (String key) { Return RedisManager.redisTemplate.opsForValue (). get (key); }}

Call the static method directly, convenient to call the Redis corresponding set key command. If additional storage types and operations are required. RedisManagerstatic methods can be added on.

Spring Boot + Spring-data-redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.