import { MongodbCacheAdapter } from "@daiso-tech/core/cache/mongodb-cache-adapter";
import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"
import { MongoClient } from "mongodb";
const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING");
const database = client.db("database");
const serde = new Serde(new SuperJsonSerdeAdapter());
const cacheAdapter = new MongodbCacheAdapter({
database,
serde,
});
// You need initialize the adapter once before using it.
await cacheAdapter.init();
The add method adds a key with given value when key doesn't exists. Returns true when key doesn't exists otherwise false will be returned.
You can provide a ttl value. If null is passed, the item will not expire.
The increment method increments the given key with given value. Returns true if the key where incremented otherwise false will be returned.
If values is not defined then it will increment the key with 1.
An error will thrown if the key is not a number.
To utilize the
MongodbCacheAdapter, you must install the"mongodb"package and supply aISerde<string>, with an adapter likeSuperJsonSerdeAdapter.IMPORT_PATH:
"@daiso-tech/core/cache/mongodb-cache-adapter"