The Conroy & Conroy Subroutine Library (2021.3 edition)

TSHeap_Cache
What: class
Ancestor: TSCache
File: Cache
Layer: 1
Platforms: C++, C#, Javascript, Pascal (OOP), PHP, Python

This class is used to cache data in memory (in the heap). Limits can be set to how much data is cached. It uses a least-recently-used approach to removing data from the cache to make room for new data. Each cached item consists of an "address" (a unique integer ID) and a size. Writing data to the cache will overwrite the existing cache ID, if it exists or has a different size. Otherwise it will add a new item to the cache. IDs are specific to a given cache instance, so the same ID can be used in different instances of TSHeap_Cache and they will refer to different items. Overhead for each cache item is 5 times the native word size of the platform (20 bytes on a 32-bit platform). The class contains the following methods:

Clear
void Clear()
procedure Clear
Clears the cache of all cache entries.

Get_Max_Size
int32 Get_Max_Size()
function Get_Max_Size : longint
Gets current maximum cache size in bytes.

Get_Size
int32 Get_Size()
function Get_Size : longint
Returns amount of currently cached data, in bytes.

Get_Stats
int32 Get_Stats( int32 Index, int32 &Address )
function Get_Stats( Index : longint ; var Address : longint ) : longint
Returns the size of the data at cached index Index and sets Address of the address of the cached data.
Note: In Python, the second parameter is ignored.

Initialize
void Initialize()
procedure Initialize
(Re)Initializes the cache.

Lock
bool Lock( int32 Address )
function Lock( Address : longint ) : boolean
Locks the data starting at the specified address into memory so that it is not thrown away. Returns false if the specified data is not in the cache. NOTE: This is not yet implemented.

Read_Cache
int32 Read_Cache( void* Data, int32 Address, int32 Size )
function Read_Cache( var Data ; Address, Size : longint ) : longint
Reads up to Size bytes from the cache for the specified address. Returns the number of bytes actually read (if 0 then the data was not cached). Cached data is copied to Data.
Note: In PHP, this method returns the data itself (or null if the data was not found in the cache). The first parameter is ignored and any value can be passed in its place.
Note: In Python, this method returns the data itself (or None if the data was not found in the cache). The first parameter is ignored and any value can be passed in its place.

Set_Max_Size
void Set_Max_Size( int32 Size )
procedure Set_Max_Size( Size : longint )
Sets maximum cache size in bytes. If this is more than is currently cached, some cache is thrown away.

Terminate
void Terminate()
procedure Terminate
Terminates the object. Any data previously in the cache is lost.

Unlock
bool Unlock( int32 Address )
function Unlock( Address : longint ) : boolean
Unlocks the data starting at the specified address in memory. Returns false if the specified data is not in the cache, or if it is not locked. NOTE: This is not yet implemented.

Write_Cache
void Write_Cache( const void* Data, int32 Address, int32 Size )
procedure Write_Cache( const Data ; Address, Size : longint )
Writes to cache up to Size bytes from Data.