目录

4.4.1.3-Redis-Java-API

优质
小牛编辑
129浏览
2023-12-01
方法名概述参数返回值
public static JedisPool getPool()构建redis连接池(单例模式)返回一个连接池JedisPool对象
getResource()从连接池中获取连接。返回从连接池中获取到的连接
returnResource(Jedis jedis)当发送出的数据被正确执行后,会自动调用该方法redis为待返还的连接
fail(Object msgId)当发送出的数据没有并正确执行(比如丢失了),会自动调用该方法。msgId为被未正确执行的元组id
public static String get(String key) {
    String value = null;
    JedisPool pool = null;
    Jedis jedis = null;
    try {
        pool = RedisAPI.getPool();
        jedis = pool.getResource();
        value = jedis.get(key);
    } catch (Exception e) {
        …
    } finally {
        …
    }
    …
}
public static String get(String key) {
    String value = null;
    JedisPool pool = null;
    Jedis jedis = null;
    try {
        pool = RedisAPI.getPool();
        jedis = pool.getResource();
        value = jedis.get(key);
    } catch (Exception e) {
        …
    } finally {
        …
    }
    …
}
public static String get(String key) {
        …
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = RedisAPI.getPool();
            jedis = pool.getResource();
            …
        } catch (Exception e) {
            …
        } finally {
            // 返还到连接池
            pool.returnResource( jedis);
        }
        …
    }