Caching HTML: blog archive list with document counts
The rule: HTML fragments should only be recalculated when there is a change, not every time they are displayed. A good example is the blog archive list:

Before, I generated the list using a @DbColumn. This gave me a list without document counts in plain HTML. This would be visible for search engines. I then used Ajax to add the document counts, as I explained in Adding document counts with Ajax. This was a hack to overcome the limitations of the @DbColumn lookup. Now, I use LotusScript to calculate the HTML and cache it in a document. A simple @formula retrieves the cached data and puts it on the page.
Calculating the HTML
I decided to put all my scripts in a single LotusScript library, so that I can call the same functions from different locations. I can do an automatic rebuild cache each time a blog entry is saved, but also rebuild the cache manually with an Agent. The following function gets the HTML:
Function getArchive
On Error Goto catch
Dim ns As New NotesSession
Dim view As notesView
Dim nav As NotesViewNavigator
Dim entry As NotesViewEntry
Dim out As New StringBuffer(100)
Dim i As Long
Dim href As String,label As String,tmp As String
Dim monthNames As Variant
monthNames=Split("January February March April May June July August September October November December", " ")
Set view=db.GetView("bymonth")
Set nav = view.CreateViewNav
out.add |<div><h4>Archive</h4><ul>|
Set entry=nav.GetFirst
While Not entry Is Nothing
href=dbPath & "bymonth!OpenView&Count=-1&RestrictToCategory="& entry.ColumnValues(0)
tmp=entry.ColumnValues(0)
label=monthNames(Cint(Right(tmp,2))-1) & " " & Left(tmp, 4)
out.add |<li><a href="| & href & |">| & label & |</a> (| & entry.ChildCount & |)</li>|
Set entry=nav.GetNextCategory(entry)
Wend
out.add |<ul></div>|
Goto finally
catch:
Error Err, Error & " in " & Getthreadinfo(1) & ", line " & Erl
Resume finally
finally:
getArchive=out.collapse("")
End Function
I had to calculate the month names on the fly, because the date was stored in 'yyyy-mm' format. The function is called by the rebuildCache function which puts the HTML in a field and saves the cache document:
Function rebuildCache
On Error Goto catch
dbSetup.replaceItemValue "ArchiveHtml", getArchive
dbSetup.save True,True
Goto finally
catch:
Error Err, Error & " in " & Getthreadinfo(1) & ", line " & Erl
Resume finally
finally:
End Function
Retrieving the HTML from cache
The full blog archive HTML block is retrieved on each page with simple @formulas in a computed field:
a:=@GetDocField(DbProfileId; "ArchiveHtml");
@If(@IsError(a); @Text(a)+":"+DbProfile; a)
Finally
When working with cached values, there is always a risk of the cache getting out of sync with the real situation. With Notes, you also have to beware of possible replication conflicts. That said, I think caching is a very powerful technique for performance optimization. My next challenge will be the calculation of the TagCloud in LotusScript and caching it as well.
Comments
20/08/2007 09:22:11, Kevin S Pettitt
Somebody else has a different take on the same thing: http://www.vincedimascio.com/vince/vpd.nsf/all/75EE9728F9671EE88825733C0078A370
20/08/2007 17:23:59, Michel Van der Meiren
Yes: with JavaScript as I also did previously. Nice site www.vincedimascio.com/vince/vpd.nsf/
To add a comment, log in or register as new user. It's free and safe.