1. Jedis Strings 操作

对应redis命令参考:redis strings组命令

public class JedisStringsTests {

    public static final String HOST = "localhost";
    public static final int PORT = 6379;

    public static final Long SUCCESS = 1L;
    public static final Long FAILED = 0L;
    public static final String OK = "OK";

    final String key1 = "key1";
    final String valueOfKey1 = "value1";

    private Jedis jedis;

    @Before
    public void beforeClass(){
        jedis = new Jedis(HOST, PORT);
        jedis.flushDB();
    }

    @After
    public void afterClass(){
//        jedis.flushDB();
        jedis.close();
    }

    @Test
    public void set(){
        final String newValueOfKey1 = "newValue1";

        // 除了 key - value 之外,不设置任何参数, 则有效期为未设置, 即永久
        String setResult = jedis.set(key1, valueOfKey1);
        assertEquals(OK, setResult);
        Long ttlOfHello = jedis.ttl(key1);
        assertEquals(Long.valueOf(-1), ttlOfHello);

        // 测试NX参数, NX 只有在键不存在时,才对键进行设置操作
        setResult = jedis.set(key1, newValueOfKey1, "NX");
        assertNull(setResult); // 返回为null, 未进行任务设置
        String newValue = jedis.get(key1);
        assertEquals(valueOfKey1, newValue); // 值未更改

        jedis.del(key1); // 删除key1
        setResult = jedis.set(key1, newValueOfKey1, "NX");
        assertEquals(OK, setResult); // 键不存在, 设置成功

        String value1 = jedis.get(key1);
        assertEquals(newValueOfKey1, value1); // 值发生更改

        // 测试 XX 参数, XX: 只在键存在时, 才进行设置操作
        jedis.del(key1);

        setResult = jedis.set(key1, value1, "XX");
        assertNull(setResult); // 键不存在, 未进行设置操作, 返回null

        jedis.set(key1, value1);

        setResult = jedis.set(key1, newValueOfKey1, "XX");
        assertEquals(OK, setResult); // 键已存在, 设置成功
        assertEquals(newValueOfKey1, jedis.get(key1)); // 值为新设置的值

        // 测试 PX 参数

        jedis.del(key1);

        setResult = jedis.set(key1, value1, "NX", "EX", 10L); // 设置有效期10秒

        assertTrue(jedis.ttl(key1) > 0);
        assertTrue(jedis.ttl(key1) <= 10);
    }

    @Test
    public void setNX(){
        // setnx 方式, 在 key 不存在时设置成功, 否则设置失败, 设置成功返回 1 , 失败返回 0
        Long setnxResult = jedis.setnx(key1, valueOfKey1);
        assertEquals(SUCCESS, setnxResult);

        setnxResult = jedis.setnx(key1, "newValue1");
        assertEquals(FAILED, setnxResult);
        assertEquals(valueOfKey1, jedis.get(key1));
    }

    @Test
    public void setEX(){
        // setex 设置值的同时,设置有效期,单位为秒
        String setexResult = jedis.setex(key1, 10, valueOfKey1);

        assertEquals(valueOfKey1, jedis.get(key1));
        assertTrue(jedis.ttl(key1) > 0);
        assertTrue(jedis.ttl(key1) <= 10);
    }

    @Test
    public void mset(){

        final String valueOfKey2 = "valueOfKey2";
        final String key2 = "key2";
        final String key3 = "key3";
        final String valueOfKey3 = "valueOfKey3";
        String msetResult = jedis.mset(key1, valueOfKey1, key2, valueOfKey2);
        assertEquals(OK, msetResult);

        List<String> values = jedis.mget(key1, key2, key3);
        assertEquals(3, values.size());
        assertEquals(valueOfKey1, values.get(0));
        assertEquals(valueOfKey2, values.get(1));
        assertNull(values.get(2));


        msetResult = jedis.mset(key1, valueOfKey1, key2, valueOfKey2, key3, valueOfKey3);
        assertEquals(OK, msetResult);

        values = jedis.mget(key1, key2, key3);
        assertEquals(3, values.size());
        assertEquals(valueOfKey1, values.get(0));
        assertEquals(valueOfKey2, values.get(1));
        assertEquals(valueOfKey3, values.get(2));
    }

    @Test
    public void msetnx(){
        final String valueOfKey2 = "valueOfKey2";
        final String key2 = "key2";
        final String key3 = "key3";
        final String valueOfKey3 = "valueOfKey3";

        // 仅当所有 key 都不存在时, 成功, 否则失败, 成功返回1 , 失败返回 0
        Long msetnxResult = jedis.msetnx(key1, valueOfKey1, key2, valueOfKey2);
        assertEquals(SUCCESS, msetnxResult);

        assertEquals(valueOfKey1, jedis.get(key1));
        assertEquals(valueOfKey2, jedis.get(key2));

        msetnxResult = jedis.msetnx(key1, valueOfKey1, key3, valueOfKey3);
        assertEquals(FAILED, msetnxResult);

        assertNull(jedis.get(key3));
    }

    @Test
    public void append(){
        // 对一个不存在的 key 执行 append操作, 相当于 set操作
        Long length = jedis.append(key1, valueOfKey1); // 返回长度
        assertEquals(Long.valueOf(valueOfKey1.length()), length);
        assertEquals(valueOfKey1, jedis.get(key1));

        final String appendStr = "AppendValue";

        // 对一个已存在的 key 执行 append操作, 在原有值上追回新字符串
        length = jedis.append(key1, appendStr);
        assertEquals(Long.valueOf((valueOfKey1 + appendStr).length()), length);
        assertEquals(valueOfKey1+appendStr, jedis.get(key1));
    }

    @Test
    public void incr(){
        // 对不存在的key进行incr, 会先赋0, 再自增
        Long incrResult = jedis.incr(key1);
        assertEquals(SUCCESS, incrResult);

        // 对存在的key进行incr操作
        incrResult = jedis.incr(key1);
        assertEquals(Long.valueOf(2), incrResult);

        // 设置步长 2
        incrResult = jedis.incrBy(key1, 2);
        assertEquals(Long.valueOf(4), incrResult);
    }

    @Test
    public void decr(){
        // 对不存在的 key 进行 decr操作,会先赋0, 再自减
        Long decrResult = jedis.decr(key1);
        assertEquals(Long.valueOf(-1), decrResult);

        jedis.set(key1, "10");

        // 对已存在的 key 执行 decr 操作
        decrResult = jedis.decr(key1);
        assertEquals(Long.valueOf(9), decrResult);

        // 设置步长 3
        decrResult = jedis.decrBy(key1, 3);
        assertEquals(Long.valueOf(6), decrResult);
    }

    @Test
    public void getset(){
        // getset 设置值的同时, 取出旧值

        // 对不存在的key进行getset操作
        String setResult = jedis.getSet(key1, valueOfKey1);
        assertNull(setResult);
        assertEquals(valueOfKey1, jedis.get(key1));

        // 对存在的 key 进行 getset 操作
        setResult = jedis.getSet(key1, "newValueOfKey1");
        assertEquals(valueOfKey1, setResult);
        assertEquals("newValueOfKey1", jedis.get(key1));
    }
}
Copyright © wychuan.com 2017 all right reserved,powered by Gitbook该文件修订时间: 2017-10-17 02:48:52

results matching ""

    No results matching ""