filipw / Strathweb.CacheOutput

ASP.NET Web API CacheOutput - library to allow you to cache the output of ApiControllers
Other
883 stars 254 forks source link

Custom CacheKeyGenerator not being used in some cases #243

Open lilmidnit opened 6 years ago

lilmidnit commented 6 years ago

I have created a customer CacheKeyGenerator which inherits from the default and overrides MakeBaseKey. It is working fine running it local no matter how I reference it (outputcache attribute or registering it) but when it is deployed to the server it uses the DefaultCacheKeyGenerator.

I am also using a custom cacheoutputprovider and this does work both locally and on the server.

[CacheOutput(ClientTimeSpan = 1, ServerTimeSpan = 120, CacheKeyGenerator = typeof(BearerControllerCacheKeyGenerator))] and I have tried registering it in global.asax config.CacheOutputConfiguration().RegisterCacheKeyGeneratorProvider(() => new BearerControllerCacheKeyGenerator()); and config.CacheOutputConfiguration().RegisterDefaultCacheKeyGeneratorProvider(() => new BearerControllerCacheKeyGenerator()); and I also use Ninject so I added it in there as well kernel.Bind<ICacheKeyGenerator>().To<BearerControllerCacheKeyGenerator>();

I have tried each of the above singly and all combinations together. I am at a loss as you why once deployed it will not use the customer key generator. There are no errors, the cache will work but it will use the default key generator.

Any help would be appreciated.

lilmidnit commented 6 years ago

I misinterpreted what was happening. It was running my custom key generator but was not running my override method. When moving the changes to the MakeCacheKey method it would then error that DefaultCacheKeyGenerator.MakeCacheKey method was not found - but only when run on the server. Once I removed the base class and implemented all the code locally it ran correctly.

I'm not sure at this point why the server did not have access to the DefaultCacheKeyGenerator properly.

Iamcerba commented 5 years ago

@lilmidnit, if you are going to use BearerControllerCacheKeyGenerator directly in CacheOutputAttribute it should be registered explicitly in IoC. Only this way default registered cache key generator will be overridden.

Something like this:

Bind<BearerControllerCacheKeyGenerator>().To<BearerControllerCacheKeyGenerator>();

or

Bind<BearerControllerCacheKeyGenerator>().ToSelf();