Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can pretty well do this now with scoped beans, with the one caveat that a bean is NSF specific. Although the XSP Starter kit I believe includes an example of how to do a Server scoped bean (which is really a singleton, meaning there is only one instance of the class for the entire JVM).</p> <p>First create a simple serializable POJO called CachedData that has two member fields, the first is a field that holds a date time value that indicates when you last read the data from the disk, and the second is some sort of list object, like a vector, that holds your values.</p> <p>Then create another POJO called ServerMap that has a map&lt;string, map&lt;string, map&lt;string, map&lt;Object, map&lt;object, CachedData&gt;&gt;&gt;&gt; as a member, and a function called doCachedLookup() or something like that. The parameters to that function can be almost the same as an @DbLookup, server, database, view, key, etc. Then in the doCachedLookup, check your ServerMap for the existence of the specified server as the key. If it doesn't exist, then create a new map and insert it into the ServerMap with the key being the server name. If it does exist, then look up the database name in that map, then the view in the next map, then finally the value in the last map. Once you get the CachedData object, you can check the date time field and see if it is expired, if it isn't, return the vector, and if it is, discard it, and then do a fresh lookup, and re-cache the data, and then return the vector.</p> <p>Here's the code samples, i was a bit lazy in my overloaded methods for getting a column versus getting a field name, and i use some deprecated java date methods, but it will give you a good base to start with. All code is tested:</p> <p>CachedData Class:</p> <pre><code>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&lt;Object&gt; values; public Date getUpdateTime() { return this.updateTime; } public void setUpdateTime(Date UpdateTime) { updateTime = UpdateTime; } public Vector&lt;Object&gt; getValues() { return this.values; } public void setValues(Vector&lt;Object&gt; values) { this.values = values; } } </code></pre> <p>CachedLookup class that is implemented as a singleton so that it can be used server-wide:</p> <pre><code>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&lt;String, HashMap&lt;String, HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;&gt;&gt; cachedLookup; public static CachedLookup getCurrentInstance() { if (_instance == null) { _instance = new CachedLookup(); } return _instance; } private CachedLookup() { HashMap&lt;String, HashMap&lt;String, HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;&gt;&gt; cachedLookupMap = new HashMap&lt;String, HashMap&lt;String, HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;&gt;&gt;(); this.cachedLookup = Collections.synchronizedMap(cachedLookupMap); } @SuppressWarnings("deprecation") public Vector&lt;Object&gt; 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()) &gt; 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&lt;Object&gt; results = new Vector&lt;Object&gt;(); 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&lt;String, HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;&gt;()); } if (!cachedLookup.get(serverName).containsKey(filePath)) { cachedLookup.get(serverName).put(filePath, new HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;()); } if (!cachedLookup.get(serverName).get(filePath).containsKey(viewName)) { cachedLookup.get(serverName).get(filePath).put(viewName, new HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;()); } if (!cachedLookup.get(serverName).get(filePath).get(viewName).containsKey(keyValues)) { cachedLookup.get(serverName).get(filePath).get(viewName).put(keyValues, new HashMap&lt;Object, CachedData&gt;()); } 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&lt;Object&gt; 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()) &gt; 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&lt;Object&gt; results = new Vector&lt;Object&gt;(); 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&lt;String, HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;&gt;()); } if (!cachedLookup.get(serverName).containsKey(filePath)) { cachedLookup.get(serverName).put(filePath, new HashMap&lt;String, HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;&gt;()); } if (!cachedLookup.get(serverName).get(filePath).containsKey(viewName)) { cachedLookup.get(serverName).get(filePath).put(viewName, new HashMap&lt;Object, HashMap&lt;Object, CachedData&gt;&gt;()); } if (!cachedLookup.get(serverName).get(filePath).get(viewName).containsKey(keyValues)) { cachedLookup.get(serverName).get(filePath).get(viewName).put(keyValues, new HashMap&lt;Object, CachedData&gt;()); } 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); } } </code></pre> <p>Example on how to use in an XPage: </p> <pre><code>&lt;xp:text id="text1"&gt; &lt;xp:this.value&gt;&lt;![CDATA[#{javascript: com.ZetaOne.example.CachedLookup.getCurrentInstance().doCachedLookup( database.getServer(), database.getFilePath(), "lookup", "Test Category", "Value", true ) }]]&gt;&lt;/xp:this.value&gt; &lt;/xp:text&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload