redis缓存java对象_Redis缓存系统-Java-Jedis操作Redis,基本操作以及 实现对象保存...
1、Maven配置
redis.clients
jedis
2.5.0
com.alibaba
fastjson
1.1.41
2、Properties 配置文件
redis.pool.maxActive=??100
redis.pool.maxIdle=??20
redis.pool.maxWait=??3000
redis.ip=??localhost
redis.port=??6379
3、代碼具體實現的Client
/**
*
*
* Redis客戶端訪問
*
*
* @author 卓軒
* @創建時間:2014年7月11日
* @version: V1.0
*/
public class RedisClient {
public static JedisPool jedisPool;
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.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
jedisPool = new JedisPool(config, ip, port);
}
/**
* 向緩存中設置字符串內容
* @param key key
* @param value value
* @return
* @throws Exception
*/
public static boolean set(String key,String value) throws Exception{
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
}
/**
* 向緩存中設置對象
* @param key
* @param value
* @return
*/
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
*/
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
*/
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
* @return
*/
public static T get(String key,Class 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);
}
}
}
4、 單元測試、保存對象、寫入對象
/**
*
*
* 測試獨立redis 客戶端
*
*
* @author 卓軒
* @創建時間:2014年7月11日
* @version: V1.0
*/
public class SimpleClient {
@Test
public void userCache(){
UserDO zhuoxuan = new UserDO();
zhuoxuan.setUserId(113445);
zhuoxuan.setSex(1);
zhuoxuan.setUname("卓軒");
zhuoxuan.setUnick("zhuoxuan");
zhuoxuan.setEmail("zhuoxuan@mogujie.com");
boolean reusltCache = RedisClient.set("zhuoxuan", zhuoxuan);
if (reusltCache) {
System.out.println("向緩存中保存對象成功。");
}else{
System.out.println("向緩存中保存對象失敗。");
}
}
@Test
public void getUserInfo(){
UserDO zhuoxuan = RedisClient.get("zhuoxuan",UserDO.class);
if(zhuoxuan != null){
System.out.println("從緩存中獲取的對象," + zhuoxuan.getUname() + "@" + zhuoxuan.getEmail());
}
}
}
總結
以上是生活随笔為你收集整理的redis缓存java对象_Redis缓存系统-Java-Jedis操作Redis,基本操作以及 实现对象保存...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java示例_Java入门示例
- 下一篇: java中获取特定时间段_获取某一时间段