码迷,mamicode.com
首页 > 系统相关 > 详细

简易的 Cache 使用

时间:2020-06-12 11:10:19      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:out   resources   gets   efault   fastjson   ping   path   overflow   parameter   

 

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;
import wfc.service.database.RecordSet;
import wfc.service.database.SQL;

/**
 * 数据库中查询市区,记在缓存中
 *
 */
@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>

 

简易的 Cache 使用

标签:out   resources   gets   efault   fastjson   ping   path   overflow   parameter   

原文地址:https://www.cnblogs.com/lifan12589/p/13097874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!