Redis与Java结合——工厂类

内容为本人原创,转载请标明出处。

首先介绍spring配置:

<!-- 配置JedisPoolConfig -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="${redis.maxTotal}"/><!--最大连接数 100 -->
    <property name="maxIdle" value="${redis.maxIdle}"/><!-- 最大空闲连接数 100 -->
</bean>

<!-- RedisFactory配置 -->
<bean id="redisFactory" class="org.ty.cloudCourse.util.RedisFactory">
    <property name="jedisPoolConfig" ref="jedisPoolConfig"/>
    <property name="HOST" value="${redis.host}"/>
    <property name="PORT" value="${redis.port}"/>
    <property name="PASSWD" value="${redis.passwd}"/>
</bean>

让所有实体类都继承自BaseEntity:

BaseEntity:

package org.ty.cloudCourse.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kangtaiyang
 * @date 2018/7/3
 */
public abstract class BaseEntity {
    protected Integer id = null;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Map<String,BaseEntity> getParentEntity(){
        return new HashMap<>();
    }
}

RedisFactory:

package org.ty.cloudCourse.util;

import org.ty.cloudCourse.entity.BaseEntity;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;

/**
 * @author kangtaiyang
 * @date 2018/6/28
 */
public class RedisFactory {
    private JedisPoolConfig jedisPoolConfig;
    private JedisPool pool;
    private String HOST;
    private String PORT;
    private String PASSWD;
    //实体对象在redis中个数的哈希key
    private final String keyCount = "keyCount";

    public Jedis getJedis() {
        //创建连接池
        pool = new JedisPool(jedisPoolConfig, HOST, Integer.parseInt(PORT), 0, PASSWD);
        //获得核心对象
        Jedis jedis = pool.getResource();
        return jedis;
    }

    public void closeJedis(Jedis jedis) {
        try {
            jedis.close();
        } catch (NullPointerException e) {
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public <A> Map<String, String> hgetall(A a) {
        Jedis jedis = getJedis();
        System.out.println("正在从Redis中获取");
        Map<String, String> map = jedis.hgetAll(a.getClass().getSimpleName() + ":" + ((BaseEntity) a).getId());
        closeJedis(jedis);
        return map;
    }

    //tuple里第一个是key 后面是map
    public String hmset(Tuple tuple) {
        Jedis jedis = getJedis();
        String i = jedis.hmset(((String) tuple.getA()), ((Map<String, String>) tuple.getB()));
        System.out.println("Redis存储成功");
        incr((String) tuple.getA());
        closeJedis(jedis);
        return i;
    }

    /**
     * tuple第一个是type(如"Class"),第二个是key(如"Class:10")
     *
     * @param tuple
     * @return
     */
    public Long del(Tuple tuple) {
        Jedis jedis = getJedis();
        System.out.println("Redis删除" + tuple.getB() + "成功");
        long l = jedis.del("" + tuple.getB());
        decr("" + tuple.getA());
        closeJedis(jedis);
        return l;
    }

    public long getKeyCount(Class a) {
        Jedis jedis = getJedis();
        Long l = Long.parseLong(jedis.hget(keyCount, a.getClass().getSimpleName()));
        closeJedis(jedis);
        return l;
    }

    public <A> long incr(Class a) {
        Jedis jedis = getJedis();
        Long l = jedis.hincrBy(keyCount, a.getClass().getSimpleName(), 1);
        closeJedis(jedis);
        return l;
    }

    public <A> long incr(String field) {
        Jedis jedis = getJedis();
        Long l =jedis.hincrBy(keyCount, field, 1);
        closeJedis(jedis);
        return l;
    }

    public <A> long decr(Class a) {
        Jedis jedis = getJedis();
        Long l = jedis.hincrBy(keyCount, a.getClass().getSimpleName(), -1);
        closeJedis(jedis);
        return l;
    }

    public  long decr(String field) {
        Jedis jedis = getJedis();
        Long l = jedis.hincrBy(keyCount, field, -1);
        closeJedis(jedis);
        return l;
    }

    @Deprecated
    public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) {
        this.jedisPoolConfig = jedisPoolConfig;
    }

    @Deprecated
    public void setHOST(String HOST) {
        this.HOST = HOST;
    }

    @Deprecated
    public void setPORT(String PORT) {
        this.PORT = PORT;
    }

    @Deprecated
    public void setPASSWD(String PASSWD) {
        this.PASSWD = PASSWD;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37580283/article/details/80941762