spring redis 连接和连接池配置 使用

spring redis 连接和连接池配置 使用

redis的使用方式方法有很多,我这里只用了这一种

jar包

redis

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.5.2</version>
</dependency>

连接池

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.9.0</version>
</dependency>

redis 连接池配置

<bean id="redisConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" lazy-init="true">
     <property name="maxTotal" value="${redis.pool.maxTotal}"/>
     <property name="maxIdle" value="${redis.pool.maxIdle}"/>
     <property name="minIdle" value="${redis.pool.minIdle}"/>
     <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
     <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
     <property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
     <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
     <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
     <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
 </bean>

redis bean

<bean id="jedisPool" class="redis.clients.jedis.JedisPool" lazy-init="true">
    <constructor-arg index="0" ref="redisConfig"/>
    <constructor-arg index="1" value="${redis.hostname}"/>
    <constructor-arg index="2" value="${redis.port}"/>
    <constructor-arg index="3" value="${redis.timeout}"/>
    <constructor-arg index="4" value="${redis.password}"/>
    <constructor-arg index="5" value="${redis.database}"/>
</bean>

redis 客户端

实现了两个方法,一个有返回值一个没有返回值,可以自定义实现方法

下面都自动关闭了连接,也可以不设置关闭,如果不关闭连接的话连接池会自动关闭连接 设置超时时间

@Lazy(value = true)
@Component
public class RedisClient {
   
    private Logger log = LoggerFactory.getLogger(RedisClient.class);

    @Resource
    private  JedisPool jedisPool;

    public  <T> T use(Function<Jedis, T> function) {
   
        Jedis jedis = jedisPool.getResource();
        try{
   
            T t = function.apply(jedis);

            return t;
        }catch (Exception e){
   
            log.error(e.getMessage(),e);
        }finally {
   
            jedis.close();
        }
        return null;
    }

    public void voidUse(Consumer<Jedis> function) {
   

        Jedis jedis = jedisPool.getResource();
        try {
   
            function.accept(jedis);
        }catch (Exception e){
   
            log.error(e.getMessage(),e);
        }finally {
   
            jedis.close();
        }
    }



    public void setStringVal(String key,String val){
   
        this.voidUse(e->{
   
            e.set(key, val);
        });
    }

    public String setStringValRet(String key,String val){
   
        return this.use(e->{
   
            return e.set(key, val);
        });
    }

    public String getStringVal(String key){
   
        return this.use(e->{
   
            return e.get(key);
        });
    }

    /**
     * redis 操作 hash
     */

    //hmset
    public String hmSetByMap(String key, Map<String,String> map){
   
        return this.use(e->{
   
            return e.hmset(key,map);
        });
    }

    public String hmSet(String key, String field,String val){
   
        return this.use(e->{
   
            Map<String,String> map = new HashMap<>();
            map.put(field,val);
            return e.hmset(key,map);
        });
    }

    //hgetall
    public Map<String, String>  hGetAll(String key){
   
        return this.use(e->{
   
           return e.hgetAll(key);
        });
    }
    //hdel
    public Long hDel(String key,String ...field){
   
        return this.use(e->{
   
            return e.hdel(key,field);
        });
    }
    //hexists
    public Boolean hexists(String key,String field){
   
        return this.use(e->{
   
           return e.hexists(key,field);
        });
    }


    //expire
    public long expire(String key,int time){
   
        return this.use(e->{
   
            return e.expire(key,time);
        });
    }
    //keys
    public Set<String> keys(String pattern){
   
        return this.use(e->{
   
            return e.keys(pattern);
        });
    }

    //del
    public long del(String key){
   
        return this.use(e->{
   
           return  e.del(key);
        });
    }

    public long del(String ...key){
   
        return this.use(e->{
   
            return  e.del(key);
        });
    }



}

使用redis

@Resource
private RedisClient redisClient;
//删除
   public boolean deleteByUserCode(String key) {
   

        long count = redisClient.hDel(WHITE_KEY_LIST,key);
        return count>0;
    }
//保存
    public boolean save(String account,String userCode) {
   

        String ret = redisClient.hmSet(WHITE_KEY_LIST,userCode,account);

        return "OK".equals(ret);
    }


其他方法可以自定义使用,有问题和建议请留言

相关推荐

  1. spring redis 连接连接配置 使用

    2024-01-10 11:34:01       35 阅读
  2. Druid数据库连接配置

    2024-01-10 11:34:01       23 阅读
  3. HttpClient4使用连接

    2024-01-10 11:34:01       6 阅读
  4. Springboot_Tomcat数据库连接配置

    2024-01-10 11:34:01       38 阅读
  5. SpringBoot连接mysql数据库相关配置(druid连接

    2024-01-10 11:34:01       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 11:34:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 11:34:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 11:34:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 11:34:01       18 阅读

热门阅读

  1. DevOps(4)

    2024-01-10 11:34:01       36 阅读
  2. 2024系统分析师---论软件系统架构风格

    2024-01-10 11:34:01       31 阅读
  3. 机器学习 -- 贝叶斯决策理论

    2024-01-10 11:34:01       38 阅读
  4. API的介绍

    2024-01-10 11:34:01       37 阅读
  5. git stash 命令详解

    2024-01-10 11:34:01       64 阅读
  6. redis(1)

    2024-01-10 11:34:01       37 阅读
  7. MATLAB中slist函数用法

    2024-01-10 11:34:01       32 阅读
  8. linux学习笔记

    2024-01-10 11:34:01       24 阅读
  9. 前端常用js、css效果

    2024-01-10 11:34:01       33 阅读