springboot整合redis,由无密码配置更改为有密码配置

springboot整合redis,由无密码配置更改为有密码配置:

springboot版本:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.1.RELEASE</version>
</parent>

pom文件添加maven依赖:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

RedisManager.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Iterator;
import java.util.Set;



@Configuration
public class RedisManager {

    /**
     * @Fields host : redis的主机地址,在配置文件中注入
     */
    @Value("${redis.host}")
    private String host;

    /**
     * @Fields port : redis的端口号,在配置文件中注入
     */
    @Value("${redis.port}")
    private int port;

    /**
     * @Fields timeout : redis的超时时间,在配置文件中注入
     */
    @Value("${redis.timeout}")
    private int timeout;

    /**
     * @Fields password : redis的密码,在配置文件中注入
     */
    @Value("${redis.password}")
    private String password;

    // 0-表示永远不过期
    private int expire = 0;


    // redis pool
    private static JedisPool jedisPool = null;

    public RedisManager() {
    }



    /**
     * @Title: init
     * @Description: 初始化方法,用来初始化
     * @param
     * @return void    返回类型
     * @throws
     */
    public void init() {
        if (null == host || 0 == port) {
            System.out.println("请初始化redis配置文件");
            throw new NullPointerException("找不到redis配置");
        }

        if (jedisPool == null) {
           if("".equals(password.trim())) password = null;//redis里null->"",发送,这里""->null发送
           jedisPool = new JedisPool(new JedisPoolConfig(),host,port,timeout,password);
        }
    }

    /**
     * @Title: get
     * @Description: 根据key来获得一条特定的缓存数据
     * @param @param key 序列化后的key
     * @param @return    设定文件
     * @return byte[]    返回类型
     * @throws
     */
    public byte[] get(byte[] key) {
        byte[] value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * @Title: get
     * @Description: 根据key来获得一条特定的缓存数据
     * @param @param key string类型的key
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public String get(String key) {
        String value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * @Title: set
     * @Description: 向redis数据库中缓存数据,key,value都为byte[](已经序列化)
     * @param @param key
     * @param @param value
     * @param @return 设定文件
     * @return byte[] 返回类型
     * @throws
     */
    public byte[] set(byte[] key, byte[] value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (this.expire != 0) {
                jedis.expire(key, this.expire);
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * @Title: set
     * @Description: 向redis数据库中缓存数据,key,value为字符串类型,缓存时间为永不过期
     * @param @param key
     * @param @param value
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     */
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (this.expire != 0) {
                jedis.expire(key, this.expire);
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * @Title: set
     * @Description: 向redis数据库中缓存数据,key,value都为byte[](已经序列化)
     * @param @param key
     * @param @param value
     * @param @param expire 0为永不过期,其他时间则会设置对应的过期时间
     * @param @return 设定文件
     * @return byte[] 返回类型
     * @throws
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * @Title: set
     * @Description: 向redis数据库中缓存数据,key,value都为字符串的类型
     * @param @param key
     * @param @param value
     * @param @param expire 0为永不过期,其他时间则会设置对应的过期时间
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     */
    public String set(String key, String value, int expire) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return value;
    }

    /**
     * @Title: del
     * @Description: 根据byte数组(已经序列化的key)来删除redis数据库中缓存的数据
     * @param @param key 设定文件
     * @return void 返回类型
     * @throws
     */
    public void del(byte[] key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * @Title: del
     * @Description: 根据特定的string类型的key来删除redis数据库中的缓存数据
     * @param @param key 设定文件
     * @return void 返回类型
     * @throws
     */
    public void del(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * @Title: flushDB
     * @Description: 清除指定redis数据库中的数据
     * @param
     * @return void 返回类型
     * @throws
     */
    public void flushDB() {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.flushDB();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * @Title: dbSize
     * @Description: 获得redis缓存数据的大小
     * @param @return 设定文件
     * @return Long 返回类型
     * @throws
     */
    public Long dbSize() {
        Long dbSize = 0L;
        Jedis jedis = jedisPool.getResource();
        try {
            dbSize = jedis.dbSize();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return dbSize;
    }

    /**
     * @Title: keys
     * @Description: 根据泛型来查询所有符合泛型的缓存数据
     * @param @param pattern
     * @param @return 设定文件
     * @return Set<byte[]> 返回类型
     * @throws
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = null;
        Jedis jedis = jedisPool.getResource();
        try {
            keys = jedis.keys(pattern.getBytes());
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return keys;
    }

    /**
     * @Title: dels
     * @Description: 根据提供的泛型来删除reids中缓存的数据
     * @param @param pattern 设定文件
     * @return void 返回类型
     * @throws
     */
    public void dels(String pattern) {
        Set<byte[]> keys = null;
        Jedis jedis = jedisPool.getResource();
        try {
            keys = jedis.keys(pattern.getBytes());
            Iterator<byte[]> ito = keys.iterator();
            while (ito.hasNext()) {
                jedis.del(ito.next());
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getTimeout(){
        return timeout;
    }

    public void setTimeout(int timeout){
        this.timeout = timeout;
    }

    public String getPassword(){
        return password;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public int getExpire() {
        return expire;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值