Cache Chilkat2 Perl Reference Documentation
Chilkat2::Cache
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Cache
Store and retrieve cached items by unique key, without the application
needing to manage the underlying cache filenames directly.
Cache strings, byte data, files, or Chilkat buffer objects depending on
the data form used by the application.
Associate cached items with expiration times, update expiration metadata,
and check whether cached content is still usable.
Store and retrieve ETag values for workflows that need HTTP-style cache
validation or conditional re-fetch logic.
Choose the cache root directory and use an optional multi-level directory
structure to avoid too many files in a single folder.
Delete a single cached item, remove expired items, remove older items, or
clear the entire cache when needed.
For an extended overview, see
Cache Class Overview.
Store, retrieve, expire, and clean up cached text or binary data on disk.
Chilkat.Cache is a disk-based cache class for storing and
retrieving text or binary items by unique key. It provides configurable cache
roots, optional multi-level directory layout, expiration metadata, ETag
metadata, cache lookup, fetch-status properties, expiration updates, and
cleanup methods for deleting individual items, expired items, older items, or
the entire cache.
Key-based storage
Text and binary data
Expiration control
ETag metadata
Configurable layout
Cleanup operations
Object Creation
use Chilkat2;
my $cache = Chilkat2::Cache->new;
# or set properties (and RaiseError) at creation:
# my $cache = Chilkat2::Cache->new(VerboseLogging => 1, RaiseError => 1);Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/Cache.pm file holds only this class's documentation (perldoc Chilkat2::Cache).
Perl frees the object when its last reference goes (undef $cache, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::Cache';): an object is a blessed hash reference whose hash is free for the subclass's own fields.
When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.
Errors
A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::Cache'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.
Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.
Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.
Properties
DebugLogFilePath
my $strVal = $cache->DebugLogFilePath;
$cache->DebugLogFilePath($strVal);
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
my $strVal = $cache->LastErrorHtml;
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
my $strVal = $cache->LastErrorText;
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
my $strVal = $cache->LastErrorXml;
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastEtagFetched
my $strVal = $cache->LastEtagFetched;
Returns the ETag metadata associated with the most recently fetched cache item. An empty string means that the fetched item has no stored ETag.
FetchBd, FetchSb, or FetchText.If-None-Match.LastExpirationFetchedStr
my $strVal = $cache->LastExpirationFetchedStr;
Returns the expiration date and time stored for the most recently fetched cache item, formatted as an RFC 822-style Internet date. An empty string represents an item with no expiration date.
LastHitExpired after the fetch to determine whether the returned entry is stale.LastHitExpired
my $boolVal = $cache->LastHitExpired;
Returns 1 when the expiration time of the most recently fetched item is earlier than the current system date and time; otherwise returns 0.
DeleteAllExpired or DeleteFromCache.LastKeyFetched
my $strVal = $cache->LastKeyFetched;
Returns the application-defined key associated with the most recently fetched cache item.
LastMethodSuccess
my $boolVal = $cache->LastMethodSuccess;
$cache->LastMethodSuccess($boolVal);
Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Level
my $intVal = $cache->Level;
$cache->Level($intVal);
Gets or sets the number of directory levels used beneath each cache root. Valid values are 0, 1, and 2.
| Value | Directory layout |
|---|---|
0 | All cache files are stored directly in the cache root. |
1 | Files are distributed among 256 first-level directories named 0 through 255. |
2 | Files are distributed through two levels of 256 directories, allowing as many as 65,536 leaf directories per root. |
NumRoots
my $intVal = $cache->NumRoots;
Returns the number of cache-root directories that have been registered by calling AddRoot.
VerboseLogging
my $boolVal = $cache->VerboseLogging;
$cache->VerboseLogging($boolVal);
If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
my $strVal = $cache->Version;
Methods
AddRoot
$cache->AddRoot($path);
Adds $path as a root directory for the disk cache. Call this method once for each root that should participate in the cache.
For example, a cache distributed across D:\cacheRoot, E:\cacheRoot, and F:\cacheRoot is configured by calling this method three times.
Level setting before accessing a cache created by an earlier process or application run.Returns the object, so calls can be chained.
DeleteAll
my $n = $cache->DeleteAll();
Deletes every cache item from all configured roots and returns the number of cache files removed. Multi-level subdirectories are retained.
DeleteAllExpired
my $n = $cache->DeleteAllExpired();
Deletes all cache items whose stored expiration time is earlier than the current system time and returns the number of files removed.
DeleteFromCache
# $success is true or false
my $success = $cache->DeleteFromCache($key);
Deletes the cache item identified by $key.
| Return value | Meaning |
|---|---|
1 | The item was deleted, or no item existed for the key. |
0 | The item exists but could not be deleted, for example because of a filesystem permission or sharing error. |
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
DeleteOlderDt
Deletes cache items older than the cutoff date and time supplied in $dt.
Returns the number of cache files deleted, or -1 if an error occurs.
DeleteAllExpired when the decision should be based specifically on each item's expiration metadata.DeleteOlderStr
# $n is an integer
my $n = $cache->DeleteOlderStr($dateTimeStr);
Deletes cache items older than the cutoff date and time supplied in $dateTimeStr as an RFC 822-style Internet date string.
Returns the number of cache files deleted, or -1 if an error occurs.
Mon, 03 Aug 2026 12:00:00 GMT. Use DeleteOlderDt when the cutoff is already available as a CkDateTime.FetchBd
# $bd is a Chilkat2::BinData
# $success is true or false
my $success = $cache->FetchBd($key, $bd);
Retrieves the binary item identified by $key and loads its bytes into the BinData object supplied in $bd.
The key is application-defined, may contain any characters, and must uniquely identify the intended item.
1. A failed lookup or filesystem error returns 0.LastHitExpired immediately afterward when stale data must not be used.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
FetchSb
# $sb is a Chilkat2::StringBuilder
# $success is true or false
my $success = $cache->FetchSb($key, $sb);
Retrieves the text item identified by $key and loads its content into the StringBuilder supplied in $sb.
The key is application-defined, may contain any characters, and must uniquely identify the intended item.
1. A failed lookup or filesystem error returns 0.LastHitExpired immediately afterward when stale data must not be used.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
FetchText
# $str is a string or undef
my $str = $cache->FetchText($key);
Retrieves the text item identified by $key and returns its string content.
The key is application-defined, may contain any characters, and must uniquely identify the intended item.
LastHitExpired immediately afterward when the application requires only fresh data.Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
GetEtag
# $str is a string or undef
my $str = $cache->GetEtag($key);
Returns the ETag metadata stored with the cache item identified by $key.
Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
GetExpirationStr
# $str is a string or undef
my $str = $cache->GetExpirationStr($url);
Returns the expiration date and time stored for the item identified by $url, formatted as an RFC 822-style Internet date string.
$url url for historical reasons. It is the cache key and does not need to be a URL.Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
GetFilename
# $str is a string or undef
my $str = $cache->GetFilename($key);
Returns the absolute filesystem path of the cache file associated with $key.
Cache API rather than modifying cache files directly.Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
GetRoot
# $str is a string or undef
my $str = $cache->GetRoot($index);
Returns the directory path of the cache root at zero-based $index. Valid indexes range from 0 through NumRoots - 1.
Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
IsCached
# $flag is true or false
my $flag = $cache->IsCached($key);
Returns 1 if an item exists for $key, or 0 if the key is not present.
LastHitExpired, or read its expiration metadata, when freshness matters.Never dies: false is an answer, not a failure.
topSaveBd
# $expiration is a string
# $etag is a string
# $bd is a Chilkat2::BinData
# $success is true or false
my $success = $cache->SaveBd($key, $expiration, $etag, $bd);
Stores the contents of $bd as binary cache data under $key. If the key already exists, its data and metadata are replaced.
| Argument | Purpose |
|---|---|
$key | The unique application-defined cache key. |
$expiration | Expiration as an RFC 822-style Internet date string. Pass an empty string for no expiration. |
$etag | Optional ETag or application metadata. Pass an empty string when unused. |
$bd | The BinData containing the bytes to save. |
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SaveTextDt
# $expireDateTime is a Chilkat2::DateTime
# $eTag is a string
# $itemTextData is a string
# $success is true or false
my $success = $cache->SaveTextDt($key, $expireDateTime, $eTag, $itemTextData);
Stores $itemTextData as a text cache item under $key, with the expiration date and time supplied in $expireDateTime. If the key already exists, its data and metadata are replaced.
$eTag is optional ETag or application metadata and may be an empty string. The key may contain any characters and may be any length, but it must uniquely identify the item.
LastHitExpired to detect staleness and a deletion method to remove it.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SaveTextNoExpire
# $eTag is a string
# $itemTextData is a string
# $success is true or false
my $success = $cache->SaveTextNoExpire($key, $eTag, $itemTextData);
Stores $itemTextData as a non-expiring text cache item under $key. If the key already exists, its data and metadata are replaced.
$eTag is optional ETag or application metadata and may be an empty string.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SaveTextStr
# $expireDateTime is a string
# $eTag is a string
# $itemTextData is a string
# $success is true or false
my $success = $cache->SaveTextStr($key, $expireDateTime, $eTag, $itemTextData);
Stores $itemTextData as a text cache item under $key, with $expireDateTime providing the expiration time as an RFC 822-style Internet date string. If the key already exists, its data and metadata are replaced.
$eTag is optional ETag or application metadata and may be empty.
SaveTextDt when the expiration is available as a CkDateTime, or SaveTextNoExpire for an entry without expiration.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UpdateExpirationDt
# $expireDateTime is a Chilkat2::DateTime
# $success is true or false
my $success = $cache->UpdateExpirationDt($key, $expireDateTime);
Replaces the expiration date and time of the cache item identified by $key with the CkDateTime value supplied in $expireDateTime. The cached data and ETag are unchanged.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UpdateExpirationStr
# $expireDateTime is a string
# $success is true or false
my $success = $cache->UpdateExpirationStr($key, $expireDateTime);
Replaces the expiration date and time of the cache item identified by $key with the RFC 822-style Internet date string supplied in $expireDateTime. The cached data and ETag are unchanged.
$key url; it is the cache key and does not need to be a URL. Use UpdateExpirationDt for a CkDateTime value.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.