alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

How do I set SizeLimit when using asp.net core DI? #151

Closed coderdnewbie closed 3 years ago

coderdnewbie commented 3 years ago

I am building a webapi and want to use LazyCache. Can the SizeLimit be set? The only way that is shown to do this is:

IAppCache cacheWithSizeLimit = new CachingService( new MemoryCacheProvider(new MemoryCache(new MemoryCacheOptions { SizeLimit = 5 })));

However, I am puzzled how to do this using the DI mechanism. How do I set the SizeLimit when asp.net is making the IAppCache instance?

I found this:

services.AddMemoryCache(options => options.SizeLimit = 1000);

Would this work for lazycache, I can’t see how to do it. Please help.

JohnFedorchak commented 3 years ago

Register it in DI with a factory class or factory delegate.

https://www.stevejgordon.co.uk/asp-net-core-dependency-injection-registering-implementations-using-delegates

coderdnewbie commented 3 years ago

Thanks I will try and figure that out.