2012-10-05 35 views
3

我有一個緩存bean,用於在應用程序中查找/存儲有關對象的信息。我想盡可能少地提取視圖,因爲我會想象每個Database.getView都有一定的代價。測試View是否被回收的最便宜方式是什麼?

觸發「查看已被回收」的最便宜方式是什麼?

+2

我也有這個問題,但已經離開緩存Domino對象,因爲閱讀http://www.intec.co .uk/object-has-been-removed-or-recycled-when-logic-wrong-wrong/by Paul Withers。我現在只緩存文檔字段中的UNID或值。像Nathan在評論中說的,我們可以在Domino對象中使用「isRecycled()」方法。 –

+0

我從不緩存文檔。我想緩存視圖的原因是文檔查找是臨時/每個項目。當bean數據不再是最新的時候,文檔值被讀入bean中。有時可能有100多種查找(= 100 + getView)。當視圖沒有被緩存時,我仍然可以獲得良好的性能,但是如果我可以獲得更好的性能,那將會很不錯:) –

+0

您可以期待每個Notes對象在請求階段後被回收。 –

回答

2

從斯文哈塞爾巴赫靈感後,我創造了這個方法:

/* 
Classes that need to be imported: 
import java.lang.reflect.Method; 
import lotus.domino.Base; 
import lotus.domino.local.NotesBase; 
*/ 

public static boolean isRecycled(Base object) { 
    boolean isRecycled = true; 
    if(! (object instanceof NotesBase)) { 
     // No reason to test non-NotesBase objects -> isRecycled = true 
     return isRecycled; 
    } 

    try { 
     NotesBase notesObject = (NotesBase) object; 
     Method isDead = notesObject.getClass().getSuperclass().getDeclaredMethod("isDead"); 
     isDead.setAccessible(true);  

     isRecycled = (Boolean) isDead.invoke(notesObject); 
    } catch (Throwable exception) { 
     // Exception handling 
    } 

    return isRecycled; 
} 

更新:它似乎使用這種方法需要修改java.policy文件。特別是這行:notesObject.getClass()。getSuperclass()。getDeclaredMethod(「isDead」)

+0

這會在我的853服務器上引發SecurityException(「不允許訪問類類lotus.domino.local.NotesDatabase中的成員」)以檢索isDead聲明的方法。我有一個默認的java.policy。你有沒有改變那個文件? –

+0

我做到了。我會更新帖子。 –

+0

@MarkLeusink:請嘗試授予*權限java.lang.reflect.ReflectPermission * *「suppressAccessChecks」; * –

3

設計用於測試各種Domino對象的測試器類如何?

如果對象被回收,您可以執行一個操作,它會引發異常 - 而不僅僅是測試null。會像下面的代碼工作,還是我過於簡單?

package com.azlighthouse.sandbox; 

import lotus.domino.Database; 
import lotus.domino.Document; 
import lotus.domino.NotesException; 

public class NPHchecker { 

    public static boolean isRecycled(Document source, boolean printStackTrace) { 
     try { 
      return (source.getUniversalID().length() > 0); 
     } catch (NotesException e) { 
      if (printStackTrace) 
       e.printStackTrace(); 
      return true; 
     } 
    } // isRecycled(Document, boolean) 

    public static boolean isRecycled(Database source, boolean printStackTrace) { 
     try { 
      return (source.getReplicaID().length() > 0); 
     } catch (NotesException e) { 
      if (printStackTrace) 
       e.printStackTrace(); 
      return true; 
     } 
    } // isRecycled(Database, boolean) 


} // NPHchecker 
+0

這是我的意圖。唯一的麻煩是我不知道哪些方法調用被緩存。例如。即使視圖已被回收,NotesView.getUniversalID()或NotesView.getName()也可能會返回緩存的值。 –

+0

我更喜歡這種方式,因爲推送更改爲大公司的java.policy文件非常困難。我還沒有遇到緩存問題。 –

相關問題