Cache
[!TIP]
Location within the framework
Hive-agent-framework/cache.
Caching is a process used to temporarily store copies of data or computations in a cache (a storage location) to facilitate faster access upon future requests. The primary purpose of caching is to improve the efficiency and performance of systems by reducing the need to repeatedly fetch or compute the same data from a slower or more resource-intensive source.
Usage
Capabilities showcase
import { UnconstrainedCache } from "Hive-agent-framework/cache/unconstrainedCache";
const cache = new UnconstrainedCache();
// Save
await cache.set("a", 1);
await cache.set("b", 2);
// Read
const result = await cache.get("a");
console.log(result); // 1
// Meta
console.log(cache.enabled); // true
console.log(await cache.has("a")); // true
console.log(await cache.has("b")); // true
console.log(await cache.has("c")); // false
console.log(await cache.size()); // 2
// Delete
await cache.delete("a");
console.log(await cache.has("a")); // false
// Clear
await cache.clear();
console.log(await cache.size()); // 0Source: examples/cache/unconstrainedCache.ts
Caching function output + intermediate steps
Source: examples/cache/unconstrainedCacheFunction.ts
Usage with tools
Source: examples/cache/toolCache.ts
[!IMPORTANT]
Cache key is created by serializing function parameters (the order of keys in the object does not matter).
Usage with LLMs
Source: examples/cache/llmCache.ts
[!TIP]
Caching for non-chat LLMs works exactly the same way.
Cache types
The framework provides multiple out-of-the-box cache implementations.
UnconstrainedCache
Unlimited in size.
SlidingCache
Keeps last k entries in the memory. The oldest ones are deleted.
Source: examples/cache/slidingCache.ts
FileCache
One may want to persist data to a file so that the data can be later loaded. In that case the FileCache is ideal candidate. You have to provide a location where the cache is persisted.
Source: examples/cache/fileCache.ts
[!NOTE]
Provided location (
fullPath) doesn't have to exist. It gets automatically created when needed.
[!NOTE]
Every modification to the cache (adding, deleting, clearing) immediately updates the target file.
Using a custom provider
Source: examples/cache/fileCacheCustomProvider.ts
NullCache
The special type of cache is NullCache which implements the BaseCache interface but does nothing.
The reason for implementing is to enable Null object pattern.
@Cache (decorator cache)
Source: examples/cache/decoratorCache.ts
Complex example
Source: examples/cache/decoratorCacheComplex.ts
[!NOTE]
Default
cacheKeyfunction isObjectHashKeyFn
[!CAUTION]
Calling an annotated method with the
@Cachedecorator with different parameters (despite the fact you are not using them) yields in cache bypass (different arguments = different cache key) generated. Be aware of that. If you want your method always to return the same response, useSingletonCacheKeyFn.
CacheFn
Because previously mentioned CacheDecorator can be applied only to class methods/getter the framework provides a way how to do caching on a function level.
Source: examples/cache/cacheFn.ts
[!NOTE]
Internally, the function is wrapped as a class; therefore, the same rules apply here as if it were a method annotated with the
@Cachedecorator.
Creating a custom cache provider
To create your cache implementation, you must implement the BaseCache class.
Source: examples/cache/custom.ts
The simplest implementation is UnconstrainedCache, which can be found here.