adobrovolsky97 / laravel-repository-service-pattern

Laravel Repository-Service Pattern
27 stars 5 forks source link

Service layer vs repository (cached) #1

Closed deevus closed 9 months ago

deevus commented 11 months ago

Hi there

This package has been a great quickstart on service/repositories in a new project. Thanks!

I'm concerned that the caching strategy out of the box is a bit half baked. Perhaps you have already solved such a problem before and I'm just not seeing the obvious solution. It's also possible that I should skip caching for this problem, since it is in userland and not global.

Let's say I have a Cart model, with associated CartService and CartRepository classes. If I want to add a LineItem to the Cart, I want to flush the cache so that the new line items are fetched on the next request.

In domain driven design, the method addToCart would go on the CartService class. However, this doesn't have access to cache flushing on the CartRepository.

Is the cached repository only meant for global objects (e.g. Products) and as such is a blunt instrument?

Appreciate your thoughts and thanks again for the package.

adobrovolsky97 commented 11 months ago

Hi! I did not really have a chance to test this cacheable repository on practice Do you use database to store the cart products ? As for cache flushing - it flushes after create/update/delete operations called from the service (it will trigger the repo method, and flush the cache inside repo function, see the screenshot) Also it`s a good point to make flush cache as a public function

image
deevus commented 11 months ago

I agree that the cache flushing methods should be public.

There should also be a way to propagate cache flushes across repositories.

adobrovolsky97 commented 11 months ago

@deevus I made cache flush function as public. Now you can access it within your repository or service