相关文章推荐
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入您要查询的商品名称:"); String goodsName = sc.nextLine(); //先去redis中查询,如果redis中有该数据,就直接返回 //若redis中没有查询到该数据,就去mysql中查询,返回查询的结果,同时向redis中写一个该数据并设置过期时间 String info = selectGoods(goodsName); System.out.println("查询结果为:"); System.out.println(info); public static String selectGoods(String name) { System.out.println(name); Jedis redisConn = RedisTool.REDIS_CONN; String res = redisConn.get(name); String info = null; try { if (res != null) { System.out.println("=====================走redis查询====================="); info = res; }else { System.out.println("=====================走mysql业务数据库查询====================="); //redis中没有该缓存,去业务数据库中查询 Connection conn = MySqlTool.getConnection(); PreparedStatement prep = conn.prepareStatement("select * from jd_goods where goods_name=?"); prep.setString(1, name); ResultSet resultSet = prep.executeQuery(); while (resultSet.next()){ String goods_name = resultSet.getString(1); String price = resultSet.getString(2); String comments = resultSet.getString(3); String shop = resultSet.getString(4); String icos = resultSet.getString(5); info = goods_name+"|"+price+"|"+comments+"|"+shop+"|"+icos; //同时向redis中添加一个缓存 redisConn.set(name, info); redisConn.expire(name,86400); }catch (Exception e){ e.printStackTrace(); return info;
 
推荐文章