2012-03-06 56 views
6

我们有许多查找数据库,我们在多个应用程序中使用,我试图找出通过java函数使这些查找数据库可用的最佳和最有效的方法,或者bean在OSGi插件库中。在java中为xpages构建一个缓存查找系统

我想实现的是创建一个函数,我可以传递查找键和字段名称,该函数将返回正确的值和可能的对象类型(处理datatime值)的某种方式。它还需要在应用程序级缓存大约一个小时的值,因为这些查找文档根本没有改变。

通常我会想使用它们用于显示目的,这样我只需要密钥存储在我的笔记文档,然后使用类似下面的在屏幕上显示什么,我需要

<xp:text escape="true" id="computedField1"> 
    <xp:this.value><![CDATA[#{javascript:com.mycompany.lookup.GetDoc("docID","fieldName")}]]></xp:this.value> 
</xp:text> 
+0

可以肯定的,它需要一个OSGi的插件?或者也可以用数据库中定义的userbean完成?有关查找数据库的信息存储在哪里? – jjtbsomhorst 2012-03-06 15:28:19

+0

它可能是一个在OSGi插件中定义的bean。我的想法是,让它在插件中对服务器来说是全局的,所以任何需要查找的应用程序都可以调用函数来返回值。查找数据库位于服务器上的固定位置。 – 2012-03-06 15:32:23

回答

8

你现在可以很好地使用scoped bean来做到这一点,但有一点需要注意,bean是NSF特定的。虽然我相信XSP入门套件包含了一个如何执行Server scoped bean的例子(这是一个单例,意味着整个JVM只有一个类的实例)。

首先创建一个简单的可序列化的POJO,它叫做CachedData,它有两个成员字段,第一个是一个字段,它包含一个日期时间值,指示您最后一次从磁盘读取数据的时间值,第二个是某种列表对象,就像一个载体一样,它保存着你的价值观。

然后创建另一个POJO称为ServerMap具有地图<串,地图<串,地图<串,地图<对象,地图<对象,CachedData > > > >作为成员,和一个称为doCachedLookup()的函数或类似的东西。该函数的参数可以与@DbLookup,服务器,数据库,视图,键等几乎相同。然后在doCachedLookup中,检查ServerMap是否存在指定的服务器作为密钥。如果它不存在,则创建一个新映射并将其插入ServerMap中,其中键为服务器名称。如果确实存在,则查找该地图中的数据库名称,然后查看下一张地图中的视图,最后查看最后一张地图中的值。一旦你获得了CachedData对象,你可以检查日期时间字段,看看它是否过期,如果不是,返回向量,如果是,丢弃它,然后重新进行查找,并重新缓存数据,然后返回矢量。

这里是代码示例,我在重载方法中获取列与获取字段名称有点懒,我使用了一些不推荐的java日期方法,但它会为您提供一个很好的基础。所有代码进行测试:

CachedData类别:如何在使用

package com.ZetaOne.example; 

import java.io.Serializable; 
import java.util.Date; 
import java.util.Vector; 
import com.ZetaOne.example.CachedData; 
import java.util.HashMap; 
import java.util.Collections; 
import java.util.Map; 

import lotus.domino.Session; 
import lotus.domino.Database; 
import lotus.domino.View; 
import lotus.domino.NotesException; 
import lotus.domino.ViewEntryCollection; 
import lotus.domino.ViewEntry; 
import lotus.domino.Document; 

import javax.faces.context.FacesContext; 

public class CachedLookup implements Serializable { 

    private static CachedLookup _instance; 

    private static final long serialVersionUID = 1L; 
    private Map<String, HashMap<String, HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>>> cachedLookup; 

    public static CachedLookup getCurrentInstance() { 
     if (_instance == null) { 
      _instance = new CachedLookup(); 
     } 
     return _instance; 
    }  

    private CachedLookup() { 
     HashMap<String, HashMap<String, HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>>> cachedLookupMap = 
      new HashMap<String, HashMap<String, HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>>>(); 
     this.cachedLookup = Collections.synchronizedMap(cachedLookupMap); 
    } 

    @SuppressWarnings("deprecation") 
    public Vector<Object> doCachedLookup(String serverName, String filePath, String viewName, Object keyValues, int columnNumber, boolean exactMatch) { 

     if (cachedLookup.containsKey(serverName)) { 
      if (cachedLookup.get(serverName).containsKey(filePath)) { 
       if (cachedLookup.get(serverName).get(filePath).containsKey(viewName)) { 
        if (cachedLookup.get(serverName).get(filePath).get(viewName).containsKey(keyValues)) { 
         if (cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).containsKey(columnNumber)) { 
          CachedData cache = cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).get(columnNumber); 
          if (cache.getUpdateTime().compareTo(new Date()) > 0) { 
           System.out.println("Cache Hit"); 
           return cache.getValues(); 
          } 
         } 
        } 
       } 
      } 
     } 

     System.out.println("Cache Miss"); 
     // if we drop to here, cache is either expired or not present, do the lookup. 

     try { 
      Session session = (Session)resolveVariable("session"); 
      Database db = session.getDatabase(serverName, filePath); 
      View view = db.getView(viewName); 
      ViewEntryCollection vc = view.getAllEntriesByKey(keyValues, exactMatch); 
      ViewEntry ve, vn; 
      ve = vc.getFirstEntry(); 
      Vector<Object> results = new Vector<Object>(); 
      while (ve != null) { 
       results.add(ve.getColumnValues().elementAt(columnNumber)); 

       vn = vc.getNextEntry(); 
       ve.recycle(); 
       ve = vn; 
      } 

      vc.recycle(); 

      if (!cachedLookup.containsKey(serverName)) { 
       cachedLookup.put(serverName, new HashMap<String, HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>>()); 
      } 

      if (!cachedLookup.get(serverName).containsKey(filePath)) { 
       cachedLookup.get(serverName).put(filePath, new HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>()); 
      } 

      if (!cachedLookup.get(serverName).get(filePath).containsKey(viewName)) { 
       cachedLookup.get(serverName).get(filePath).put(viewName, new HashMap<Object, HashMap<Object, CachedData>>()); 
      } 

      if (!cachedLookup.get(serverName).get(filePath).get(viewName).containsKey(keyValues)) { 
       cachedLookup.get(serverName).get(filePath).get(viewName).put(keyValues, new HashMap<Object, CachedData>()); 
      } 

      CachedData cache; 
      if (cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).containsKey(columnNumber)) { 
       cache = cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).get(columnNumber); 
      } else { 
       cache = new CachedData(); 
      } 

      Date dt = new Date(); 
      dt.setHours(dt.getHours() + 1); 
      cache.setUpdateTime(dt); 
      cache.setValues(results);   

      cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).put(columnNumber, cache); 

      view.recycle(); 
      db.recycle(); 

      return results; 

     } catch (NotesException e) { 
      // debug here, im lazy 
      return null; 
     } 
    } 

    public Vector<Object> doCachedLookup(String serverName, String filePath, String viewName, Object keyValues, String fieldName, boolean exactMatch) { 

     if (cachedLookup.containsKey(serverName)) { 
      if (cachedLookup.get(serverName).containsKey(filePath)) { 
       if (cachedLookup.get(serverName).get(filePath).containsKey(viewName)) { 
        if (cachedLookup.get(serverName).get(filePath).get(viewName).containsKey(keyValues)) { 
         if (cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).containsKey(fieldName)) { 
          CachedData cache = cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).get(fieldName); 
          if (cache.getUpdateTime().compareTo(new Date()) > 0) { 
           System.out.println("Cache Hit");         
           return cache.getValues(); 
          } 
         } 
        } 
       } 
      } 
     } 

     System.out.println("Cache Miss");   
     // if we drop to here, cache is either expired or not present, do the lookup. 

     try { 
      Session session = (Session)resolveVariable("session"); 
      Database db = session.getDatabase(serverName, filePath); 
      View view = db.getView(viewName); 
      ViewEntryCollection vc = view.getAllEntriesByKey(keyValues, exactMatch); 
      ViewEntry ve, vn; 
      ve = vc.getFirstEntry(); 
      Vector<Object> results = new Vector<Object>(); 
      while (ve != null) { 
       Document doc = ve.getDocument(); 
       results.add(doc.getItemValue(fieldName)); 
       doc.recycle(); 

       vn = vc.getNextEntry(); 
       ve.recycle(); 
       ve = vn; 
      } 

      vc.recycle(); 

      if (!cachedLookup.containsKey(serverName)) { 
       cachedLookup.put(serverName, new HashMap<String, HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>>()); 
      } 

      if (!cachedLookup.get(serverName).containsKey(filePath)) { 
       cachedLookup.get(serverName).put(filePath, new HashMap<String, HashMap<Object, HashMap<Object, CachedData>>>()); 
      } 

      if (!cachedLookup.get(serverName).get(filePath).containsKey(viewName)) { 
       cachedLookup.get(serverName).get(filePath).put(viewName, new HashMap<Object, HashMap<Object, CachedData>>()); 
      } 

      if (!cachedLookup.get(serverName).get(filePath).get(viewName).containsKey(keyValues)) { 
       cachedLookup.get(serverName).get(filePath).get(viewName).put(keyValues, new HashMap<Object, CachedData>()); 
      } 

      CachedData cache; 
      if (cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).containsKey(fieldName)) { 
       cache = cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).get(fieldName); 
      } else { 
       cache = new CachedData(); 
      } 

      Date dt = new Date(); 
      dt.setHours(dt.getHours() + 1); 
      cache.setUpdateTime(dt); 
      cache.setValues(results);   

      cachedLookup.get(serverName).get(filePath).get(viewName).get(keyValues).put(fieldName, cache); 

      view.recycle(); 
      db.recycle(); 

      return results; 

     } catch (NotesException e) { 
      // debug here, im lazy 
      return null; 
     } 
    } 

    private static Object resolveVariable(String variable) { 
     return FacesContext.getCurrentInstance().getApplication() 
       .getVariableResolver().resolveVariable(
         FacesContext.getCurrentInstance(), variable); 
    } 

} 

示例:被实现为一个单,使得它可以用于服务器范围

package com.ZetaOne.example; 

import java.io.Serializable; 
import java.util.Date; 
import java.util.Vector; 

public class CachedData implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private Date updateTime; 
    private Vector<Object> values; 

    public Date getUpdateTime() { 
     return this.updateTime; 
    } 

    public void setUpdateTime(Date UpdateTime) { 
     updateTime = UpdateTime; 
    } 

    public Vector<Object> getValues() { 
     return this.values; 
    } 

    public void setValues(Vector<Object> values) { 
     this.values = values; 
    } 
} 

CachedLookup类一个XPage:

<xp:text id="text1"> 
    <xp:this.value><![CDATA[#{javascript: 
     com.ZetaOne.example.CachedLookup.getCurrentInstance().doCachedLookup(
      database.getServer(), 
      database.getFilePath(), 
      "lookup", 
      "Test Category", 
      "Value", 
      true 
     ) 
    }]]></xp:this.value> 
</xp:text> 

+1

这真的很有帮助。由于我的查询数据库是固定的,如果需要传递给函数的变量只是键和字段以及查找所需的其余部分在函数中设置,我会减少数量。谢谢你。 – 2012-03-06 19:28:18

+0

我没有任何关于服务器范围bean的经验,但是这样一个bean需要多久才会失效?在nsf级别,您可以指定应用程序从内存中清除之前的超时时间。 – jjtbsomhorst 2012-03-07 05:29:23

+1

这与服务器范围的bean不同,它与视图范围的bean相同。它的whats称为单例,意味着只有一个实例被创建并用于整个JVM,并且在JVM重新启动之前不会死亡。既然如此,那就是为什么CachedData具有updateTime成员,它控制着高速缓存数据的有效生存期。此示例不会剔除任何缓存,因此即使缓存不再有效,缓存仍位于内存中,而更强大的实现可能可用于在指定的时间段内剔除缓存记录。 – 2012-03-07 14:50:01

2

当你做bean时,你可以使用EL来获取比SSJS更快的内容。并且是 - 使用XSP Starter套件中的服务器范围。对于缓存:不要重新发明轮子!有各种可用的花式缓存的一个非常完整的实现:http://commons.apache.org/jcs/我用在Tomcat应用之前和它的工作非常可靠。我不明白为什么它不适合你的任务。