[redis] 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)); }
}

更多参考:

Redis常用命令

java对redis的基本操作

上一篇:Redis中String类型的Value最大可以容纳数据长度


下一篇:redis的String类型以及其操作