Commons DbUtils QueryLoader查询Map缓存

QueryLoader类,是一个从一个文件加载查询到一个Map的简单的类。然后,当需要的时候,你从Map中选择一些查询。当然了,这个方法的实现是比较简单的。现看看文件载入的源代码:

 

Java代码   收藏代码
  1. /** 
  2.  * 载入一个查询命名和SQL值映射的Map集合. 
  3.  * 此Map被缓存以便以后相同路径的请求可以返回被缓存的Map 
  4.  * Loads a Map of query names to SQL values.  The Maps are cached so a  
  5.  * subsequent request to load queries from the same path will return 
  6.  * the cached Map. 
  7.  *  
  8.  * @param path The path that the ClassLoader will use to find the file.  
  9.  * ClassLoader通过path查找文件 
  10.  *  
  11.  * This is <strong>not</strong> a file system path.  If you had a jarred 
  12.  * Queries.properties file in the com.yourcorp.app.jdbc package you would  
  13.  * pass "/com/yourcorp/app/jdbc/Queries.properties" to this method. 
  14.  * 这不是一个文件系统路径。如果你有一个Queries.properties文件在com.yourcorp.app.jdbc这个包下, 
  15.  * 那么你应该传递"/com/yourcorp/app/jdbc/Queries.properties"参数到这个方法. 
  16.  *  
  17.  * @throws IOException if a file access error occurs 
  18.  * @throws IllegalArgumentException if the ClassLoader can't find a file at 
  19.  * the given path. 
  20.  * @return Map of query names to SQL values 
  21.  */  
  22. public synchronized Map<String,String> load(String path) throws IOException {  
  23.   
  24.     Map<String,String> queryMap = (Map<String,String>) this.queries.get(path);  
  25.   
  26.     if (queryMap == null) {  
  27.         queryMap = this.loadQueries(path);  
  28.         this.queries.put(path, queryMap);  
  29.     }  
  30.   
  31.     return queryMap;  
  32. }  

    这个方法加入了同步锁机制,所以是线程安全的,这个方法需要注意的一点就是传入的路径,因为是通过ClassLoader载入,所以,传入的路径是绝对路径名。首先呢,先从本地的Map集合queries拿到路径名里对应的集合,如果为空,则说明没有缓存对不对,OK,没有就加呗,来loadQueries方法:

 

Java代码   收藏代码
  1. /** 
  2.  * Loads a set of named queries into a Map object.  This implementation 
  3.  * reads a properties file at the given path. 
  4.  *  
  5.  * 加载命名查询集到一个Map对象中. 
  6.  * 这个实现用于读取给定路径的Properties文件 
  7.  *  
  8.  * @param path The path that the ClassLoader will use to find the file. 
  9.  *      ClassLoader使用指定的path去查找file 
  10.  * @throws IOException file访问异常 
  11.  * @throws IllegalArgumentException ClassLoader查找不到指定路径的文件 
  12.  * @since DbUtils 1.1 
  13.  * @return Map of query names to SQL values 查询名称到SQL值的映射集合 
  14.  */  
  15. @SuppressWarnings("unchecked")  
  16. protected Map<String,String> loadQueries(String path) throws IOException {  
  17.     // Findbugs flags getClass().getResource as a bad practice; maybe we should change the API?  
  18.       
  19.     InputStream in = getClass().getResourceAsStream(path);//获取指定文件的流对象  
  20.   
  21.     if (in == null) {  
  22.         throw new IllegalArgumentException(path + " not found.");  
  23.     }  
  24.   
  25.     Properties props = new Properties();  
  26.     props.load(in);  
  27.   
  28.     // Copy to HashMap for better performance  
  29.     return new HashMap(props);  
  30. }  

    哝,有没有?!通过ClassLoader载入指定文件流对象,如果为空,则会抛出找不到文件的异常。否则呢,载入Properties文件并以HashMap返回。

   载入了未缓存的properties文件,那么,存一个:

 

Java代码   收藏代码
  1. this.queries.put(path, queryMap);  

   完成了载入、缓存,缓存这个东西呢是要占内存的,所以呢,不要缓存太多或者大的对象,除非有必要,建议各位用完就remove掉:

 

Java代码   收藏代码
  1. /** 
  2.  * Removes the queries for the given path from the cache. 
  3.  *  从缓存中删除指定的路径 
  4.  * @param path The path that the queries were loaded from. 
  5.  *   queries载入的路径 
  6.  */  
  7. public synchronized void unload(String path){  
  8.     this.queries.remove(path);  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值