1. 程式人生 > >redis對String型別資料的處理

redis對String型別資料的處理

package com.xwolf.java.redis;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

/**
 * Created by Administrator on 2015/12/15 0015.
 */
public class RedisOp {

    private JedisPool jedisPool ;
    private Jedis jedis;
    private String host="127.0.0.1";
    private int port=6379;
    @Before
    public void connect(){

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);
        config.setMaxTotal(20);
        config.setTestOnBorrow(false);
        jedisPool=new JedisPool(config,host,port);
        jedis= jedisPool.getResource();
    }

    /**
     * redis String 型別測試
     */
    @Test
    public void testString(){

     jedis.set("name","3243"); //新增資料
      System.out.println(jedis.get("name"));//獲取資料
        System.out.println(jedis.flushDB());//清空所有資料
        System.out.println(jedis.get("name"));

        jedis.set("qq","sdfsd32332");
        System.out.println(jedis.exists("qq"));//是否存在key

        // 設定多個key,value值
        jedis.mset("k01","v01","k02","v02","k03","v03","k04","v04");

        //刪除key,返回刪除的數量
        System.out.println(jedis.del("k03", "v03"));

        jedis.rename("k01","k001");//重新命名key

        //獲取多個key
        List<String> vls =jedis.mget("k001", "k02", "v01");
        for(String vl:vls){
            System.out.println(vl);
        }

        System.out.println(jedis.substr("k001",1,4));//擷取指定key 的字串

        jedis.lpush("list01","javascript","Bootstrap","jQuery","Hadoop","Redis");//儲存List型別資料

        System.out.println(jedis.ltrim("list01",2,5));



    }
}