const cache = new Client({
mongoURI: "mongodb://localhost:27017/dog-test",
});
cache.on("ready", (v) => console.log("Cache is ready."));
cache.on("debug", console.log);
const DogSchema = new Schema(
{
_id: String,
name: String,
},
);
const Dog = model("Dog", DogSchema);
cache.model(Dog);
cache.init();
cache.set("Dog", "dog-1", "isBarking", true);
Adding a model to the cache enables it to use it to save, update, and retrieve documents from MongoDB.
const { Schema, model } = require("mongoose");
const DogSchema = new Schema(
{
_id: String,
name: String,
isBarking: Boolean,
},
);
const CatSchema = new Schema(
{
_id: String,
name: String,
isMeowing: Boolean,
},
);
const FishSchema = new Schema(
{
_id: String,
name: String,
isBeingEatenByCat: Boolean,
},
);
const Dog = model("Dog", DogSchema),
Cat = model("Cat", CatSchema),
Fish = model("Fish", FishSchema);
cache.model(Dog, Cat, Fish);
-
get
- Retrieve a value from the cache/databasecache.get(modelName: string, key: string, field: string): Promise<any>
-
getAll
- Get all fields under a key in object formcache.getAll(modelName: string, key: string): Promise<object>
-
set
- Set a value to the cachecache.set(modelName: string, key: string, field: string, value: any): Promise<boolean>
-
init
- Initialises the cache, connecting it to both Redis and MongoDB -
model
- Adds a Mongoose model to the cache for it to use for method validation/access to Mongocache.model(models: ...Mongoose.Model<any>[])
get
cache.get("Dog", "1234", "name")
// Example Response: "Sir Woofalot The 3rd"
getAll
cache.getAll("Dog", "1234")
/* Example Response: {
name: "Sir Woofalot The 3rd",
isBarking: false
} */
set
cache.set("Dog", "1234", "isBarking", true);