1. 程式人生 > >Redis與Java結合——工廠類

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;
}
}