你现在可以很好地使用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>
可以肯定的,它需要一个OSGi的插件?或者也可以用数据库中定义的userbean完成?有关查找数据库的信息存储在哪里? – jjtbsomhorst 2012-03-06 15:28:19
它可能是一个在OSGi插件中定义的bean。我的想法是,让它在插件中对服务器来说是全局的,所以任何需要查找的应用程序都可以调用函数来返回值。查找数据库位于服务器上的固定位置。 – 2012-03-06 15:32:23