import { KyselyCacheAdapter } from "@daiso-tech/core/cache/kysely-cache-adapter";
import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"
import SQLite from 'better-sqlite3'
import { Kysely, SqliteDialect } from 'kysely'
const serde = new Serde(new SuperJsonSerdeAdapter());
const cacheAdapter = new KyselyCacheAdapter({
kysely: new Kysely({
dialect: new SqliteDialect({
database: new Sqlite("local.db"),
}),
}),
serde,
});
// You need initialize the adapter once before using it.
await cacheAdapter.init();
Creates a new cache entry, but only if the key does not already exist. Has no effect if the key already exists.
Cache key to add
Value to cache
Time-to-live duration for this entry. Pass null to cache without expiration.
true if the entry was created, false if the key already existed
Retrieves a value by key without removing it.
Cache key to retrieve
The cached value, or null if not found or expired
Retrieves a value by key and immediately removes it. Useful for one-time-use values (tokens, temporary states, etc.).
Cache key to retrieve and remove
The cached value, or null if not found or expired
The getOrAdd method retrieves the value for the given key if it exists,
otherwise adds the valueToAdd to the cache and returns it.
The cache key to retrieve or add.
The value to store if the key is not found.
Optional time-to-live for the cached item. If null is passed, the item will not expire.
The cached value if the key exists, or the newly added value.
Increments a numeric cache entry by a given amount. Useful for counters, rates, and statistics.
Cache key to increment
Amount to increment by.
true if the entry was incremented, false if the key did not exist
Creates a new cache entry or updates an existing one (upsert). Also updates the TTL when overwriting an existing entry.
Cache key to set
Value to cache
Time-to-live duration for this entry. Pass null to cache without expiration.
true if the entry was added, false if it was updated
Removes all cache entries. Use with caution as this completely clears the cache.
Removes all cache entries whose keys start with a given prefix. Useful for invalidating groups of related cache entries.
Key prefix to match for removal
Removes multiple cache entries at once.
Array of cache keys to remove
true if at least one key was removed, false if none existed
Updates an existing cache entry without changing its TTL. Has no effect if the key does not exist.
Cache key to update
New value to cache
true if the entry was updated, false if the key did not exist
To utilize the
KyselyCacheAdapter, you must install the"kysely"package and configure aKyselyclass instance. The adapter have been tested withsqlite,postgresandmysqldatabases.IMPORT_PATH:
"@daiso-tech/core/cache/kysely-cache-adapter"