Common/DocumentResultList: Add caching

This commit is contained in:
Jonathan Jenne 2022-02-23 14:29:14 +01:00
parent f86ac21ecd
commit b2509dce65
3 changed files with 51 additions and 14 deletions

View File

@ -1,8 +1,11 @@
Imports DigitalData.Modules.EDMI.API.Client Imports DigitalData.Modules.Base
Imports DigitalData.Modules.EDMI.API.Client
Imports DigitalData.Modules.Logging
Namespace DocumentResultList Namespace DocumentResultList
Public Class Cache Public Class Cache
Inherits BaseClass
Implements ICollection(Of Document) Implements ICollection(Of Document)
Private Const _DefaultCapacity As Long = 1000 Private Const _DefaultCapacity As Long = 1000
@ -17,16 +20,20 @@ Namespace DocumentResultList
Private ReadOnly Index As New Dictionary(Of String, LinkedListNode(Of Document)) Private ReadOnly Index As New Dictionary(Of String, LinkedListNode(Of Document))
Private ReadOnly Lock As New Object Private ReadOnly Lock As New Object
Public Sub New() Public Sub New(pLogConfig As LogConfig)
Me.New(_DefaultCapacity) Me.New(pLogConfig, _DefaultCapacity)
End Sub End Sub
Public Sub New(capacity As Long) Public Sub New(pLogConfig As LogConfig, pCapacity As Long)
If capacity < 0 Then MyBase.New(pLogConfig)
If pCapacity < 0 Then
Throw New InvalidOperationException("DocumentResultCache capacity must be positive.") Throw New InvalidOperationException("DocumentResultCache capacity must be positive.")
End If End If
Me.Capacity = capacity Logger.Debug("Initializing DocumentResultCache with capacity of [{0}] bytes.", pCapacity)
Me.Capacity = pCapacity
End Sub End Sub
Public Event DiscardingOldestItem As EventHandler Public Event DiscardingOldestItem As EventHandler
@ -40,6 +47,8 @@ Namespace DocumentResultList
End Property End Property
Public Sub Add(pItem As Document) Implements ICollection(Of Document).Add Public Sub Add(pItem As Document) Implements ICollection(Of Document).Add
Logger.Debug("Adding document [{0}].", pItem.Id)
SyncLock Lock SyncLock Lock
If Index.ContainsKey(pItem.Id) Then If Index.ContainsKey(pItem.Id) Then
List.Remove(Index(pItem.Id)) List.Remove(Index(pItem.Id))
@ -64,6 +73,10 @@ Namespace DocumentResultList
Return Index.ContainsKey(pItem.Id) Return Index.ContainsKey(pItem.Id)
End Function End Function
Public Function Contains(pDocumentId As Long) As Boolean
Return Index.ContainsKey(pDocumentId)
End Function
Public Sub CopyTo(pArray As Document(), pIndex As Integer) Implements ICollection(Of Document).CopyTo Public Sub CopyTo(pArray As Document(), pIndex As Integer) Implements ICollection(Of Document).CopyTo
SyncLock Lock SyncLock Lock
For Each item As Document In Me For Each item As Document In Me
@ -86,6 +99,8 @@ Namespace DocumentResultList
End Property End Property
Public Function Remove(pItem As Document) As Boolean Implements ICollection(Of Document).Remove Public Function Remove(pItem As Document) As Boolean Implements ICollection(Of Document).Remove
Logger.Debug("Removing document [{0}].", pItem.Id)
SyncLock Lock SyncLock Lock
If Index.ContainsKey(pItem.Id) Then If Index.ContainsKey(pItem.Id) Then

View File

@ -6,6 +6,7 @@ Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Base.IDB Imports DigitalData.Modules.Base.IDB
Imports DigitalData.Modules.ZooFlow.Constants Imports DigitalData.Modules.ZooFlow.Constants
Imports DigitalData.Modules.Base Imports DigitalData.Modules.Base
Imports DigitalData.Modules.ZooFlow
Namespace DocumentResultList Namespace DocumentResultList
@ -14,29 +15,50 @@ Namespace DocumentResultList
Private ReadOnly Client As Client Private ReadOnly Client As Client
Private ReadOnly Mode As OperationMode Private ReadOnly Mode As OperationMode
Private ReadOnly User As DigitalData.Modules.ZooFlow.State.UserState Private ReadOnly User As State.UserState
Public Sub New(pLogConfig As LogConfig, pMode As OperationMode, pClient As Client, pUser As DigitalData.Modules.ZooFlow.State.UserState) Private ReadOnly Cache As Cache
Public Sub New(pLogConfig As LogConfig, pMode As OperationMode, pClient As Client, pUser As State.UserState)
MyBase.New(pLogConfig) MyBase.New(pLogConfig)
Client = pClient Client = pClient
Mode = pMode Mode = pMode
User = pUser User = pUser
' Set up cache with capacity of 100 MB
Cache = New Cache(104_900_000)
End Sub End Sub
Public Function Load(pObjectId As Long, pFullPath As String) As Document Public Sub Invalidate(pDocument As Document)
Cache.Remove(pDocument)
End Sub
Public Function Load(pObjectId As Long, pFullPath As String, Optional pForce As Boolean = False) As Document
Dim oDocument As Document = Nothing
If Cache.Contains(pObjectId) And pForce = False Then
Return Cache.Where(Function(doc) doc.Id = pObjectId).Single()
End If
Select Case Mode Select Case Mode
Case OperationMode.NoAppServer Case OperationMode.NoAppServer
Return Load_FromWindream(pObjectId, pFullPath) oDocument = Load_FromWindream(pObjectId, pFullPath)
Case OperationMode.WithAppServer Case OperationMode.WithAppServer
Return Load_FromIDB(pObjectId) oDocument = Load_FromIDB(pObjectId)
Case OperationMode.ZooFlow Case OperationMode.ZooFlow
Return Load_FromZooflow(pObjectId) oDocument = Load_FromZooflow(pObjectId)
Case Else Case Else
Return Nothing oDocument = Nothing
End Select End Select
If oDocument IsNot Nothing Then
Cache.Add(oDocument)
End If
Return oDocument
End Function End Function
Private Function Load_FromWindream(pObjectId As Long, pFullPath As String) As Document Private Function Load_FromWindream(pObjectId As Long, pFullPath As String) As Document

View File

@ -50,7 +50,6 @@ Public Class frmDocumentResultList
Private ReadOnly Filesystem As Modules.Filesystem.File Private ReadOnly Filesystem As Modules.Filesystem.File
Private ReadOnly GridBuilder As GridBuilder Private ReadOnly GridBuilder As GridBuilder
Private ReadOnly FileEx As Modules.Windows.File Private ReadOnly FileEx As Modules.Windows.File
Private ReadOnly Cache As New DocumentResultList.Cache(50000000)
Private ReadOnly Helpers As DocumentResultList.Helpers Private ReadOnly Helpers As DocumentResultList.Helpers
Private ReadOnly Params As DocumentResultList.Params Private ReadOnly Params As DocumentResultList.Params
@ -322,6 +321,7 @@ Public Class frmDocumentResultList
.Language = Environment.User.Language, .Language = Environment.User.Language,
.Username = Environment.User.UserName .Username = Environment.User.UserName
}) })
Documentloader.Invalidate(pFile.Document)
If IsNothing(oObjectId) Then If IsNothing(oObjectId) Then
MsgBox($"Beim Speichern der Datei '{oDisplayName}' Fehler ist ein Fehler aufgetreten!", MsgBoxStyle.Critical, Text) MsgBox($"Beim Speichern der Datei '{oDisplayName}' Fehler ist ein Fehler aufgetreten!", MsgBoxStyle.Critical, Text)