spring redis 连接和连接池配置 使用
作者:mmseoamin日期:2024-02-04

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

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

jar包

redis


    redis.clients
    jedis
    2.5.2

连接池


    org.apache.commons
    commons-pool2
    2.9.0

redis 连接池配置


     
     
     
     
     
     
     
     
     
 

redis bean


    
    
    
    
    
    

redis 客户端

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

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

@Lazy(value = true)
@Component
public class RedisClient {
    private Logger log = LoggerFactory.getLogger(RedisClient.class);
    @Resource
    private  JedisPool jedisPool;
    public   T use(Function 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 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 map){
        return this.use(e->{
            return e.hmset(key,map);
        });
    }
    public String hmSet(String key, String field,String val){
        return this.use(e->{
            Map map = new HashMap<>();
            map.put(field,val);
            return e.hmset(key,map);
        });
    }
    //hgetall
    public Map  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 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);
    }

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