zoukankan      html  css  js  c++  java
  • (十一)分类展示下

                
        上面的操作我们已经可以在每个页面上查看到分类信息了,但是只要换一次页面就会查询一下数据库,增加服务器的压力,
        对于数据不常变化的情况,我们可以使用缓存技术,
            常见的缓存技术
                ehcache:今天用,hibernate中底层使用了ehcache
                memcache
                redis
            ehcache使用步骤:
                1.导入jar包
                2.编写配置文件
                3.使用api
                    获取数据先从缓存中获取
                        若获取的值为空
                            再去查询数据库,
                            将数据放入缓存中

    导入ehcache包

    http://pan.baidu.com/s/1c2AjdTm

    导入ehcache配置文件

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
        
        <diskStore path="C:/ehcache"/>
    
        <cache
                name="categoryCache"
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="true"
                maxElementsOnDisk="10000000"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU"
                />
                
        <!--
            默认缓存配置,
            以下属性是必须的:
                name :cache的标识符,在一个CacheManager中必须唯一。
                maxElementsInMemory : 在内存中缓存的element的最大数目。
                maxElementsOnDisk : 在磁盘上缓存的element的最大数目。
                eternal : 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略。
                overflowToDisk : 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上。
    
            以下属性是可选的:
                 timeToIdleSeconds : 缓存element在过期前的空闲时间。
                 timeToLiveSeconds : 缓存element的有效生命期。
                 diskPersistent : 在VM重启的时候是否持久化磁盘缓存,默认是false。
                 diskExpiryThreadIntervalSeconds : 磁盘缓存的清理线程运行间隔,默认是120秒.
                 memoryStoreEvictionPolicy : 当内存缓存达到最大,有新的element加入的时候,
                    移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO
    
        -->
    </ehcache>

     在service中添加cache

    package com.louis.service.impl;
    
    import java.io.InputStream;
    import java.util.List;
    import java.util.zip.InflaterInputStream;
    
    import com.louis.dao.CategoryDao;
    import com.louis.dao.impl.CategoryDaoImpl;
    import com.louis.domain.Category;
    import com.louis.service.CategoryService;
    
    import net.sf.ehcache.Cache;
    import net.sf.ehcache.CacheManager;
    import net.sf.ehcache.Element;
    
    public class CategoryServiceImpl implements CategoryService {
    
        /*
         * 查询所有的分类
         * */
        public List<Category> findAll() throws Exception {
            //1、创建缓存管理器
            CacheManager cacheManager = CacheManager.create(CategoryServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml"));
            
            //2、获取制定的缓存
            Cache cache = cacheManager.getCache("categoryCache");
            
            //3、通过缓存获取数据,将cache看成map集合
            Element  element = cache.get("clist");
            
            List<Category> list = null;
            //4、判断数据
            if (element == null) {
                //从数据库中获取
                CategoryDao categoryDao = new CategoryDaoImpl();
                list = categoryDao.findAll();
                //将list放入缓存
                cache.put(new Element("clist", list));
                
                System.out.println("缓存中没有数据,已去数据库中获取");
            }else {
                //直接返回
                list = (List<Category>) element.getObjectValue();
                System.out.println("缓存中有数据");
            }
            
            
            
            return list;
        }
    
    
    }

    效果

    问题

    1、加载文件

    //获取的是加载器目录下的文件。classpath
            InputStream inputStream = CategoryServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml");
            System.out.println(inputStream);

    2、ehcache解析

     参考:http://blog.csdn.net/supingemail/article/details/45365521

  • 相关阅读:
    无法在WEB服务器上启动调试
    Zedgraph悬停时显示内容闪烁的解决
    用ZedGraph控件作图圆
    34.node.js之Url & QueryString模块
    33.Node.js 文件系统fs
    32.Node.js中的常用工具类util
    31.Node.js 常用工具 util
    30.Node.js 全局对象
    28.Node.js 函数和匿名函数
    27.Node.js模块系统
  • 原文地址:https://www.cnblogs.com/Michael2397/p/7640433.html
Copyright © 2011-2022 走看看