注意:防火墙应该为redis打开!
在pom.xml文件下引入redis相关依赖
<!--      redis                      -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <!-- 1.5的版本默认采用的连接池技术是jedis  2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar -->
            <exclusions>
                <exclusion>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 添加jedis客户端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!--spring2.0集成redis所需common-pool2-->
        <!-- 必须加上,jedis依赖此  -->
        <!-- spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.5.0</version>
        </dependency>
	在properties文件中,加入以下基本配置
spring.redis.port=6379 spring.redis.host=127.0.0.1 #我的redis连接不需要密码 #spring.redis.password=123 spring.redis.jedis.pool.max-active=100 spring.redis.jedis.pool.max-idle=5 spring.redis.jedis.pool.max-wait=60000 spring.redis.database=0 spring.redis.timeout=10000 #若开启redis方式的session存储 type值应为redis spring.session.store-type=redis spring.session.timeout=10 server.servlet.session.timeout=10
package com.dbc.usermanager.service;
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.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
    @Autowired
    protected StringRedisTemplate redisTemplate;
    /**
     * 写入redis缓存(不设置expire存活时间)
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, String value){
        boolean result = false;
        try {
            ValueOperations operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            System.out.println("写入redis缓存失败!错误信息为:" + e.getMessage());
        }
        return result;
    }
    /**
     * 写入redis缓存(设置expire存活时间)
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public boolean set(final String key, String value, Long expire){
        boolean result = false;
        try {
            ValueOperations operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            System.out.println("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage());
        }
        return result;
    }
    /**
     * 读取redis缓存
     * @param key
     * @return
     */
    public Object get(final String key){
        Object result = null;
        try {
            ValueOperations operations = redisTemplate.opsForValue();
            result = operations.get(key);
        } catch (Exception e) {
            System.out.println("读取redis缓存失败!错误信息为:" + e.getMessage());
        }
        return result;
    }
    /**
     * 判断redis缓存中是否有对应的key
     * @param key
     * @return
     */
    public boolean exists(final String key){
        boolean result = false;
        try {
            result = redisTemplate.hasKey(key);
        } catch (Exception e) {
            System.out.println("判断redis缓存中是否有对应的key失败!错误信息为:" + e.getMessage());
        }
        return result;
    }
    /**
     * redis根据key删除对应的value
     * @param key
     * @return
     */
    public boolean remove(final String key){
        boolean result = false;
        try {
            if(exists(key)){
                redisTemplate.delete(key);
            }
            result = true;
        } catch (Exception e) {
            System.out.println("redis根据key删除对应的value失败!错误信息为:" + e.getMessage());
        }
        return result;
    }
    /**
     * redis根据keys批量删除对应的value
     * @param keys
     * @return
     */
    public void remove(final String... keys){
        for(String key : keys){
            remove(key);
        }
    }
}