zoukankan      html  css  js  c++  java
  • java 缓存ehcache的使用(使用方式一)

    实体要序列化

    resource文件夹下建立 ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
      <diskStore path="java.io.tempdir" />
      <defaultCache maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
      <cache name="ehcacheGO" maxElementsInMemory="10000"
        eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"
        overflowToDisk="true" />
    </ehcache>

    建立ehcache的工具类

    package com.cjt.utils;

    import java.net.URL;

    import net.sf.ehcache.Cache;
    import net.sf.ehcache.CacheManager;
    import net.sf.ehcache.Element;


    public class EhcacheUtil {

      private static final String path = "/ehcache.xml";

      private URL url;

      private CacheManager manager;

      private static EhcacheUtil ehCache;

      private EhcacheUtil(String path) {
        url = getClass().getResource(path);
        manager = CacheManager.create(url);
      }

      public static EhcacheUtil getInstance() {
        if (ehCache== null) {
        ehCache= new EhcacheUtil(path);
       }
        return ehCache;
      }

      public void put(String cacheName, String key, Object value) {
        Cache cache = manager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
      }

      public Object get(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        Element element = cache.get(key);
        return element == null ? null : element.getObjectValue();
      }

      public Cache get(String cacheName) {
        return manager.getCache(cacheName);
      }

      public void remove(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        cache.remove(key);
       }

     }

    ===================================================

    使用ehcache

     @RequestMapping("/showUser")
     public String toIndex(HttpServletRequest request, Model model) {
      System.out.println("UserController showUser");
      int id = Integer.parseInt(request.getParameter("id"));
      User user = userService.getUserById(id);

      //将查询到的结果放入缓存
      EhcacheUtil.getInstance().put("ehcacheGO", "userEch", user);
      model.addAttribute("user", user);
      return "showUser";
     }


     @RequestMapping("/getUser")
     public String toEhcahe(HttpServletRequest request, Model model) {
      System.out.println("use cacher");

         //从缓存中取数据
      User user=(User)EhcacheUtil.getInstance().get("ehcacheGO", "userEch");
      model.addAttribute("userCache", EhcacheUtil.getInstance().get("ehcacheGO", "userEch"));
      model.addAttribute("user", user);
      return "showUser";
      }

     

    上面showUser时发送了sql语句,下面getUser时没有发送sql语句说明直接从缓存中取的数据

  • 相关阅读:
    websphere节点、单元、服务之间的关系
    easyui tree 判断点击的节点是否还存在子节点
    判断程序员在干什么
    配置was7、并部署发布项目!
    解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '
    spring mvc easyui tree 异步加载树
    《再见三山街》
    websphere内存溢出,手动导出was的phd和javacore文件
    js控制 点一下增加一个输入框,点一下增加一个输入框……
    C#中线程调用带有参数的方法
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6229334.html
Copyright © 2011-2022 走看看