ssm开发使用redis作为缓存的使用步骤
1、关于spring配置文件中对于redis的配置
2、配置自定义的RedisAPI,对redis数据库的管理
packageorg.slsale.common;
importredis.clients.jedis.Jedis;
importredis.clients.jedis.JedisPool;
/**
*jedisAPI
*@authorluzhewu
*
*/
publicclassRedisAPI{
publicJedisPooljedisPool;//redis连接池对象
publicJedisPoolgetJedisPool(){
returnjedisPool;
}
publicvoidsetJedisPool(JedisPooljedisPool){
this.jedisPool=jedisPool;
}
/**
*setkeyandvaluetpredis
*@paramkey
*@paramvalue
*@return
*/
publicbooleanset(Stringkey,Stringvalue){
Jedisjedis=null;
try{
jedis=jedisPool.getResource();//获取jedis对象
jedis.set(key,value);
returntrue;
}catch(Exceptione){
e.printStackTrace();
}finally{
//返还到连接池
returnResource(jedisPool,jedis);
}
returnfalse;
}
/**
*判断某个key是否存在
*@paramkey
*@return
*/
publicbooleanexist(Stringkey){
Jedisjedis=null;
try{
jedis=jedisPool.getResource();
returnjedis.exists(key);
}catch(Exceptione){
e.printStackTrace();
}finally{
//返还到连接池
returnResource(jedisPool,jedis);
}
returnfalse;
}
/**
*通过key获取value
*@paramkey
*@return
*/
publicStringget(Stringkey){
Stringvalue=null;
Jedisjedis=null;
try{
jedis=jedisPool.getResource();
value=jedis.get(key);
}catch(Exceptione){
e.printStackTrace();
}finally{
//返还到连接池
returnResource(jedisPool,jedis);
}
returnvalue;
}
/**
*返还到连接池
*@paramjedisPool
*@paramjedis
*/
publicstaticvoidreturnResource(JedisPooljedisPool,Jedisjedis){
if(jedis!=null){
jedisPool.returnResource(jedis);
}
}
}
3、redis相关依赖
redis.clients jedis 2.6.1
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短