MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.34k stars 457 forks source link

Asp.Net Core DI failure on CacheManager exception #218

Closed avireddy02 closed 6 years ago

avireddy02 commented 6 years ago

I'm using CacheManger with Redis Backplane and in-memory. Trying to build an application that should work even on redis/caching layer failures (may be degraded performance backed by sql db).

CacheManager is registered as part of Asp.Net Core DI. When ICacheManager is requested and redis connection throws exception then an my api is not even executing.

Referring AspnetCore.WebApp

When Redis connection fails ValuesController will never be invoked.

Is there a way to still use DI and get exception only on cacheManager crud operation? Open for suggestions

MichaCo commented 6 years ago

In DI, CacheManager gets initialized the moment it is first used. This happens usually the first time it gets injected to a controller or something else. So, your website totally starts normally and hits the controller, but the constructor injection then fails with a connection exception... This means, your controller doesn't get initialized and the request will fail before it hits any controller method. Your app though is still running normally...

This should be fine for the most part as it will work normally again when the app can connect to Redis again.

If that behavior is not what you want, don't use ctor injection or Redis or build 2 controllers, one with and one without cache as fallback for example? Just ideas... there are many options, depends on what you need... ;)

avireddy02 commented 6 years ago

thanks @MichaCo I actually found your blog which is very resourceful. WhatIfRedisStopsWorkingHowDoIkeepMyAppRunning

Using the above blog I have wrapped the CacheManagers Registration with try catch to solve DI issue when redis connectivity exceptions was thrown.

Suggestion: It would be great if this Link is explicitly specified on CacheManager Docs

MichaCo commented 6 years ago

Hey @avireddy02 great to hear you found that useful. The links is on of the first things on the http://cachemanager.michaco.net/ homepage though. I thought that's enough ;)