最后修改的代码如下:
Jedis jedis = jedisPool.getResource(); boolean success = true; try { if(jedis == null){ Product product = productDao.findProductById(id); return product; } String json = jedis.hget(JedisKeyConstants.PRODUCT_CONFIG__CACHE_KEY, id); if (StringUtils.isNotBlank(json)) { return JSON.parseObject(json, Product.class); }else{ Product product = productDao.findProductById(id); if (product != null) { Transaction t = jedis.multi(); //t.hdel(key, field) //t.del(JedisKeyConstants.PRODUCT_CONFIG__CACHE_KEY); t.hset(JedisKeyConstants.PRODUCT_CONFIG__CACHE_KEY, product.getId(), JSON.toJSONString(product)); t.exec(); jedis.bgsave(); return product; } } } catch (Exception e) { success = false; if(jedis != null){ jedisPool.returnBrokenResource(jedis); } } finally{ if(success && jedis != null){ jedisPool.returnResource(jedis); } }