redis工具 :springboot使用redis

mac 安装redis,用homebrew 安装

homebrew常用命令

brew search **  //查找某个软件包
brew list  //列出已经安装的软件的包
brew install ** //安装某个软件包,默认安装的是稳定版本
brew uninstall **//卸载某个软件的包
brew upgrade ** //更新某个软件包
brew info ** //查看指定软件包的说明
brew cache clean //清理缓存

搜索查看

 brew search redis 
 //出现如下
 ==> Searching local taps...
hiredis         redis           redis-leveldb   redis@2.8       redis@3.2
==> Searching taps on GitHub...
homebrew/cask/redis-app
==> Searching blacklisted, migrated and deleted formulae...

安装 3.2版本

 	brew install redis@3.2

安装后根据提示操作

/usr/local/etc 下修改redis.config找到
daemonize no改成yes 以守护进程的方式启动
配置环境变量echo ‘export PATH="/usr/local/opt/redis@3.2/bin:$PATH"’ >> ~/.zshrc

将redis配到环境变量里 /usr/local/opt/redis@3.2/bin 配好 ~/.bash_profile中

启动redis

启动server

redis-server /usr/local/etc/redis.conf
查看进程
ps axu | grep redis

启动客户端

redis-cli -h 127.0.0.1 -p 6379
关闭:redis-cli shutdown
杀死: sudo pkill redis-server

springboot中配置 yml文件

redis:
    database: 10
    host: 127.0.0.1
    port: 6379
    timeout: 43200000
    jedis:
      pool:
        max-idle: 500
        min-idle: 50
        max-active: 2000
        max-wait: 1000
    block-when-exhausted: true

新建redis配置类


	import lombok.extern.slf4j.Slf4j;
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;
	import redis.clients.jedis.JedisPool;
	import redis.clients.jedis.JedisPoolConfig;
	
	/**
	 * Created on 2019/12/18
	 *
	 * @Author: Sean
	 */
	
	@Configuration
	@Slf4j
	public class RedisConfig {
   
	
	    @Value("${spring.redis.host}")
	    private String host;
	
	    @Value("${spring.redis.port}")
	    private int port;
	
	    @Value("${spring.redis.timeout}")
	    private int timeout;
	
	    @Value("${spring.redis.jedis.pool.max-idle}")
	    private int maxIdle;
	
	    @Value("${spring.redis.jedis.pool.max-wait}")
	    private long maxWaitMillis;
	
	//    @Value("${spring.redis.password}")
	//    private String password;
	
	    @Value("${spring.redis.block-when-exhausted}")
	    private boolean blockWhenExhausted;
	
	    @Bean
	    public JedisPool redisPoolFactory() throws Exception {
   
	        log.info("JedisPool注入成功!!");
	        log.info("redis地址:" + host + ":" + port);
	        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	        jedisPoolConfig.setMaxIdle(maxIdle);
	        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
	        // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
	        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
	        // 是否启用pool的jmx管理功能, 默认true
	        jedisPoolConfig.setJmxEnabled(true);
	        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
	        return jedisPool;
	    }
	
	}
	

JedisUtil 类

package citic.cph.zdw.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;

/**
 * Created on 2019/12/18
 *
 * @Author: Sean
 */

@Component
@Slf4j
public class JedisUtil{
   

    @Autowired
    private JedisPool jedisPool;

    /**
     * 通过key获取储存在redis中的value
     * 并释放连接
     * @param key
     * @param indexdb 选择redis库 0-15
     * @return 成功返回value 失败返回null
     */
    public String get(String key,int indexdb) {
   
        Jedis jedis = null;
        String value = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            value = jedis.get(key);
            log.info(value);
        } catch (Exception e) {
   

            log.error(e.getMessage());
        } finally {
   
            returnResource(jedisPool, jedis);
        }
        return value;
    }

    /**
     * 通过key获取储存在redis中的value
     * 并释放连接
     * @param key
     * @param indexdb 选择redis库 0-15
     * @return 成功返回value 失败返回null
     */
    public byte[] get(byte[] key,int indexdb) {
   
        Jedis jedis = null;
        byte[] value = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            value = jedis.get(key);
        } catch (Exception e) {
   

            log.error(e.getMessage());
        } finally {
   
            returnResource(jedisPool, jedis);
        }
        return value;
    }
    /**
     * 向redis存入key和value,并释放连接资源
     * 如果key已经存在 则覆盖
     * @param key
     * @param value
     * @param indexdb 选择redis库 0-15
     * @return 成功 返回OK 失败返回 0
     */
    public String set(String key, String value,int indexdb) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.set(key, value);
        } catch (Exception e) {
   

            log.error(e.getMessage(),e);
            return "0";
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * 向redis存入key和value,并释放连接资源
     * 如果key已经存在 则覆盖
     * @param key
     * @param value
     * @param indexdb 选择redis库 0-15
     * @return 成功 返回OK 失败返回 0
     */
    public String set(byte[] key, byte[] value,int indexdb) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.set(key, value);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return "0";
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * 删除指定的key,也可以传入一个包含key的数组
     * @param keys 一个key 也可以使 string 数组
     * @return 返回删除成功的个数
     */
    public Long del(String... keys) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            return jedis.del(keys);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return 0L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * 删除指定的key,也可以传入一个包含key的数组
     * @param indexdb 选择redis库 0-15
     * @param keys 一个key 也可以使 string 数组
     * @return 返回删除成功的个数
     */
    public Long del(int indexdb,String... keys) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.del(keys);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return 0L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * 删除指定的key,也可以传入一个包含key的数组
     * @param indexdb 选择redis库 0-15
     * @param keys 一个key 也可以使 string 数组
     * @return 返回删除成功的个数
     */
    public Long del(int indexdb,byte[]... keys) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.del(keys);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return 0L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * 通过key向指定的value值追加值
     * @param key
     * @param str
     * @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度 异常返回0L
     */
    public Long append(String key, String str) {
   
        Jedis jedis = null;
        Long res = null;
        try {
   
            jedis = jedisPool.getResource();
            res = jedis.append(key, str);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return 0L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * 判断key是否存在
     * @param key
     * @return true OR false
     */
    public Boolean exists(String key) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return false;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * 清空当前数据库中的所有 key,此命令从不失败。
     * @return 总是返回 OK
     */
    public String flushDB() {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            return jedis.flushDB();
        } catch (Exception e) {
   
            log.error(e.getMessage());
        } finally {
   
            returnResource(jedisPool, jedis);
        }
        return null;
    }

    /**
     * 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
     * @param key
     * @param value
     *            过期时间,单位:秒
     * @return 成功返回1 如果存在 和 发生异常 返回 0
     */
    public Long expire(String key, int value, int indexdb) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.expire(key, value);
        } catch (Exception e) {
   
            log.error(e.getMessage());
            return 0L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * 以秒为单位,返回给定 key 的剩余生存时间
     * </p>
     *
     * @param key
     * @return 当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key
     *         的剩余生存时间。 发生异常 返回 0
     */
    public Long ttl(String key,int indexdb) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.ttl(key);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return 0L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * 移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )
     * </p>
     *
     * @param key
     * @return 当生存时间移除成功时,返回 1 .如果 key 不存在或 key 没有设置生存时间,返回 0 , 发生异常 返回 -1
     */
    public Long persist(String key) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            return jedis.persist(key);
        } catch (Exception e) {
   

            log.error(e.getMessage());
            return -1L;
        } finally {
   
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * 新增key,并将 key 的生存时间 (以秒为单位)
     * </p>
     *
     * @param key
     * @param seconds
     *            生存时间 单位:秒
     * @param value
     * @return 设置成功时返回 OK 。当 seconds 参数不合法时,返回一个错误。
     */
    public String setex(String key, int seconds, String value) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            return jedis.setex(key, seconds, value);
        } catch (Exception e) {
   

            log.error(e.getMessage());
        } finally {
   
            returnResource(jedisPool, jedis);
        }
        return null;
    }

    /**
     * <p>
     * 设置key value,如果key已经存在则返回0,nx==> not exist
     * </p>
     *
     * @param key
     * @param value
     * @return 成功返回1 如果存在 和 发生异常 返回 0
     */
    public Long setnx(String key, String value) {
   
        Jedis jedis = null;
        try {
   
            jedis = jedisPool.getResource();
            return jedis.setnx(key, value);
        } catch (Exception e) {
   

            log
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值