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

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

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

jar包

redis

<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
    <version>2.5.2version>
dependency>

连接池

<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-pool2artifactId>
    <version>2.9.0version>
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);
    }


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

你可能感兴趣的:(spring,redis,java)