Distributed caching using Redis #7618
We have 2 load balanced web server and we need to implement distributed caching for some of the frequently used SQL tables. The documentation is not clear, https://aspnetboilerplate.com/Pages/Documents/v1.5.2/Caching
We have uncommented the code for Redis in the pre initialize method of the webcoremodule.cs.
We have also setup the redis server as a windows service on a windows machine using the below link: https://github.com/MicrosoftArchive/redis/releases
how can we proceed further. How and where(in which project) can we actually write the code to set the cache with the data from SQL table.
We have setup the caching of a particular table as mentioned and the same is getting cached as desired using the cacheManager.
While unit testing we came across a scenario. Post uncommenting the use of Redis as cacheManager, everything is getting saved on the redis server(setup on a separate machine as a windows service). THis includes the token validity key as well. THis key is being validated every time a api request is made from the ValidateToken method(JwtSecurityTokenHandler.cs file)
If for some reason the Redis service stops or the machine altogether reboots/shuts down, the entire site stops and the user is redirected to the login page.
THis is not the case if we have multiple web servers(load balanced) and we are using in-memory caching instead of Redis. We tried this by login into our website(the request is being served by server 1), browsing the website for a while. Then stopping the IIS of server 1 and trying to browse the site further, the request is automatically served by server2 without user getting throw out to the login page.
How can this be achieved when using distributed caching?
Also, is there a possibility that the system uses pre-defined in-memory caching for caching the token and abp tables, while for caching particular SQL tables we use distributed caching.
When you enable the line "UseRedis", this code is executed https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.RedisCache/Runtime/Caching/Redis/RedisCacheConfigurationExtensions.cs#L30
It basically replaces the implementation of
So, you can leave UseRedis line uncommented and inject
AbpRedisCacheManagerclass where you want to use Redis cache. Rest of the usages of
ICacheManagerwill use In-Memory cache in that case.
You mean to say leave UseRedis commented right? Cause if we uncomment the code, all the caching is done on redis.
If we inject the AbpRedisCacheManager class, how can we define the connection string of the server. It always takes localhost:6379 as the connection string. My web server and the redis server would be placed on different physical servers. Below is the code snippet of the impelementation:
If I run it locally with redis server setup on my machine, everything is working as desired. But what if I want to point to a different redis server.
Thank you, but could you please explain how and where this needs to be implemented. If would be very helpful if you could share a code snippet for the implementation.
Also, say if I cache a 1000 rows of a SQL table and all the consequent requests are catered from the cache. Now, the admin adds 2 more rows to the table, when and how can we refresh the cache so that the end users always get the fresh data.
For updating cahce, you can use Domain Events https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events and in that event, you can update the cace.
You can configure the connectionString in the PreInitialize method of your web module.