zoukankan      html  css  js  c++  java
  • Redis的二八定律

    复制代码
      1 package com.itheima.store.service.impl;
      2 
      3 import java.sql.SQLException;
      4 import java.util.List;
      5 
      6 import com.itheima.store.dao.CategoryDao;
      7 import com.itheima.store.dao.impl.CategoryDaoImpl;
      8 import com.itheima.store.domain.Category;
      9 
     10 /**
     11  * 分类模块:业务层的实现类。
     12  * */
     13 
     14 import com.itheima.store.service.CategoryService;
     15 import com.itheima.store.utils.BeanFactory;
     16 import com.itheima.store.utils.DBUtil;
     17 import com.itheima.store.utils.JedisUtils;
     18 
     19 import net.sf.json.JSONArray;
     20 import redis.clients.jedis.Jedis;
     21 
     22 @SuppressWarnings("all")
     23 public class CategoryServiceImpl implements CategoryService {
     24 
     25     // @Override
     26     // 异步加载查询所有分类的方法:
     27     public List<Category> findAllCategory() throws Exception {
     28         // CategoryDao dao = new CategoryDaoImpl();
     29         CategoryDao dao = (CategoryDao) BeanFactory.getBean("CategoryDao");
     30         return dao.findAllCategory();
     31     }
     32 
     33     @Override
     34     // 缓存技术查询所有分类的方法:
     35     /*
     36      * 为了提升程序的性能,在这里将加入缓存的技术(redis),将获取到的分类数据放进缓存中,当用户下次再访问时,直接从缓存中获取数据,从而
     37      * 减少了和数据库的交互。
     38      */
     39     public String findAllCategoryAjax() throws Exception {
     40         // 创建jedis对象
     41         Jedis jedis = null;
     42         try {
     43             // 获得jedis对象
     44             jedis = JedisUtils.getJedis();
     45             // 首先获取缓存中是否已经有分类数据
     46             String categoryList = jedis.get("category_list");
     47             if (categoryList == null) {
     48                 // 说明缓存中没有数据,则去数据库查询
     49                 System.out.println("---------数据库中的数据-----------");
     50                 // CategoryDao dao = new CategoryDaoImpl();
     51                 CategoryDao dao = (CategoryDao) BeanFactory.getBean("CategoryDao");
     52                 List<Category> list = dao.findAllCategory();
     53                 // 将list转成JSON格式的数据
     54                 JSONArray json = JSONArray.fromObject(list);
     55                 // 将JSON数据放进缓存中,并将查询到的数据返回给页面
     56                 jedis.set("category_list", json.toString());
     57                 return jedis.get("category_list");
     58             } else {
     59                 // 如果缓存已经有数据,则将缓存中的数据返回给页面
     60                 System.out.println("--------------缓存中的数据-----------------");
     61                 return categoryList;
     62             }
     63         } catch (Exception e) {
     64             e.printStackTrace();
     65         } finally {
     66             JedisUtils.closeJedis(jedis);
     67         }
     68         return null;
     69     }
     70 
     71     @Override
     72     // 添加分类:
     73     public void addCategory(Category category) throws Exception {
     74         CategoryDao dao = (CategoryDao) BeanFactory.getBean("CategoryDao");
     75         dao.addCategory(category);
     76         // 注意:在修改了category表后,因为缓存中的数据还没有更新,前台页面的数据也没有更新,所以一旦改变了缓存中的数据后记得将缓存清空
     77         // 清空缓存:
     78         Jedis jedis = null;
     79         try {
     80             jedis = JedisUtils.getJedis();
     81             // 删除存放分类数据的key
     82             jedis.del("category_list");
     83         } catch (Exception e) {
     84             e.printStackTrace();
     85         } finally {
     86             JedisUtils.closeJedis(jedis);
     87         }
     88     }
     89 
     90     @Override
     91     // 根据cid查询分类数据:
     92     public Category findByCid(String cid) throws Exception {
     93         CategoryDao dao = (CategoryDao) BeanFactory.getBean("CategoryDao");
     94         Category category = dao.findByCid(cid);
     95         return category;
     96     }
     97 
     98     @Override
     99     // 编辑分类数据:
    100     public void editCategory(Category category) throws Exception {
    101         CategoryDao dao = (CategoryDao) BeanFactory.getBean("CategoryDao");
    102         dao.editCategory(category);
    103         // 清空缓存:
    104         Jedis jedis = null;
    105         try {
    106             jedis = JedisUtils.getJedis();
    107             // 删除存放分类数据的key
    108             jedis.del("category_list");
    109         } catch (Exception e) {
    110             e.printStackTrace();
    111         } finally {
    112             JedisUtils.closeJedis(jedis);
    113         }
    114     }
    115 
    116     @Override
    117     //删除分类数据:
    118     /**
    119      * 注意:因为分类表和商品表有着外检约束的关系,所以在删除分类数据之前,
    120      *         要先删除该分类所关联的所有商品数据或将该分类所关联的所有商品的外键改变,则牵扯到事务。
    121      * */
    122     public void deleteCategory(String cid) throws Exception {
    123         Jedis jedis = null;
    124         try {
    125             //开启事务:
    126             DBUtil.startTransaction();
    127             CategoryDao dao = (CategoryDao) BeanFactory.getBean("CategoryDao");
    128             //首先根据当前cid修改商品表中的外键为null:
    129             dao.updateByCid(cid);
    130             dao.deleteCategory(cid);
    131             //提交事务:
    132             DBUtil.commit();
    133             // 清空缓存:
    134             jedis = JedisUtils.getJedis();
    135             // 删除存放分类数据的key
    136             jedis.del("category_list");
    137         } catch (Exception e) {
    138             //回滚事务:
    139             DBUtil.rollBack();
    140             e.printStackTrace();
    141         } finally {
    142             JedisUtils.closeJedis(jedis);
    143         }
    144     }
    145 
    146 }
    复制代码
  • 相关阅读:
    13、字符串相互匹配删除
    12、指定长度替换制表符
    11、输入字符颠倒输出
    10、字符串输入删除末尾特殊符,清除空行
    9、筛选满足长度条件的输入内容进行输出
    8、获取输入的字符串并输出最长的那个
    7、初识函数
    6、计数垂直直方图输出
    5、计数水平直方图输出
    Tiny4412 Uboot
  • 原文地址:https://www.cnblogs.com/wangchaoyuana/p/7545247.html
Copyright © 2011-2022 走看看