2018年4月19日 星期四

[ASP.NET] Use Hangfire to schedue caching to Redis


 Redis   HangFire  


Introduction


Here is an example of using Hangfire with Sql server to schedule a cache-creating to Redis.


Environment


Visual Studio 2017 community
ASP.NET MVC 5
Hangfire 1.6.19
Hangfire.SqlServer 1.6.19
StackExchange.Redis 1.2.6
Newtonsoft.Json 11.0.2


Implement


Install packages



Create database for Hangfire

First we have to create a database or use an exist one for Hangfire.
Make sure the connection string is in your WebConfig.

<connectionStrings>
   <add name="HangFire" connectionString="…." providerName="System.Data.SqlClient" />
</connectionStrings>


Setup HangFire

Add an OWIN Startup class into your ASP.NET MVC template.




public void Configuration(IAppBuilder app)
{
            this.setHangFire(app);
}

private void setHangFire(IAppBuilder app)
{
            var options = new SqlServerStorageOptions
            {
                QueuePollInterval = TimeSpan.FromSeconds(60), // Default value
            };

            // Hangfire: Use Sql server
            GlobalConfiguration.Configuration.UseSqlServerStorage("HangFire", options);
            // Enable HanfireServer
            app.UseHangfireServer();
            // Enable Hangfire's dashboard
            app.UseHangfireDashboard();
        }
}

1.  Job storage in Sql server uses pooling to fetch jobs.The default polling interval is 15 seconds. You can modify it by defining a custom option: SqlServerStorageOptions

2.  Notice that “HangFie” of this line is the connection string’s name in WebConfig

GlobalConfiguration.Configuration.UseSqlServerStorage("HangFire", options);

3.  (Optional) Enable the dashboard of Hangfire

app.UseHangfireDashboard();


Build and run to initialize

After initializing Hangfire, you can open the website like http://MyServer/hangfire
to watch the dashboard.




And check that the tables created automatically by Hangfire.

 


Logging

If we already have logging framework such like NLog, Log4Net, Elmah (See document),
We don’t have to do extra settings to enable logging.

However, you can still specify the logging level of Hangfire in your logging framework.

For example, in NLog.config

<rules>
    <logger name="Hangfire.*" levels="Error, Fatal" writeTo="MyFile"  />
</rules>




Create Redis Service class

Before scheduling a Hangfire job, lets create service class for accessing Redis.

public class CacheService
{
        private const string SERVER = "MyServer:6379";
        private readonly ConnectionMultiplexer redis = null;
        private readonly IDatabase redisDb = null;

        /// <summary>
        /// Constructor
        /// </summary>
        public CacheService()
        {
            this.redis = ConnectionMultiplexer.Connect(SERVER);
            this.redisDb = redis.GetDatabase();
        }

        public void SaveCache<T>(string key, T data)
        {
            var value = JsonConvert.SerializeObject(data);
            this.redisDb.StringSet(key, value);
        }

public bool GetCache<T>(string key, out T rtn) where T : new()
        {
            if (redisDb.KeyExists(key))
            {
                var value = this.redisDb.StringGet(key);
                rtn = JsonConvert.DeserializeObject<T>(value);
                return true;
            }
            else
            {
                rtn = new T();
                return false;
            }
        }
}



Fire or Schedule a Background job

//Fire a background job immediately
BackgroundJob.Enqueue(() => ()=>new CacheService().SaveCache("Emp", data));

//Schedule a cron job
RecurringJob.RemoveIfExists("CacheEmployeeId");
RecurringJob.AddOrUpdate("CacheEmployeeId", ()=>new CacheService().SaveCache("Emp", data), Cron.Daily);



Result for firing a background job immediately







Result for scheduling a cron job




Reference







沒有留言:

張貼留言