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

  • 相关阅读:
    python的类的继承和多继承
    python的类访问控制、私有属性
    python的面向对象、类方法和静态方法
    python的序列化和反序列化
    python高阶函数、柯里化、装饰器、参数注解
    python函数参数、参数解构、作用域、递归及匿名函数
    python内置函数列表解析、生成器
    python内置函数字典(dict)
    python内置函数集合(set)
    定制自己的打印函数---进阶篇
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/6229334.html
Copyright © 2011-2022 走看看