Unit implementing cache class

The CacheCls unit implements a caching class: similar to a hash class, it can be used to cache data, associated with string values (keys). The class is calles TCache

Exception support Message shown when an invalid index is passed. Exception class used in the cachecls unit. Pointer to TCacheSlot record. Record representing 1 item in the cache list. TCacheSlot is internally used by the class. It represents 1 element in the linked list. Pointer to previous element in the list. Pointer to next element in the list. Data pointer for this item. Index in the list. Pointer to TCacheSlotArray array Array of TCacheSlot TCacheSlotArray is an array of TCacheSlot items. Do not use TCacheSlotArray directly, instead, use and allocate memory dynamically. Callback type to check whether 2 data types are equal.

TOnIsDataEqual is a callback prototype; It is used by the call to determine whether the item to be added is a new item or not. The function returns True if the 2 data pointers AData1 and AData2 should be considered equal, or False when they are not.

For most purposes, comparing the pointers will be enough, but if the pointers are ansistrings, then the contents should be compared.

True if AData1 and AData2 are equal. TCache instance which calls the callback. First data element pointer. Second data element pointer. Callback called when a slot must be freed. TOnFreeSlot is a callback prototype used when not enough slots are free, and a slot must be freed. Cache class calling the function Index of the slot that will be freed. Cache class

TCache implements a cache class: it is a list-like class, but which uses a counting mechanism, and keeps a Most-Recent-Used list; this list represents the 'cache'. The list is internally kept as a doubly-linked list.

The Data property offers indexed access to the array of items. When accessing the array through this property, the MRUSlot property is updated.

Create a new cache class. Create instantiates a new instance of TCache. It allocates room for ASlotCount entries in the list. The number of slots can be increased later. Initial slot count Free the TCache class from memory Destroy cleans up the array for the elements, and calls the inherited Destroy. The elements in the array are not freed by this action. Add a data element to the list.

Add checks whether AData is already in the list. If so, the item is added to the top of the MRU list. If the item is not yet in the list, then the item is added to the list and placed at the top of the MRU list using the AddNew call.

The function returns the index at which the item was added.

If the maximum number of slots is reached, and a new item is being added, the least used item is dropped from the list.

Index at which item was added. Data pointer to add to the cache. Add a new item to the list.

AddNew adds a new item to the list: in difference with the Add call, no checking is performed to see whether the item is already in the list.

The function returns the index at which the item was added.

If the maximum number of slots is reached, and a new item is being added, the least used item is dropped from the list.

Position at which item is added to the list Data pointer to add to the list Find data pointer in the list

FindSlot checks all items in the list, and returns the slot which contains a data pointer that matches the pointer AData.

If no item with data pointer that matches AData is found, Nil is returned.

For this function to work correctly, the OnIsDataEqual event must be set.

If OnIsDataEqual is not set, an exception wil be raised.
Slot which contains AData Data pointer to look for. Return index of a data pointer in the list.

IndexOF searches in the list for a slot with data pointer that matches AData and returns the index of the slot.

If no item with data pointer that matches AData is found, -1 is returned.

For this function to work correctly, the OnIsDataEqual event must be set.

If OnIsDataEqual is not set, an exception wil be raised.
Index of item that matches AData Data pointer to search for. Remove a data item from the list. Remove searches the slot which matches AData and if it is found, sets the data pointer to Nil, thus effectively removing the pointer from the list. None. Data pointer to remove Indexed access to data items

Data offers index-based access to the data pointers in the cache. By accessing an item in the list in this manner, the item is moved to the front of the MRU list, i.e. MRUSlot will point to the accessed item. The access is both read and write.

The index is zero-based and can maximally be SlotCount-1. Providing an invalid index will result in an exception.

Index of slot in the list Most recent item slot. MRUSlot points to the most recent used slot. The most recent used slot is updated when the list is accessed through the Data property, or when an item is added to the list with Add or AddNew Last used item LRUSlot points to the least recent used slot. It is the last item in the chain of slots. Number of slots in the list SlotCount is the number of slots in the list. Its initial value is set when the TCache instance is created, but this can be changed at any time. If items are added to the list and the list is full, then the number of slots is not increased, but the least used item is dropped from the list. In that case OnFreeSlot is called. Indexed array to the slots

Slots provides index-based access to the TCacheSlot records in the list. Accessing the records directly does not change their position in the MRU list.

The index is zero-based and can maximally be SlotCount-1. Providing an invalid index will result in an exception.

Index of slot Event to compare 2 items. OnIsDataEqual is used by FindSlot and IndexOf to compare items when looking for a particular item. These functions are called by the Add method. Failing to set this event will result in an exception. The function should return True if the 2 data pointers should be considered equal. Event called when a slot is freed

OnFreeSlot is called when an item needs to be freed, i.e. when a new item is added to a full list, and the least recent used item needs to be dropped from the list.

The cache class instance and the index of the item to be removed are passed to the callback.