zoukankan      html  css  js  c++  java
  • Ehcache基于java API实现

    上代码:

      1 package com.utils.cacheutils;
      2 
      3 import com.situopenapi.constant.EhcacheConstants;
      4 import com.situopenapi.constant.GestureImageConstants;
      5 import com.situopenapi.data.systemUtils.ExecResult;
      6 import net.sf.ehcache.Cache;
      7 import net.sf.ehcache.CacheManager;
      8 import net.sf.ehcache.Element;
      9 
     10 import java.util.List;
     11 
     12 /**
     13  * 使用ehcache进行数据的缓存
     14  *
     15  */
     16 public class EhcacheManager {
     17 
     18     private static CacheManager cacheManager;
     19 
     20     static {
     21         cacheManagerInit();
     22     }
     23 
     24     /**
     25      * EhcacheConstants.CACHE_NAME,                cache name
     26      * EhcacheConstants.MAX_ELEMENTS_MEMORY,       缓存最大个数
     27      * EhcacheConstants.WHETHER_OVERFLOW_TODISK,   内存不足时是否启用磁盘缓存
     28      * EhcacheConstants.WHETHER_ETERNAL,           缓存中的对象是否为永久的,如果是,超过设置将被忽略,对象从不过期
     29      * EhcacheConstants.timeToLiveSeconds,         缓存数据的生存时间;元素从构建到消亡的最大时间间隔值,只在元素不是永久保存时生效;若该值为0表示该元素可以停顿无穷长的时间
     30      * EhcacheConstants.timeToIdleSeconds          对象在失效前的允许闲置时间,仅当eternal=false对象不是永久有效时使用;可选属性,默认值是0可闲置时间无穷大;
     31      */
     32     private static CacheManager cacheManagerInit() {
     33         if (cacheManager == null) {
     34 
     35 
     36             //创建一个缓存管理器
     37             cacheManager = CacheManager.create();
     38             //建立一个缓存实例
     39             Cache memoryOnlyCache = new Cache(EhcacheConstants.CACHE_NAME, EhcacheConstants.MAX_ELEMENTS_MEMORY, EhcacheConstants.WHETHER_OVERFLOW_TODISK, EhcacheConstants.WHETHER_ETERNAL, EhcacheConstants.timeToLiveSeconds, EhcacheConstants.timeToIdleSeconds);
     40             //在内存管理器中添加缓存实例
     41             cacheManager.addCache(memoryOnlyCache);
     42             return cacheManager;
     43         }
     44         return cacheManager;
     45     }
     46 
     47     /**
     48      * 向缓存中添加元素
     49      *
     50      * @param key
     51      * @param value
     52      */
     53     public static void put(String ehcacheInstanceName, String key, ExecResult<Object> value) {
     54         Cache cache = cacheManager.getCache(ehcacheInstanceName);
     55         cache.put(new Element(key, value));
     56 
     57     }
     58 
     59     /**
     60      * 根据key获取对应的value值
     61      *
     62      * @param key
     63      * @return
     64      */
     65     public static Object getValue(String ehcacheInstanceName, Object key) {
     66         Cache cache = cacheManager.getCache(ehcacheInstanceName);
     67         Object value = cache.get(key).getObjectValue();
     68         return value;
     69     }
     70 
     71     /**
     72      *获取缓存中最新的结果
     73      *
     74      * @return   最后一次添加的结果集
     75      */
     76     public static Object getLastValue(String ehcacheInstanceName) {
     77         boolean flag = true;
     78         
     79             Cache cache = cacheManager.getCache(ehcacheInstanceName);
     80             List keys = cache.getKeys();
     81             String string = getMaxKey(keys).toString();
     82             return getValue(ehcacheInstanceName, string);;
     83     }
     84 
     85 
     86     /**
     87      * 在缓存管理器中获取一个缓存实例
     88      * 返回缓存数据个数
     89      *
     90      * @return
     91      */
     92     public static int getCacheSize(String ehcacheInstanceName) {
     93         Cache cache = cacheManager.getCache(ehcacheInstanceName);
     94         return cache.getSize();
     95     }
     96 
     97     private static Object getMaxKey(List source) {
     98 
     99         long[] target = new long[source.size() - 1];
    100         for (int i = 0; i < source.size() - 1; i++) {
    101             target[i] = Long.parseLong((String) source.get(i));
    102         }
    103         long maxKey = target[0];
    104         for (int i = 0; i < target.length; i++) {
    105             if (maxKey < target[i]) {
    106                 maxKey = target[i];
    107             }
    108         }
    109         return maxKey;
    110     }
    111 
    112 }

    顺利完毕!!!

    有疑惑请留言,如若不对之处,感谢提出;

  • 相关阅读:
    微信小程序之文件(图片)使用MD5加密(二)
    微信小程序之文件(图片)使用MD5加密(一)
    02-Django简介
    01-Web框架的原理
    15-pymysql模块的使用
    06-数据类型
    05-表的操作
    04-库的操作
    03-初始mysql语句
    02-MySql安装和基本管理
  • 原文地址:https://www.cnblogs.com/routine/p/10544238.html
Copyright © 2011-2022 走看看