gpt4 book ai didi

xpages - 用java为xpages构建一个缓存查找系统

转载 作者:行者123 更新时间:2023-12-04 14:04:25 27 4
gpt4 key购买 nike

我们有许多在多个应用程序中使用的查找数据库,我试图找出通过 Java 函数或 OSGi 插件库中的 bean 使这些查找数据库可用的最佳和最有效的方法。

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

通常,我希望将它们用于显示目的,这样我只需将 key 存储在我的笔记文档中,然后使用类似以下内容的内容在屏幕上显示我需要的内容

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

最佳答案

您现在可以使用作用域 bean 很好地做到这一点,但需要注意的是 bean 是特定于 NSF 的。尽管我相信 XSP Starter kit 包含了一个关于如何做服务器作用域 bean 的例子(这实际上是一个单例,意味着整个 JVM 只有一个类的实例)。

首先创建一个名为 CachedData 的简单可序列化 POJO,它有两个成员字段,第一个是保存日期时间值的字段,该值指示您上次从磁盘读取数据的时间,第二个是某种列表对象,如向量,这就是你的值(value)观。

然后创建另一个名为 ServerMap 的 POJO,它有一个 map >>> 作为成员,以及一个名为 doCachedLookup() 或类似的函数。该函数的参数几乎可以与@DbLookup、服务器、数据库、 View 、键等相同。然后在doCachedLookup 中,检查您的ServerMap 是否存在指定的服务器作为键。如果它不存在,则创建一个新映射并将其插入到 ServerMap 中,键是服务器名称。如果确实存在,则在该映射中查找数据库名称,然后在下一个映射中查找 View ,最后在最后一个映射中查找值。拿到CachedData对象后,可以查看日期时间字段是否过期,如果没有,返回vector,如果是,丢弃,然后重新查找,重新缓存数据,然后返回向量。

这是代码示例,我在获取列与获取字段名称的重载方法中有点懒惰,并且我使用了一些不推荐使用的 Java 日期方法,但它会给你一个很好的基础。所有代码都经过测试:

缓存数据类:

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 类作为单例实现,以便它可以在服务器范围内使用:
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);
}

}

关于如何在 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>

关于xpages - 用java为xpages构建一个缓存查找系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9586449/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com