package com.gm.common;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* redis缓存技术
* @author Administrator
*
*/
public class RedisAPI {
public JedisPool jedisPool;//redis的连接池对象
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
/**
* set key and value to redis
* @param key
* @param value
* @return
*/
public boolean set(String key,String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return false;
}
/**
* 判断某个key是否存在
* @param key
* @return
*/
public boolean exist(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return false;
}
public static void returnResource(JedisPool pool,Jedis jedis){
if(jedis != null){
pool.returnResource(jedis);
}
}
/**
* 获取数据
* @param key
* @return
*/
public String get (String key){
String value = null;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
value = jedis.get(key);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
//返还到连接池
returnResource(jedisPool, jedis);
}
return value;
}
}
最近下载更多
511076827 LV33
2023年9月20日
泓鼎168 LV20
2023年4月14日
微信网友_6200823191523328 LV3
2022年11月11日
oulingqiao LV13
2022年6月8日
skook7 LV2
2022年3月31日
lzlzyw LV14
2022年3月24日
wanglinddad LV55
2022年2月25日
不会写代码的小姚 LV3
2021年11月20日
543666826 LV34
2021年11月19日
ᯤ⁵²¹ᴳ⁺ LV8
2021年11月13日

最近浏览