码迷,mamicode.com
首页 > 其他好文 > 详细

[redis] redis 对string类型数据操作

时间:2015-12-15 12:10:21      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

 

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));



    }
}

 

更多参考:

 

Redis常用命令

java对redis的基本操作

[redis] redis 对string类型数据操作

标签:

原文地址:http://www.cnblogs.com/lonelywolfmoutain/p/5047510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!