使用ehcache做缓存,存储Web数据

版权声明:本文为博主原创文章,未经博主允许不得转载。如果引起争议,需负法律责任,后果自负。 https://blog.csdn.net/lvhaoguang0/article/details/84670915

              做了一个小Demo,如今ehcache喝memcache缓存好像用的都不多,在企业开发中首选使用的都是redis缓存,尽管如此,有时候还是回因为ehcache比较方便,导入配置文件依赖就可以使用,而应用在一些小项目中。操作步骤如下:

          1.首先在web工程src下导入配置文件

 ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="C:/ehcache"/>

	<cache
			name="categoryCache"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
	
	<cache
			name="newProducts"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
    
    <cache
			name="hotProducts"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
	<!--
		默认缓存配置,
		以下属性是必须的:
			name :cache的标识符,在一个CacheManager中必须唯一。
			maxElementsInMemory : 在内存中缓存的element的最大数目。
			maxElementsOnDisk : 在磁盘上缓存的element的最大数目。
			eternal : 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略。
			overflowToDisk : 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上。

		以下属性是可选的:
			 timeToIdleSeconds : 缓存element在过期前的空闲时间。
			 timeToLiveSeconds : 缓存element的有效生命期。
			 diskPersistent : 在VM重启的时候是否持久化磁盘缓存,默认是false。
			 diskExpiryThreadIntervalSeconds : 磁盘缓存的清理线程运行间隔,默认是120秒.
			 memoryStoreEvictionPolicy : 当内存缓存达到最大,有新的element加入的时候,
				移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO
	-->
</ehcache>

 2.在Java程序中加入缓存,首先读取得是缓存中得数据,如果缓存中没有数据,在去数据库中去取数据保存到缓存中,然后从缓存中读取出来,展示到页面上,缓存中必须保存得是不经常变化的数据,通常就是一些一些查询操作的数据。

public class ProductServiceImpl implements ProductService{

	@Override
	public List<Product> findNew() throws Exception {
		System.out.println("这是NewProducts********************************************");
		//2.查询最新商品 热门商品
		// 3.最新商品 首先查看缓存中是否有最新商品
		// 创建创建缓存管理器
		CacheManager cacheManager = CacheManager.create(ProductServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml"));
		// 指定缓存名称
		Cache cache = cacheManager.getCache("newProducts");
		Element newelement = cache.get("newList");
		List<Product> newList = null;
		if (newelement == null) {
			// 缓存为空 查询数据库
			ProductDao dao = new ProductDaoImpl();
			newList = dao.findNew();
			// 放入缓存中
			cache.put(new Element("newList", newList));
			System.out.println("商品缓存为空,已经查询数据库放入缓存中");
		} else {
			@SuppressWarnings("unchecked")
			List<Product> list = (List<Product>) newelement.getObjectValue();
			System.out.println("缓存中保存了商品信息!");
			System.out.println("NewProducts:" + list.toString());
		}
		return newList;
	}

	@Override
	public List<Product> findHot() throws Exception {
		System.out.println("这是*******************************************HotProducts商品");
		//4.热门商品	 首先查看缓存中查看是否有热门商品
		CacheManager cacheManager = CacheManager.create(ProductServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml"));
		Cache cache = cacheManager.getCache("hotProducts");
		Element hotelement = cache.get("hotList");
		List<Product> hotList = null;
		if (hotelement == null) {
			// 缓存为空 查询数据库
			ProductDao dao = new ProductDaoImpl();
			hotList = dao.findHot();
			// 放入缓存中
			cache.put(new Element("hotList", hotList));
			System.out.println("热门商品缓存为空,已经查询数据库放入缓存中");
		} else {
			@SuppressWarnings("unchecked")
			List<Product> list = (List<Product>) hotelement.getObjectValue();
			System.out.println("缓存中保存了热门商品信息!");
			System.out.println("HotProducts:" + list.toString());
		}
		return hotList;
	}
}

   以上是使用SpringMVC 加入ehcache缓存的一个实例,仅供参考!!!

猜你喜欢

转载自blog.csdn.net/lvhaoguang0/article/details/84670915