博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis实例
阅读量:4356 次
发布时间:2019-06-07

本文共 3056 字,大约阅读时间需要 10 分钟。

public class RedisHelper implements NoSqlInterface{
private static JedisPool pool;
private static RedisHelper instance;
private RedisHelper(){
}
public synchronized static RedisHelper getInstance(){
if(instance == null){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(PropertyUtils.getIntVal("redis.max_active"));
config.setMaxIdle(PropertyUtils.getIntVal("redis.max_idle"));
config.setMaxWait(PropertyUtils.getIntVal("redis.max_wait"));
pool = new JedisPool(config,PropertyUtils.getStringVal("redis.host.ip"),PropertyUtils.getIntVal("redis.host.port"));
instance = new RedisHelper();
}
return instance;
}
/**
* 获取Jedis对象
* @return
*/
private static Jedis getJedis(){
return pool.getResource();
}
/**
* 添加value为Stirng的数据
* @param key
* @param value
*/
public void setStringVal(String key,String value){
Jedis jedis = getJedis();
jedis.set(key, value);
pool.returnResource(jedis);
}
/**
* 添加value为Stirng的数据
* @param key
* @param value
*/
public void setStringVal(String key,String value,Long seconds){
Jedis jedis = getJedis();
jedis.set(key, value);
jedis.expire(key,seconds.intValue());
pool.returnResource(jedis);
}
/**
* 根据key获取剩余过期时间
* @param key
* @return
*/
public Long getSurplusExpire(String key){
Jedis jedis = getJedis();
return jedis.ttl(key);
}
/**
* 根据key获取String value
* @param key
* @return
*/
public String getStringVal(String key){
Jedis jedis = getJedis();
String value = jedis.get(key);
pool.returnResource(jedis);
return value;
}
/**
* 移除key对应的value
* @param key
*/
public void remove(String key){
Jedis jedis = getJedis();
jedis.del(key);
pool.returnResource(jedis);
}
/**
* 清空所有的key
*/
public void removeAll(){
Jedis jedis = getJedis();
jedis.flushAll();
pool.returnResource(jedis);
}
/**
* 存储java bean对象
* @param key
* @param obj
*/
public void setObjectVal(String key,Object obj){
try {
Jedis jedis = getJedis();
jedis.set(key.getBytes(), SerializeUtil.serializeObj(obj));
pool.returnResource(jedis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取java bean
* @param key
* @return
*/
public Object getObjectVal(String key){
Jedis jedis = getJedis();
try {
return SerializeUtil.unSerializeObj(jedis.get(key.getBytes()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
pool.returnResource(jedis);
}
return null;
}
public static void main(String[] args) throws InterruptedException {
// Person p = new Person(14,"张三");
// p.setAddr(new Address(10086,"江苏扬州"));
// setObjectVal("zs",p);
//
// Person pp = (Person)getInstance().getObjectVal("zs");
// System.out.println(pp.getId()+":"+pp.getName());
// System.out.println(pp.getAddr().getId()+":"+pp.getAddr().getAddress());
getInstance().setStringVal("nihao", "abc",120L);
new Thread().sleep(1000*5);
System.out.println(getInstance().getJedis().ttl("nihao"));
System.out.println(getInstance().getStringVal("nihao"));
}
}

转载于:https://www.cnblogs.com/biihcihc/p/5867927.html

你可能感兴趣的文章
密码学基础
查看>>
Java基础Map接口+Collections工具类
查看>>
OSGI基础知识整理
查看>>
Revit 开发将自己的窗口设置为Revit窗口
查看>>
倾斜摄影数据OSGB转换成3DML(转载)
查看>>
给年轻程序员的几句话
查看>>
ionic如何uglify和minify你的js,css,image,png....
查看>>
[LeetCode]Minimum Depth of Binary Tree
查看>>
jboss初体验
查看>>
Python列表、元组练习
查看>>
angular页面刷新
查看>>
Leetcode:7- Reverse Integer
查看>>
C6表单(方成eform)---自定义流程标题格式
查看>>
GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
查看>>
Python之set集合
查看>>
Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle
查看>>
Win7 下新版本Windows Virtual PC 安装SharePoint步骤详解
查看>>
SQLSERVER 升级版本的方法
查看>>
正则表达式基本语法详解
查看>>
BZOJ2132: 圈地计划
查看>>