1. 程式人生 > >Jedis API簡單總結

Jedis API簡單總結

Jedis的各種API,整理了一下,直接拿來用

public interface JedisClient {
    String set(String key, String value);
    String get(String key);
    Boolean exists(String key);
    Long expire(String key, int seconds);
    Long ttl(String key);
    Long incr(String key);
    Double incrByFloat(String key,double value);
    Long incrBy(String key,Long value);
    Long hset(String key, String field, String value);
    String hget(String key, String field);
    Long hdel(String key, String... field);
}

實現方法

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

public class JedisClientPool implements JedisClient {

   private JedisPool jedisPool = new JedisPool("192.168.157.128",6379);
   private Jedis jedis = jedisPool.getResource();

    @Override
    public String set(String key, String value) {
        String result = jedis.set(key, value);
        jedis.close();
        return result;
    }

    @Override
    public String get(String key) {
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

    @Override
    public Boolean exists(String key) {
        Boolean result = jedis.exists(key);
        jedis.close();
        return result;
    }

    @Override
    public Long expire(String key, int seconds) {
        Long result = jedis.expire(key, seconds);
        jedis.close();
        return result;
    }

    @Override
    public Long ttl(String key) {
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public Long incr(String key) {
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public Double incrByFloat(String key, double value) {
        Double result = jedis.incrByFloat(key, value);
        jedis.close();
        return result;
    }

    @Override
    public Long incrBy(String key, Long value) {
         Long result = jedis.incrBy(key, value);
         jedis.close();
         return result;
    }

    @Override
    public Long hset(String key, String field, String value) {
        Long result = jedis.hset(key, field, value);
        jedis.close();
        return result;
    }

    @Override
    public String hget(String key, String field) {
        String result = jedis.hget(key, field);
        jedis.close();
        return result;
    }
    @Override
    public Long hdel(String key, String... field) {
        Long result = jedis.hdel(key, field);
        jedis.close();
        return result;
    }

}

測試一下

public class TestJedisUtils {
    public static void main(String[] args) {

        JedisClientPool jedisUtils = new JedisClientPool();
        //jedisUtils.hset("test","hash1","123");
        //System.out.println(jedisUtils.hget("test", "hash1"));
        Double result = jedisUtils.incrByFloat("value", 23.0);
         System.out.println(result);
    }
}