Answered by:
how to use Redis as a cache for SQL Server in asp.net mvc

Question
-
User-341459723 posted
Please can you help me
How should I use Redis as a cache for MySQL or SQL Server Database in asp.net mvc:
I want to create sample app asp.net mvc for Example
I have table in sql server database ...
EMP table
id , name , city , phone..
I want to use Redis as a cache data for SQL Server Database (takes around 10 seconds)
after that I would put an expiry on that data and then when it becomes stale it will get refetched from the SQL Server database.
and remove Records in Redis as a cacheThursday, May 2, 2019 6:40 AM
Answers
-
User1520731567 posted
Hi NabilMosali,
after that store data in sql server and remove cach
You could refer to this article:
you could create a class named 'CacheStrigsStack'.
This class creates a connection and has all the methods to store the list and retrieve values from Redis.
public class CacheStrigsStack { private readonly RedisEndpoint _redisEndpoint; public CacheStrigsStack() { var host = ConfigurationManager.AppSettings["host"].ToString(); var port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]); _redisEndpoint = new RedisEndpoint(host, port); } public bool IsKeyExists(string key) { using (var redisClient = new RedisClient(_redisEndpoint)) { if (redisClient.ContainsKey(key)) { return true; } else { return false; } } } public void SetStrings(string key, string value) { using (var redisClient = new RedisClient(_redisEndpoint)) { redisClient.SetValue(key, value); } } public string GetStrings(string key, string value) { using (var redisClient = new RedisClient(_redisEndpoint)) { return redisClient.GetValue(key); } } public bool StoreList<T>(string key, T value, TimeSpan timeout) { try { using (var redisClient = new RedisClient(_redisEndpoint)) { redisClient.As<T>().SetValue(key, value, timeout); } return true; } catch (Exception) { throw; } } public T GetList<T>(string key) { T result; using (var client = new RedisClient(_redisEndpoint)) { var wrapper = client.As<T>(); result = wrapper.GetValue(key); } return result; } public long Increment(string key) { using (var client = new RedisClient(_redisEndpoint)) { return client.Increment(key, 1); } } public long Decrement(string key) { using (var client = new RedisClient(_redisEndpoint)) { return client.Decrement(key, 1); } } }
I want to use Redis as a cache data for SQL Server Database (takes around 10 seconds)
I suggest you could create a timer method to call CacheStrigsStack in 10 seconds in Global.asax.cs,for example:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace Proposal_Sln { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 10000; timer.Elapsed+=timer_Redis; timer.Start(); } private void timer_Redis(object sender, System.Timers.ElapsedEventArgs e) { /*do something,for example:
we are going to - first, check if the key exists.
If not, then we are going we are going to pull data from the database
and save it to Redis cache if the key exists then we are going to pull from Redis cache,
more details,please refer to the above article*/ } }Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 7, 2019 2:15 AM
All replies
-
User1520731567 posted
Hi NabilMosali,
According to your requirement,I find this post is similar to you.
Someone suggest use Service stack redis implementation,here are all the details required.
Redis is particularly good when doing caching in compare to other nosql.
But if you are having high read - write application, I will insist to check out nosql database as database combined with SQL server. That will help in case of scalability.
You can serialize your tables and store them in the Cache.
The following blog explains how you can store your information in SQL Server and Redis Cache. It using MVC and EntityFramework as well:
https://azure.microsoft.com/en-us/blog/mvc-movie-app-with-azure-redis-cache-in-15-minutes/
Best Regards.
Yuki Tao
Thursday, May 2, 2019 8:03 AM -
User-341459723 posted
thanxxx Yuki Tao, for help me ^_^
I want to use Redis Cache in localhost not Azure how to use it that
after that store data in sql server and remove cach
Best Regards.
Thursday, May 2, 2019 9:50 AM -
User1520731567 posted
Hi NabilMosali,
after that store data in sql server and remove cach
You could refer to this article:
you could create a class named 'CacheStrigsStack'.
This class creates a connection and has all the methods to store the list and retrieve values from Redis.
public class CacheStrigsStack { private readonly RedisEndpoint _redisEndpoint; public CacheStrigsStack() { var host = ConfigurationManager.AppSettings["host"].ToString(); var port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]); _redisEndpoint = new RedisEndpoint(host, port); } public bool IsKeyExists(string key) { using (var redisClient = new RedisClient(_redisEndpoint)) { if (redisClient.ContainsKey(key)) { return true; } else { return false; } } } public void SetStrings(string key, string value) { using (var redisClient = new RedisClient(_redisEndpoint)) { redisClient.SetValue(key, value); } } public string GetStrings(string key, string value) { using (var redisClient = new RedisClient(_redisEndpoint)) { return redisClient.GetValue(key); } } public bool StoreList<T>(string key, T value, TimeSpan timeout) { try { using (var redisClient = new RedisClient(_redisEndpoint)) { redisClient.As<T>().SetValue(key, value, timeout); } return true; } catch (Exception) { throw; } } public T GetList<T>(string key) { T result; using (var client = new RedisClient(_redisEndpoint)) { var wrapper = client.As<T>(); result = wrapper.GetValue(key); } return result; } public long Increment(string key) { using (var client = new RedisClient(_redisEndpoint)) { return client.Increment(key, 1); } } public long Decrement(string key) { using (var client = new RedisClient(_redisEndpoint)) { return client.Decrement(key, 1); } } }
I want to use Redis as a cache data for SQL Server Database (takes around 10 seconds)
I suggest you could create a timer method to call CacheStrigsStack in 10 seconds in Global.asax.cs,for example:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace Proposal_Sln { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 10000; timer.Elapsed+=timer_Redis; timer.Start(); } private void timer_Redis(object sender, System.Timers.ElapsedEventArgs e) { /*do something,for example:
we are going to - first, check if the key exists.
If not, then we are going we are going to pull data from the database
and save it to Redis cache if the key exists then we are going to pull from Redis cache,
more details,please refer to the above article*/ } }Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 7, 2019 2:15 AM