缓存框架Ehcacahe的学习与使用(二):Ehcache入门使用

1. 添加pom.xml中引入依赖

    
    
	net.sf.ehcache
	ehcache-core
	2.4.3<
    

2.创建ehcache.xml配置文件



	
	

	
	
	
	
	
	

	
		
	

	
		
	

	

3.创建CacheManager

    CacheManagerEhcache框架的核心类和入口,它负责管理一个或多个Cache对象。要使用Ehcache框架,必须要先创建CacheManager对象,CacheManagerstatic类型的创建方法包括:create()getInstance()

import net.sf.ehcache.CacheManager;

/**
 * @Description: Ehcache
 * @author Roy_Carroll
 * @email [email protected]
 */
 
public class EHCACHE {  
    public static void main(String[] args) {  
        CacheManager manager1 = CacheManager.getInstance();  
        CacheManager manager2 = CacheManager.create();  
        System.out.println(manager1 == manager2);// true  	
    } 
}

4.持久化数据

    ehcache可以在程序突然异常中止后,将原来缓存中的数据在程序进行重启的时候重新加载到缓存中去。

  • 在web.xml中加入监听 
 
    net.sf.ehcache.constructs.web.ShutdownListener

  • 在ehcache.xml文件中修添加配置
 

  • 创建CacheManager对象时setProperty
private static void ehcacheSetUp() {  
    	System.setProperty("net.sf.ehcache.enableShutdownHook", "true"); //持久化使用
    	manager = CacheManager.create();  
    }

  • 在get方法的时候使用flush()
    因为ehcache 持久化数据是根据 .index 索引文件进行数据恢复,从而达到数据持久化的目的,但程序启动以后无法加载之前缓存中的数据,所以应在使用 get 方法时使用 flush() 方法,将索引重写回到缓存。

5.ehcache的简单读写操作

private static Element readSomeData(Cache cache, String key, String value) {
    return cache.get(key);               
}
private static void writeSomeData(Cache cache, String key, String value) { 
    cache.put(new Element(key, value));  
} 

6.txt文档进行读取的简单Demo

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @Description: Ehcache
 * @author Roy_Carroll
 * @email [email protected]
 */
 
public class cache {  

    public static final String AUTHOR_CODE_CACHE = "authorCodecache"; 
    public static Cache authorCodeCache; 
    public static CacheManager manager ; 
    
    public static void main(String[] args) {
    	File file = new File("C:/Users/Administrator/Desktop/testData.txt");
    	StringBuilder filetxt = new StringBuilder();
    	String value = filetxt.append(txt2String(file)).toString();
    	if (manager==null&&authorCodeCache == null) {
    		System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
    		manager = CacheManager.getInstance(); 
    		authorCodeCache = manager.getCache(AUTHOR_CODE_CACHE); 
    		ehcacheUse(authorCodeCache,value);
    		}  	
    	}
  
    private static void ehcacheUse(Cache cache,String value) {  
        cache.getSize();
        String key = "20180510";    
        Element element1 = new Element(key, value);
        cache.put(element1);
        Element element2 = readSomeData(cache, key, value);
        System.out.println(element2);   
    }  
 
    private static Element readSomeData(Cache cache, String key, String value) {
    	return cache.get(key);               
    } 
    
    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
}

  • 运行结果(随便找了个我的文档,内容无关紧要可以忽略):
[ key = 20180510, value=
SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';
show VARIABLES like '%general_log_file%';
show VARIABLES like '%slow_query_log_file%';
show status like '%lock%';
set global innodb_buffer_pool_size=1610612736
set global query_cache_size=2097152
set global back_log=1500
set global thread_cache_size=50
set global query_cache_size=2097152
set global thread_cache_size=50, version=1, hitCount=1, CreationTime = 1526370703166, LastAccessTime = 1526370703182 ]

 
 

你可能感兴趣的:(缓存框架Ehcacahe的学习与使用(二):Ehcache入门使用)