package com.hanchao.testredis;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import com.alibaba.fastjson.JSON;
import com.hanchao.entity.Person;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis简单学习2
* @author liweihan ()
* @version 1.0 (2014年12月16日 下午2:08:35)
*/
public class RedisClient2 {
public static JedisPool jedisPool; // 池化管理jedis链接池
// public static Jedis jedis; //非切片的客户端连接
static {
//读取相关的配置
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
String ip = resourceBundle.getString("redis.ip");
int port = Integer.parseInt(resourceBundle.getString("redis.port"));
JedisPoolConfig config = new JedisPoolConfig();
//设置最大连接数
config.setMaxActive(maxActive);
//设置最大空闲数
config.setMaxIdle(maxIdle);
//设置超时时间
config.setMaxWait(maxWait);
//初始化连接池
jedisPool = new JedisPool(config, ip, port);
// jedis = jedisPool.getResource();
}
/**
* 向缓存中设置字符串内容
* @param key
* @param value
* @return
*
* 2014年12月16日 下午2:18:31
* liweihan
*/
public static boolean set(String key , String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 向redis中设置对象【使用了fastJson】
* @param key
* @param value
* @return
*
* 2014年12月16日 下午2:20:47
* liweihan
*/
public static boolean set(String key,Object value) {
Jedis jedis = null;
try {
String objectJson = JSON.toJSONString(value);
jedis = jedisPool.getResource();
jedis.set(key, objectJson);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 根据key删除缓存中的对象
* @param key
* @return
*
* 2014年12月16日 下午2:22:32
* liweihan
*/
public static boolean del(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 根据key获取内容
* @param key
* @return
*
* 2014年12月16日 下午2:24:10
* liweihan
*/
public static Object get(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Object value = jedis.get(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 根据key获取对象
* @param key
* @param clazz
* @return
*
* 2014年12月16日 下午2:27:15
* liweihan
*/
public static <T> T get(String key ,Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 测试一下
* @param args
*
* 2014年12月16日 下午2:28:27
* liweihan
*/
public static void main(String[] args) {
System.out.println(" ----------- 从redis中存入对象 --------------");
//1.向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("保存失败");
}
//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-config
redis.pool.maxActive= 100
redis.pool.maxIdle= 20
redis.pool.maxWait= 3000
redis.ip= 10.10.78.208
redis.port= 6379
参考文章:http://www.tuicool.com/articles/UVVz2e