I want to implement basket functionality and store the basket using Redis in my WebApi. In whole project I am using CQRS and MediatR in operations on database. But I don't know how it should be implemented in case of Redis.
Should I implement operations on my basket also same way like: GetBasketByIdRequest, GetBasketByIdResponse, GetBasketByIdHandler, GetBasketByIdCommand, GetBasketByIdQuery, etc.)?
Or just do it apart like IBasketRepository?
I am really curious if creating MediaTr handlers the same way they typically do for a database makes sense, or if creating some service class would be better?
My repository: https://github.com/Lukash88/FlowerShop/tree/basket/FlowerShop
The repo I'm referring to: https://github.com/TryCatchLearn/skinet7/commit/73ecdb7626a36611686fad16c2c5108afb9c7534
Thanks for a help and any advices!
CodePudding user response:
You should treat Redis much like you do any database access.
Redis read/write should "just" surface as an abstract Unit of work or repository pattern.
A quick look at your code and I see your project has an IRepository. Redis should "just" be surfaced to the app as another implementation of IRepository.
The fact that it is in memory and a key value pair rather than rdbms ( or a nosql for that matter ) is irrelevant.
This is your data store and you should abstract away your read/write to any data store in a consistent manner. Mostly once you pick a database then you're quite unlikely to change that. It's a fundamental. Redis is a sort of a cache though and an exception. You might later decide to move to say mongodb as the flower empire grows. Or redis may prove expensive for low order numbers.
There is one caveat though with baskets held in redis.
Abandoned baskets are a thing. You need to track how long a basket was used. You also need to link a basket to an account so you should either use account id or add basket id to the user accounts table if you have one. ( I didn't look at your code in enough depth to check ).
You will want a batch process of some sort removes any baskets which have not been accessed for a month or so. Abandoned baskets will clog up redis eventually if you don't do this. In any case, should a user log back in December they could well be surprised to see that potential valentine present still lingering.