JBoot中实现Redis的incrByFloat
JBoot的redis操作实例JBootRedisImpl类并没有封装Redis的incrByFloat方法,这带来了一定的不方便。但可以通过继承这个类,然后通过获取Jedis实例,调用Jedis原生方法来实现。需要注意的是Jedis调用完成后需要手动释放连接,否则会导致Redis连接池连接耗尽。
public class RedisHelper extends JBootRedisImpl{
/**
* 计数器,递增
* @param key
* @param step
* @return
*/
public static double incrByFloat(String key, double step){
JbootJedisImpl jbootJedisImpl = null;
Jedis jedis = null;
try {
jbootJedisImpl = (JbootJedisImpl) Jboot.me().getRedis();
jedis = jbootJedisImpl.getJedis();
return jedis.incrByFloat(key, step);
} finally {
if(jbootJedisImpl != null) {
jbootJedisImpl.returnResource(jedis);
}
}
}
/**
* 计数器,递减
* @param key
* @param step
* @return
*/
public static double decrByFloat(String key, double step){
JbootJedisImpl jbootJedisImpl = null;
Jedis jedis = null;
try {
jbootJedisImpl = (JbootJedisImpl) Jboot.me().getRedis();
jedis = jbootJedisImpl.getJedis();
return jedis.incrByFloat(key, step * -1);
} finally {
if(jbootJedisImpl != null) {
jbootJedisImpl.returnResource(jedis);
}
}
}
} 相关推荐
郭宇 2020-02-22
txj 2020-08-17
亦碎流年 2020-06-18
middleware0 2020-06-13
anglehearts 2020-04-03
camhan 2020-05-09
枫叶上的雨露 2020-05-04
zhangtianshun 2020-05-03
博了个客 2020-04-29
sunzxh 2020-04-22
八角塘塘主 2020-04-09
GavinZhera 2020-04-08
Cheetahcubs 2020-02-12
middleware0 2020-02-11
wangxiaoxue 2020-01-23
guweiyuthinker 2020-01-20
MLXY 2020-01-21