標籤:springboot 整合redis
一、簡介
redis是一種可以持久儲存的緩衝系統,是一個高效能的key-value資料庫。
二、使用
2.1、添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2.2、application.properties配置
#########################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、服務類
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服務類 * * @Author: 我愛大金子 * @Description: Redis服務類 * @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)) { ops.set(key, value); System.out.println("set key success"); } else { // 存在則列印之前的 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、測試
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", "你好"); } @Test public void get() { System.out.println(redisUtil.get("liuy")); } @Test public void del() { redisUtil.del("liuy"); }}
執行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" />
執行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" />
執行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" />
本文出自 “我愛大金子” 部落格,請務必保留此出處http://1754966750.blog.51cto.com/7455444/1944008
springBoot(16):整合redis