MongoDB & Redis C#/.NET
A program that can insert a record into MongoDB and Redis at the same time.
I document here the implementation of the program and how everything is beeing added into both data base’s.
Insert A record
Inside MongoDB
Inside Redis
The Class’s
● MongoDBCRUD
● PersonModel
● RedisOperations
I will be calling everyting in one method called DoSome() becasue its roughly easier.
I first create a string called conenctionString which is equal to the redis connection string, Cratea a ConnectionMultiplexer instance called redis , using the Connect function and it should be equal to the connection string. Create Idatabase instance called dbredis which is getting the database. And now for me to get the Mongo data base I create an MongoDBCRUD instance called db which will implement the “table” name(from ctor) which is the database name in the case “Address_Book”.
I also created a Random instance for the user id.
I create an instance from PersonModel called p and set the information.the Id is a [BsonId] which is declared inside the PersonModel class and it will be equal to the randomator variable that i made.so the random number should be the id number of the new inserted record and should be dispalyed on both MongoDB and Redis.
I insert the record by calling the InserRecord function which is used by the db instance I created from MongoDBCRUD, and the “table” name in this case is users (collection name) and the value which is the instance p where i setted all the information.
I insert the record into Redis by calling StringSet function whic is used by dbredis instance that I created from Idabase interface which is getting the redis db.
So the key is to the collection name “users” adding before the recordKey(randomator_) so I could find easily the key by the id number, and I insert the records of p instance.
(Full Function Page)
Screenshot