使用Java操作Redis需要jedis-2.7.3.jar
如果需要使用Redis连接池的话,还需commons-pool2-2.3.jar,下载地址:http://pan.baidu.com/s/1bp1FgM7
package com.henry.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisT {
private Jedis jedis;
private JedisPool jedisPool;//连接池
// 初始化
public RedisT(){
// 构造函数中初始化jedisPool
initialPool();
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化连接池
*/
private void initialPool(){
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20); //可用连接实例的最大数目,默认值为8;
config.setMaxIdle(5); //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
config.setMaxWaitMillis(10000); //等待可用连接的最大时间
config.setTestOnBorrow(false); // 定时对线程池中空闲的链接进行validateObject校验
jedisPool = new JedisPool(config,"ip",6379); // redis默认 6379
}
/**
*
* @Title: testString
* @Description: 测试java 操作redis中的String
* @author henry
* @Date 2016-7-16 上午11:08:38
*/
public void testString(){
System.out.println("======================start String==========================");
// 清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());
jedis.set("name", "henry"); //向key-->name中放入了value-->henry
System.out.println("获取name键值对的值:"+jedis.get("name"));
System.out.println("执行对name键值对的值的拼接:"+jedis.append("name", " is my lover"));
System.out.println("获取拼接后的name值:"+jedis.get("name"));
System.out.println("获取name键值对的值:"+jedis.get("name"));
System.out.println("删除name键值对:"+jedis.del("name"));
System.out.println("获取name键值对的值:"+jedis.get("name"));
//设置多个键值对
jedis.mset("name","henry","age","22");
System.out.println("获取age键值对的值:"+jedis.get("age"));
System.out.println("对age键值对进行+1:"+jedis.incr("age"));
System.out.println("获取键值对的对应值:"+jedis.get("name") + "-" + jedis.get("age"));
// 清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());
System.out.println("=============新增键值对时防止覆盖原先值=============");
System.out.println("原name值不存在,新增name:"+jedis.setnx("name", "henry"));
System.out.println("当name值存在时,测试新增name:"+jedis.setnx("name", "henry"));
System.out.println("=============超过有效期键值对被删除=============");
System.out.println("新增age,并指定过期时间为2秒"+jedis.setex("age", 2, "30"));
//等待3秒
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("3秒之后,获取age对应的值:"+jedis.get("age"));
System.out.println("获取name旧值,并更新name新值:"+jedis.getSet("name", "hi henry"));
System.out.println("获取name新值:"+jedis.get("name"));
System.out.println("======================end String==========================");
System.out.println();
}
/**
*
* @Title: testHash
* @Description: 测试java 操作redis中的Hash
* @author henry
* @Date 2016-7-16 下午2:31:00
*/
public void testHash(){
System.out.println("======================start Hash==========================");
//清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());
System.out.println("user中添加name键值对:"+jedis.hset("user", "name", "henry"));
System.out.println("user中添加age键值对:"+jedis.hset("user", "age", "22"));
System.out.println("user中添加age键值对:"+jedis.hset("user", "sex", "男"));
System.out.println("user中的所有值:"+jedis.hgetAll("user"));
System.out.println("user中的所有value值:"+jedis.hvals("user"));
System.out.println("user中删除sex键值对:"+jedis.hdel("user", "sex"));
System.out.println("user中的所有值:"+jedis.hgetAll("user"));
System.out.println("user整型键值的值增加10:"+jedis.hincrBy("user", "age", 10));
System.out.println("user中的所有值:"+jedis.hgetAll("user"));
System.out.println("user中的所有key:"+jedis.hkeys("user"));
System.out.println("======================end Hash==========================");
System.out.println();
}
/**
* @Title: testList
* @Description: 测试java 操作redis中的List
* @author henry
* @Date 2016-7-16 下午2:31:30
*/
public void testList(){
System.out.println("======================start List==========================");
System.out.println("清空库中所有数据:"+jedis.flushDB());
System.out.println("list中添加键值对:"+jedis.lpush("list", "key1"));
System.out.println("list中添加键值对:"+jedis.lpush("list", "key2"));
System.out.println("list中添加键值对:"+jedis.lpush("list", "key3"));
System.out.println("list中添加键值对:"+jedis.lpush("list", "key4"));
System.out.println("list中添加键值对:"+jedis.lpush("list", "key5"));
System.out.println("list中添加键值对:"+jedis.lpush("list", "key6"));
System.out.println("查询list中所有键值对:"+jedis.lrange("list", 0, -1));
System.out.println("删除list指定元素:"+jedis.lrem("list", 2, "key2"));
System.out.println("查询list中删除后的所有键值对:"+jedis.lrange("list", 0, -1));
System.out.println("删除下标0-3区间之外的元素:"+jedis.ltrim("list", 0, 3));
System.out.println("删除指定区间之外元素后的list:"+jedis.lrange("list", 0, -1));
System.out.println("出栈元素:"+jedis.lpop("list"));
System.out.println("元素出栈后-list:"+jedis.lrange("list", 0, -1));
System.out.println("长度-list:"+jedis.llen("list"));
System.out.println("获取下标为2的元素:"+jedis.lindex("list", 2));
System.out.println("======================end List==========================");
System.out.println();
}
/**
* @Title: testSet
* @Description: 测试java 操作redis中的Set
* @author henry
* @Date 2016-7-16 下午2:31:50
*/
public void testSet(){
System.out.println("======================start set==========================");
System.out.println("清空库中所有数据:"+jedis.flushDB());
System.out.println("向sets中加入元素:"+jedis.sadd("sets", "key7"));
System.out.println("向sets中批量加入元素:"+jedis.sadd("sets", "key1","key2","key3","key4","key5","key6"));
System.out.println("查询sets中的所有数据:"+jedis.smembers("sets"));
System.out.println("删除sets中指定的数据:"+jedis.srem("sets", "key7"));
System.out.println("查询sets中的所有数据:"+jedis.smembers("sets"));
System.out.println("批量删除sets中指定的数据:"+jedis.srem("sets", "key5","key6"));
System.out.println("查询sets中的所有数据:"+jedis.smembers("sets"));
System.out.println("======================set计算==========================");
System.out.println("向sets1中批量加入元素:"+jedis.sadd("sets1", "key4","key5","key6","key7","key8","key9"));
System.out.println("查询sets1中的所有数据:"+jedis.smembers("sets1"));
System.out.println("sets和sets1交集:"+jedis.sinter("sets", "sets1"));
System.out.println("sets和sets1并集:"+jedis.sunion("sets", "sets1"));
System.out.println("sets和sets1差集:"+jedis.sdiff("sets", "sets1"));
System.out.println("======================end set==========================");
System.out.println();
}
/**
* @Title: testZset
* @Description: 测试java 操作redis中的Zset
* @author henry
* @Date 2016-7-16 下午2:32:21
*/
public void testZset(){
System.out.println("======================start zset==========================");
System.out.println("清空库中所有数据:"+jedis.flushDB());
System.out.println("向zsets中加入元素:"+jedis.zadd("zsets", 1, "key1"));
System.out.println("向zsets中加入元素:"+jedis.zadd("zsets", 2, "key2"));
System.out.println("向zsets中加入元素:"+jedis.zadd("zsets", 3, "key3"));
System.out.println("向zsets中加入元素:"+jedis.zadd("zsets", 5, "key5"));
System.out.println("向zsets中加入元素:"+jedis.zadd("zsets", 4, "key4"));
System.out.println("查询zsets中所有值:"+jedis.zrange("zsets", 0, -1));
System.out.println("删除zsets中的指定值:"+jedis.zrem("zsets", "key2"));
System.out.println("查询zsets删除元素后的所有值:"+jedis.zrange("zsets", 0, -1));
System.out.println("长度-zset:"+jedis.zcard("zsets"));
System.out.println("======================end zset==========================");
System.out.println();
}
public static void main(String[]args){
RedisT redisT = new RedisT();
redisT.testString();
redisT.testHash();
redisT.testList();
redisT.testSet();
redisT.testZset();
}
}