Loading...
Changes Saved.
Error Occurred!

Hướng dẫn tích hợp sử dụng Elastic Cached (for Redis) với mã nguồn ASP.NET | .NET Core trên hosting Window

Trong hướng dẫn này sẽ hướng dẫn tạo mới 1 ứng dụng ASP.NET WEB sử dụng Elastic Cached của PA Việt Nam.

 

Đầu tiên, các bạn cần tạo mới 1 project ASP.NET MVC trên Visual Studio.

 Ở bài viết này, mình sử dụng visual Studio 2019 và tạo project ASP.NET MVC với Frameworkd 4.5.2

 

Tiếp theo cài đặt thư viện để hỗ trợ sử dụng redis Cached.

Trong bài viết này sẽ sử dụng StackExchange.Redis : https://stackexchange.github.io/StackExchange.Redis/
(Bạn có thể sử dụng thư viện khác phụ thuộc vào kinh nghiệm và quyết định của bạn, tìm từ khóa Redis trong nuget)

1 - Cài đặt StackExchange.Redis thông qua nuget manager trên visual studio, lưu ý chọn phiên bản tương thích với version Framework mà bạn sử dụng

 

Cập nhật cấu hình cho MVC Website.

1. Cập nhật file web.config thêm khai báo chuỗi kết nối vào Server Redis (ElasticCached)

<appSettings>

    <add key="CacheConnection" value="IP_Server:Port,ssl=false,password=<password>"/>

</appSettings>

 

Trong đó , Các thông tin IP_Server, Port, Password khi Quý khách đăng ký dịch vụ ElasticCached phía kỹ thuật sẽ cung cấp cho Quý khách

 

2. Cập nhật file HomeController.cs trong ứng dụng MVC của bạn để sử dụng thư viện StackExchange.Redis

Code : 

using System.Configuration;

using StackExchange.Redis;

 

- Thêm vào code để test Redis như sau : 

public ActionResult RedisCache()

    {

        ViewBag.Message = "A simple example with ElasticCached for Redis on ASP.NET.";
        var lazyConnection = new Lazy<ConnectionMultiplexer>(() =>        {
            string cacheConnection = ConfigurationManager.AppSettings["CacheConnection"].ToString();
            return ConnectionMultiplexer.Connect(cacheConnection);
        });

        IDatabase cache = lazyConnection.Value.GetDatabase();     

        ViewBag.command1 = "PING";

        ViewBag.command1Result = cache.Execute(ViewBag.command1).ToString();

        ViewBag.command2 = "GET Message";

        ViewBag.command2Result = cache.StringGet("Message").ToString();

        ViewBag.command3 = "SET Message \"Hello! The cache is working from ASP.NET!\"";

        ViewBag.command3Result = cache.StringSet("Message", "Hello! The cache is working from ASP.NET!").ToString();

        ViewBag.command4 = "GET Message";

        ViewBag.command4Result = cache.StringGet("Message").ToString();

        ViewBag.command5 = "CLIENT LIST";

        ViewBag.command5Result = cache.Execute("CLIENT", "LIST").ToString().Replace(" id=", "\rid=");

        lazyConnection.Value.Dispose();

        return View();

    }

3 - Cập nhật file _Layout.cshtml 

thay đoạn code : @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })

bằng đoạn code : @Html.ActionLink("Azure Cache for Redis Test", "RedisCache", "Home", new { area = "" }, new { @class = "navbar-brand" })

 

4 - Thêm 1 view mới trong project đặt tên RedisCache.cshtml  và nhập đoạn code sau

 

@{
    ViewBag.Title = "ElasticCache for Redis Test";
}

<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<br /><br />
<table border="1" cellpadding="10">
    <tr>
        <th>Command</th>
        <th>Result</th>
    </tr>
    <tr>
        <td>@ViewBag.command1</td>
        <td><pre>@ViewBag.command1Result</pre></td>
    </tr>
    <tr>
        <td>@ViewBag.command2</td>
        <td><pre>@ViewBag.command2Result</pre></td>
    </tr>
    <tr>
        <td>@ViewBag.command3</td>
        <td><pre>@ViewBag.command3Result</pre></td>
    </tr>
    <tr>       
<td>@ViewBag.command4</td>
        <td><pre>@ViewBag.command4Result</pre></td>
    </tr>
    <tr>
        <td>@ViewBag.command5</td>
        <td><pre>@ViewBag.command5Result</pre></td>
    </tr>
</table>

 

Cuối cùng Build project và test Web Site và xem kết quả. Nếu kết nối và lưu cached thành công sẽ có thông tin như ảnh dưới

 

Đối với mã nguồn mở Dotnetnuke (DNN)

Bạn có thể sử dụng plugin DNN Redis Caching provider , được cung cấp tại https://github.com/davidjrh/dnn.rediscachingprovider

Download latest release tại : https://github.com/davidjrh/dnn.rediscachingprovider/releases

1. Vào Quản trị  DNN -> Quản lý Extension và chọn install từ file zip download ở trên 

2. Vào file web.config, thêm khai báo connection String cho Redis Cached như sau

<connectionStrings>
    <add name="RedisCachingProvider" 
   connectionString="IP_Server:Port,password=<password>,ssl=false
  providerName="DotNetNuke.Providers.RedisCachingProvider" />
  </connectionStrings>

Trong đó , Các thông tin IP_Server, Port, Password khi Quý khách đăng ký dịch vụ ElasticCached phía kỹ thuật sẽ cung cấp cho Quý khách

3. Vào Setting -> Redis Cached tích Enable Redis Cache để bắt đầu sử dụng




Related Articles