package com.hanchao.testredis;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Jedis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hanchao.entity.Person;
import com.jedismanager.config.JedisClient;
import com.jedismanager.config.JedisClientFactory;
import com.sohu.tv.so.model.pojo.Kis;
/**
* 使用JedisClientFactory
* @author liweihan
* @version 1.0 (2014年12月16日 下午4:30:13)
*/
public class RedisClient3 {
private static final JedisClientFactory factory = JedisClientFactory.getFactory("redis2");
private static final int Data12 = 12;// 数据库12
/**
* 向缓存中设置字符串内容
* @param key
* @param value
* @return
*
* 2014年12月16日 下午2:18:31
* liweihan
*/
public static boolean set(String key, String value) {
JedisClient client = factory.getClient();
try {
Jedis jedis = client.getSource();
jedis.select(3); //会存到cluster的redis第三个库中[根据权重]
//redis.master貌似没有多少用啊
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
client.returnSource();
}
}
/**
* 向redis中设置字符串【使用了fastJson】
* @param key
* @param value
* @return
*
* 2014年12月16日 下午2:20:47
* liweihan
*/
public static boolean set(String key,Object value) {
JedisClient client = factory.getClient();
try {
String objectJson = JSON.toJSONString(value);
Jedis jedis = client.getSource();
jedis.select(Data12);
jedis.set(key, objectJson);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
client.returnSource();
}
}
/**
* 根据key删除缓存中的对象
* @param key
* @return
*
* 2014年12月16日 下午2:22:32
* liweihan
*/
public static boolean del(String key) {
JedisClient client = factory.getClient();
try {
Jedis jedis = client.getSource();
jedis.select(Data12);//需要指定那个数据库
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
client.returnSource();
}
}
/**
* 根据key获取内容
* @param key
* @return
*
* 2014年12月16日 下午2:24:10
* liweihan
*/
public static Object get(String key) {
JedisClient client = factory.getClient();
try {
Jedis jedis = client.getSource();
jedis.select(Data12);
Object value = jedis.get(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
client.returnSource();
}
}
/**
* 根据key获取对象
* @param key
* @param clazz
* @return
*
* 2014年12月16日 下午2:27:15
* liweihan
*/
public static <T> T get(String key ,Class<T> clazz) {
JedisClient client = factory.getClient();
try {
Jedis jedis = client.getSource();
jedis.select(Data12);
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
client.returnSource();
}
}
public static void main(String[] args) {
//1.存入字符串
/* for (int i = 0; i < 20; i++) {
System.out.println("存入字符串" + set("hanchao"+i, "redis"+i));
}*/
//1.向redis中保存对象
System.out.println(" ----------- 从redis中存入对象 --------------");
Person p1 = new Person();
p1.setId(1);
p1.setName("hanchao");
boolean result = set("p1", p1);
if (result) {
System.out.println("保存成功");
} else {
System.out.println("保存失败");
}
//2.删除对象
//System.out.println(del("p1"));
//1.1从redis取对象
System.out.println(" ----------- 从redis中获取对象 --------------");
Person person1 = get("p1", Person.class);
System.out.println("获取的对象:" + person1.getId() + "," + person1.getName());
//2.向redis存入集合
System.out.println(" ----------- 从redis中存入集合 --------------");
List<Person> list = new ArrayList<Person>();
Person p2 = new Person();
p2.setId(2);
p2.setName("hanchao2");
list.add(p1);
list.add(p2);
boolean result2 = set("list", list);
if (result2) {
System.out.println("存入list成功!");
} else {
System.out.println("存入list失败!");
}
//2.向redis取集合
System.out.println(" ----------- 从redis中取集合 --------------");
//http://hanchaohan.blog.51cto.com/2996417/1584759
String listStr = (String) get("list");
System.out.println("list:" + listStr);
//list:[{"id":1,"name":"hanchao"},{"id":2,"name":"hanchao2"}]
List<Person> list2 = JSON.parseArray(listStr, Person.class);
for (Person p : list2) {
System.out.println("ID:" + p.getId());
System.out.println("NAME:" + p.getName());
}
System.out.println("-- 可以判断是linux机器还是windows机器:" + File.separator);
}
}
配置文件:
redis.pool.maxActive=1024
redis.pool.maxIdle=1000
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.clusters=10.10.52.141:6379:3,10.10.78.208:6379:3
redis.connection.max.tried=10
#redis.master=10.10.78.208:6379:1