java-web 常见的缓冲技术

 

使用缓存技术:对程序进行优化.

* 缓存:其实就是内存中的一块空间.可以使用缓存将数据源中的数据拿到,存入到内存中.后期获得数据的话 从缓存中进行获得.

* 常见欢送有以下几种

1.EHCache         :是Hibernate常使用的二级缓存的插件.

2.Memcache       :

3.Redis                :

        

 

ehcache

* 使用ehcache:

    * 引入jar包:

    * 引入配置文件到src目录下

 

jar包结构如图:

java-web 常见的缓冲技术_第1张图片

配置文件



    

	
			
	

 

测试代码

public static void aTest() throws AddressException, MessagingException {
		//通过配置文件的流对象创建ehcache实例
		InputStream is = Test.class.getClassLoader().getResourceAsStream("ehcache.xml");
		CacheManager cm = CacheManager.create(is);
		//按cache name获得cacha
		Cache cache = cm.getCache("categoryCache");
		
		//模拟加入数据数据
		List slist = new ArrayList<>();
		slist.add("a");
		slist.add("b");
		slist.add("c");
		
		cache.put(new Element("testKey", "testVal"));
		cache.put(new Element("testKey2", slist));
		
		//按key获得数据并打印
		Element element = cache.get("testKey");
		System.out.println(element.getObjectKey());
		
		Element element2 = cache.get("testKey2");
		System.out.println(element2.getObjectValue().toString());
	}

输出结果

 

你可能感兴趣的:(JAVA,练习,缓冲,JAVA_WEB)