How to deal with costly building operations using MemoryCache?

We solved this issue by combining Lazy<T> with AddOrGetExisting to avoid a need for a lock object completely. Here is a sample code (which uses infinite expiration): public T GetFromCache<T>(string key, Func<T> valueFactory) { var newValue = new Lazy<T>(valueFactory); // the line belows returns existing item or adds the new value if it doesn’t exist … Read more

How can I use Dependency Injection in a .Net Core ActionFilterAttribute?

As per the documentation, you have a few options here: If your filters have dependencies that you need to access from DI, there are several supported approaches. You can apply your filter to a class or action method using one of the following: ServiceFilterAttribute TypeFilterAttribute IFilterFactory implemented on your attribute ServiceFilter or TypeFilter attributes If … Read more

Using multiple instances of MemoryCache

I recently went through this myself as well. Considering an in memory cache will be process specific (not shared across multiple instances of a website or native business app or multiple servers) there is really no benefit to having multiple MemoryCache instances except for code organizational reasons (which can be achieved in other ways). The … Read more

MemoryCache Thread Safety, Is Locking Necessary?

The default MS-provided MemoryCache is entirely thread safe. Any custom implementation that derives from MemoryCache may not be thread safe. If you’re using plain MemoryCache out of the box, it is thread safe. Browse the source code of my open source distributed caching solution to see how I use it (MemCache.cs): https://github.com/haneytron/dache/blob/master/Dache.CacheHost/Storage/MemCache.cs

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)