Similarly, we serialise our users list and save it as a string in the Redis cache under the _Users key. It’s worth noting that we need to deserialise it to the type we want before returning it to the caller. In the GetFromCache method, we call the GetStringAsync with a given key ( _Users in this case). The code itself it pretty self-explanatory. The real magic happens in the ICacheProvider class. I’m going to skip all the other plumbing code and show you how we Get and Set values with the Redis cache. Refer to Andrew Lock’s excellent article on this topic. You don’t want to write the plumbing code we have written here to emulate a similar thing as the default DI container doesn’t support the behaviour. □ Tip: You would ideally want to decorate the UsersService class with CacheService by using a DI container such as Scrutor. The code here is pretty self-explanatory, and we implement the 3 features we discussed in the Index, CacheUserAsync and CacheRemoveAsync actions. We will create a Web MVC app in ASP.NET Core 6. Wrapper around the NCache Distributed Cache Use Redis as a backing store (locally or in cloud with Azure Redis Cache)client package is Developed by peeps at StackExchange. Use SQL Server instance as a cache (locally or in cloud with Azure SQL Server). This is only recommended for dev and testing purposes. Here’s a summary of different ways you can do this. And the actual implementation is specific to the technology we want to use. The IDistributedCache interface provides us with a bunch of methods to manipulate your cache. Head over here to have a look at how this could be useful in enterprise applications. The main reason why we call this a distributed cache is that it lives outside of our application server (as opposed to traditional in-memory caching) and we have the flexibility of scaling it horizontally (when operating in the cloud), if need be.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |