import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
import Sqlite from "better-sqlite3";
import { Cache } from "@daiso-tech/core/cache";
import { KeyPrefixer } from "@daiso-tech/core/utilities";
const database = new Sqlite("local.db");
const serde = new Serde(new SuperJsonSerdeAdapter());
const cacheAdapter = new SqliteCacheAdapter({
database,
serde,
});
// You need initialize the adapter once before using it.
await cacheAdapter.init();
const cache = new Cache({
keyPrefixer: new KeyPrefixer("cache"),
adapter: cacheAdapter,
});
The add
method adds a key
with given value
when key doesn't exists.
true when key doesn't exists otherwise false will be returned.
You can listen to the following CacheEvents
of the ICache
instance.
To understand how this method works, refer to IEventListenable
.
You can listen to the following CacheEvents
of the ICache
instance.
To understand how this method works, refer to IEventListenable
.
The clear
method removes all the keys in the cache. If a cache is in a group then only the keys part of the group will be removed.
The decrement
method decrements the given key
with given value
.
An error will thrown if the key is not a number.
true if the key
where decremented otherwise false will be returned.
The exists
method returns true when key
is found otherwise false will be returned.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
The get
method returns the value when key
is found otherwise null will be returned.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
The getAndRemove
method returns the value when key
is found otherwise null will be returned.
The key will be removed after it is returned.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
The getOr
method will retrieve the given key
if found otherwise defaultValue
will be returned.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
can be regular value, sync or async Invokable
value and LazyPromise
value.
The getOrAdd
method will retrieve the given key
if found otherwise valueToAdd
will be added and returned.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
can be regular value, sync or async Invokable
value and LazyPromise
value.
Optional
ttl: null | TimeSpanThe getOrFail
method returns the value when key
is found otherwise an error will be thrown.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
The increment
method increments the given key
with given value
.
An error will thrown if the key is not a number.
true if the key
where incremented otherwise false will be returned.
You can listen to the following CacheEvents
of the ICache
instance.
To understand how this method works, refer to IEventListenable
.
The missing
method returns true when key
is not found otherwise false will be returned.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
The put
method replaces th given key
with the given value
and ttl
if the key
exists
othwerwise it will add the given value
with the given ttl
.
true if the key
where replaced otherwise false is returned.
The remove
method removes the given key
.
can be a string or an Iterable
of strings.
If it's an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
true if the key is found otherwise false is returned.
You can listen to the following CacheEvents
of the ICache
instance.
To understand how this method works, refer to IEventListenable
.
The removeMany
method removes many keys.
The param items can be a string or an Iterable
of strings.
If the param items are an Iterable
, it will be joined into a single string.
Think of an Iterable
as representing a path.
true if one of the keys where deleted otherwise false is returned.
You can listen to the following CacheEvents
of the ICache
instance.
To understand how this method works, refer to IEventListenable
.
You can listen to the following CacheEvents
of the ICache
instance.
To understand how this method works, refer to IEventListenable
.
The update
method updates the given key
with given value
.
true if the key
where updated otherwise false will be returned.
IMPORT_PATH:
"@daiso-tech/core/cache"