zoukankan      html  css  js  c++  java
  • 简易的 Cache 使用

    java :

    package com.ehCache;
    
    import java.io.IOException;
    import javax.annotation.PostConstruct;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.Cache;
    import org.springframework.cache.CacheManager;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import com.alibaba.fastjson.JSONArray;/**
     * 数据库中查询市区,记在缓存中
     *
     */
    @Controller
    public class ehCache {
    
        private Cache cache;
        
        @Autowired
        private CacheManager cacheManager;
        @PostConstruct
        public void init() {
            this.cache = cacheManager.getCache("designatedDict");
        }
        @RequestMapping("/ehCache/jsonCache.do")
        public  void jsonCache (HttpServletRequest request,HttpServletResponse response) throws IOException{
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain; charset=UTF-8");
            JSONArray jsonArray = new JSONArray();
            String sheng = request.getParameter("sheng");
            if (this.cache.get(sheng) == null|| this.cache.get(sheng).get() == null) {
                String sql ="";
                Object[] objects;
                if(sheng  !=null){
                    sql = "select * from SHI_QU where SHENG = ?";
                    objects = new Object[] {sheng};
                    RecordSet rs = SQL.execute(sql, objects);
                    while (rs.next()) {
                        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
                        String code = rs.getString("SHI_ID");
                        String  name = rs.getString("SHI");
                        json.put("code", code);
                        json.put("name", name);
                        jsonArray.add(json);
                    }
                    this.cache.put(sheng, jsonArray);
                    System.out.println("jsonArray---->"+jsonArray);
                    System.out.println("this.cache---->"+this.cache);
                    System.out.println((JSONArray) this.cache.get(sheng).get());
            }
            }else{
                jsonArray = (JSONArray) this.cache.get(sheng).get();
            }
            response.getWriter().print(jsonArray.toString());
    }    
        
    
        
        
    }

    cache.xml 配置:

    路径:src/main/resources/ehcache.xml

    <ehcache>
    
        <diskStore path="java.io.tmpdir" />
    
        <defaultCache maxElementsInMemory="10000" eternal="false"
            timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
            diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        </defaultCache>
    
        
        <cache name="designatedDict" maxElementsInMemory="100000" eternal="true"
             overflowToDisk="false" memoryStoreEvictionPolicy="LRU">
        </cache>
        
        
    </ehcache>
  • 相关阅读:
    jQuery tablesort插件推荐
    更改firefox默认搜索引擎
    Chrome Firefox 自定义背景色
    python 基础之列表切片内置方法
    python 基础之while无限循环
    python 基础之for循环有限循环
    python 基础之格式化输出
    nginx之HTTP模块配置
    Kubernetes之pod的属性
    nginx的工作流程
  • 原文地址:https://www.cnblogs.com/lifan12589/p/13097874.html
Copyright © 2011-2022 走看看