Class wwCache

The wwCache class provides a simple caching mechanisms for strings. Caching allows you to generate output once, store it to the cache and then attempt to retrieve it from the cache if it already exists the next time the same content is required. This can save time reproducing output that might be very time consuming and instead simply reusing previously generated output.

Cached items are accessible by key and have an expiration, which allows for timing the availability of cached content to a timeout period that content is valid. If you have a busy site, don't be afraid of short cache durations. It's better to generate once and have an item served hundreds of times in a few minutes, than generate each and every time. The savings are huge and the overhead of re-generating every few minutes is likely minor by comparison.

Caching is powerful for storing previously generated output - complete HTML repsonses, or HTML fragments of a page. XML results or fragments of results. You can even cache binary content like the result of a PDF output generation for example. Anything that can be represented as a string can be cached.

Caching can provide huge performance gains for your applications by avoiding regenerating output and not requiring you to go back to the database to re-run complex queries. Instead results are returned from a single indexed key of the cache cursor or table.

DO wwCache

*** Generally try to make the cache 'global' so it sticks around
goCache = CREATEOBJECT("wwCache")

*** Check if the key exists
lcTime = goCache.GetItem("Time")

*** If it doesn't add a new key
IF ISNULL(lcTime)
	goCache.AddItem("Time", TIME() )
ENDIF

*** Retrieval from cache at some point
? goCache.GetItem("Time")


*** Get a key or add one if it doesn't exist
*** This code effectively does what the code above does in 1 line
lcTime = goCache.GetOrAddItem("Time", Time())
? lcTime   && Existing or new value
Relation
  wwCache

Remarks

The cache is implemented as a cursor, so the contents of the cache is not permanently stored on disk, but stored in temporary files.

Category list items are case sensitive

Class Members

MemberDescription

AddItem

This method adds an item to the cache.

o.AddItem(lcKey as String, lcValue as String, lnTimeoutSeconds as Integer)

Clear

Clears the cache of all values by closing the cache cursor and creating a new one.

o.Clear()

Expire

Removes all expired entries from the cache data file.

o.Expire()

GetItem

Retrieves an item by key from the cache. Item is returned if it exists and not expired.

o.GetItem(lcKey as String)

GetOrAddItem

This method is a combination of GetItem() and AddItem() which retrieves a value if it exists, or sets a value if it doesn't. In either case the value (existing or new) is returned.

o.GetOrAddItem(lcKey, lcNewValue, lnTimeoutSeconds)

IsCached

Determines whether an item exists in the cache or not.

o.IsCached(lcKey as String)

Open

Opens the cache cursor. Either creates it or just uses it if already open.

o.Open()

Reindex

Packs and reindexes the file specified in the cFixedFileName. This method works only if the cFixedFilename property is set and only if an exclusive lock can be set on the table.

o.Reindex()

Remove

This method removes an entry from the Cache.

o.Remove(lcKey)

cCacheCursor

The name of the cursor used for caching.

cFixedFileName

This property allows you to specify a fixed filename for the cache. including the .DBF extension.

nDefaultCacheTimeout

The default timeout for the cache in seconds.

Requirements

Assembly: wwCache.prg

See also:

How wwCache works | When and How to use Caching

© West Wind Technologies, 1996-2024 • Updated: 05/30/24
Comment or report problem with topic