JFinal用Redis

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {

    private static JedisPool pool;

    public static void start() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setMaxIdle(95);
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxWaitMillis(1000);
        pool = new JedisPool(new JedisPoolConfig(), "localhost");
    }

    public static void close() {
        if (pool != null) {
            pool.destroy();
        }
    }

    public static Jedis getJedis() {
        return pool.getResource();
    }
}

import com.jfinal.plugin.IPlugin;


public class JedisPlugin implements IPlugin {

    @Override
    public boolean start() {
        JedisUtil.start();
        return true;
    }

    @Override
    public boolean stop() {
        JedisUtil.close();
        return true;

    }

    public void setExTime(String name, Integer time) {
        JCacheKit.setExTime(name, time);
    }

}

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;

public class JCacheKit {

    private static final Map<String, Integer> EX_TIME = new HashMap<String, Integer>();

    protected static void setExTime(String name, Integer time) {
        EX_TIME.put(name, time);
    }

    protected static Integer getExTime(String name) {
        return EX_TIME.get(name);
    }

    public static void put(String name, String key, Object value) {
        if (value == null || key == null) {
            return;
        }
        Integer exT = getExTime(name);
        if (exT == null) {
            throw new RuntimeException("the ex_time with " + name + " is not found.");

        }
        Jedis jedis = JedisUtil.getJedis();
        byte[] val = toBytes(value);
        if (val == null) {
            return;
        }
        jedis.setex((name + "." + key).getBytes(), exT, val);
        jedis.close();
    }

    public static Object get(String name, String key) {
        if (name == null || key == null) {
            return null;
        }
        Jedis jedis = JedisUtil.getJedis();
        byte[] gets = jedis.get((name + "." + key).getBytes());
        jedis.close();
        if (gets == null) {
            return null;
        }
        Object obj = toObject(gets);
        return obj;
    }

    private static Object toObject(byte[] arr) {
        Object obj = null;
        ByteArrayInputStream bin = new ByteArrayInputStream(arr);
        try {
            ObjectInputStream obin = new ObjectInputStream(bin);
            obj = obin.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    private static byte[] toBytes(Object object) {
        byte[] bytes = null;
        ByteArrayOutputStream obj = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(obj);
            out.writeObject(object);
            bytes = obj.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }
}

代碼簡單 序列化部分有問題java

相關文章
相關標籤/搜索