• 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语句说明直接从缓存中取的数据

  • 相关阅读:
    Windows下MySQL8.0.23的下载与安装简单易用
    【转】decimal double的区别
    【转】.NET垃圾回收
    vs2010 断点调试故障 反编译插件引起的
    【摘】别人对面向对象的理解
    【转】C# indexof
    【转】八大排序算法总结
    【转】JS windows.open()详解
    【转】with as
    【转】SQL Server的几种约束
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6229334.html
Copyright © 2020-2023  润新知