First, Introduction
Redis is a cache system that can be stored durably and is a high-performance Key-value database.
Second, use
2.1. Add dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-data-redis</artifactid></dependency>
2.2. Application.properties Configuration
######################## #redis开始 ######################## #spring. redis.host=192.168.175.13spring.redis.port= 6379spring.redis.password=123456#spring.redis.database=0#spring.redis.pool.max-active=8# Spring.redis.pool.max-idle=8#spring.redis.pool.max-wait=-1#spring.redis.pool.min-idle=0#spring.redis.timeout=0 ######################## #redis结束 #########################
2.3. Service class
Package com.example.demo.utils;import org.springframework.beans.factory.annotation.autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.valueoperations;import org.springframework.stereotype.component;/** * redis Service * * @Author: I love Big gold * @Description: redis service class * @Date: create in 12:02 2017/7/3 */@Componentpublic class redisutil { @Autowired private StringRedisTemplate Stringredistemplate; public void set (String key, string value) { valueoperations<string, string> ops = this.stringredistemplate.opsforvalue (); if (! This.stringRedisTemplate.hasKey (key)) { &nbsP; ops.set (Key, value); system.out.println ("set key success"); } else { // existing before printing value value system.out.println ("this key = " + ops.get (key)); } } public string get (String key) { return this.stringredistemplate.opsforvalue (). Get (Key); } public void del (String key) { This.stringRedisTemplate.delete (key); }}
2.4, test
Package com.example.demo;import com.example.demo.utils.redisutil;import org.junit.test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.springboottest;import Org.springframework.test.context.junit4.SpringRunner, @RunWith (springrunner.class) @SpringBootTestpublic class springbootredisapplicationtests { @Autowired private redisutil redisutil; @Test public void set () { redisutil.set ("Liuy", "Hello"); } @Test public void get () { system.out.println ( Redisutil.get ("Liuy")); } @Test public void del () { redisutil.del ("Liuy"); }}
Execute set:
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9A/B6/wKioL1lZyq6DA3jCAABU2cEhsmI024.png "title=" 4c5fb88d-ccd8-4664-be7f-d59b72145a87.png "alt=" Wkiol1lzyq6da3jcaabu2cehsmi024.png "/>
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9A/B5/wKiom1lZyxaQ135cAAAl49E7M7M693.png "title=" 31bfba08-c08f-4cf3-9f1a-571acf7c965f.png "alt=" Wkiom1lzyxaq135caaal49e7m7m693.png "/>
Execute get:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9A/B6/wKioL1lZy1XziCh5AABOlT-Yo4o431.png "title=" 01916b1b-9b27-4b99-9d51-03e3f88bbc23.png "alt=" Wkiol1lzy1xzich5aabolt-yo4o431.png "/>
Execute del:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9A/B5/wKiom1lZy4nThwb8AABMEZlGMko401.png "title=" 92a4826b-a8bf-496e-89e7-20821f4f0620.png "alt=" Wkiom1lzy4nthwb8aabmezlgmko401.png "/>
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9A/B6/wKioL1lZy7TjFIcfAAAENCiqzIQ478.png "title=" D759ef50-62d4-4597-ab28-e61d2fbe65f3.png "alt=" Wkiol1lzy7tjficfaaaenciqziq478.png "/>
This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1944008
Springboot (16): Integrated Redis