Thursday 1 February 2018

Azure Radis Cache Integration In C# Asp.net.

Step1- First add a Class File in your webApplication Name is RadisCacheLayer ->
 //----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Threading;
using StackExchange.Redis;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;
using System.Configuration;
using Newtonsoft.Json;
using BusinessEntities;
using System.Net.Http;
using System.Net;
using System.Linq;

namespace WebApplication
{
    public class RadisCacheLayer
    {
        private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            //string cacheConnection = ConfigurationManager.AppSettings["CacheConnection"].ToString();
            //return ConnectionMultiplexer.Connect(cacheConnection);
            return ConnectionMultiplexer.Connect("Satya.redis.cache.windows.net ,abortConnect=false,ssl=true,password=gfd5654vcbc5L1XEOq59RAvhgftlA11KcBEMhLmmvkqWpsLC4=");
        });

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return lazyConnection.Value;
            }
        }

        public void ClearCachedTeams(string CacheName)
        {
            IDatabase cache = Connection.GetDatabase();
            cache.KeyDelete(CacheName);
            //cache.KeyDelete("teamsSortedSet");
            //  ViewBag.msg += "Team data removed from cache. ";
        }
    }
}
Step2- Declare Radis cache Object ->
   #region Declare Radeis Object and Set Connection
        IDatabase cache = RadisCacheLayer.Connection.GetDatabase();

    #endregion
Step3- Declare a Class For Example Name is ClsBackgroudLoadData ->
public class ClsBackgroudLoadData
    {

        public string Client_Domain { get; set; }
        public string CompanyBanner { get; set; }
        public string CompanyLogo { get; set; }
        public string CompanyTheme { get; set; }
        public string FavicaIconUrl { get; set; }
        public string CompanyTitle { get; set; }
        public string CompanyColorCode { get; set; }
        public string companytopBorder { get; set; }
        public string poweredImageurl { get; set; }    }
Step4- Get Data from Database and Store Radis cache Object ->
  List<ClsBackgroudLoadData> _objbranding = new List<ClsBackgroudLoadData>();
        private List<ClsBackgroudLoadData> GetAllcompayBrandingData()
        {

            string listvalueallCompanybranding = cache.StringGet("cachecompanyBranding");
            if (listvalueallCompanybranding == null)
            {
                _objbranding = (from m in GetMaster_ERPClient_Member()
                                join n in GetMaster_Login_User() on m.Login_User_ID equals n.Login_User_ID
                                join p in GetMaster_User_Registration() on n.User_RegistrationId equals p.User_RegistrationId
                                join o in GetAllCompanyBranding() on n.Login_User_ID equals o.Login_User_ID
                                select new ClsBackgroudLoadData
                                {
                                    Client_Domain = m.Client_Domain,
                                    CompanyBanner = o.CompanyBanner,
                                    CompanyLogo = o.CompanyLogo,
                                    CompanyTheme = o.CompanyTheme,
                                    FavicaIconUrl = o.FavicaIconUrl,
                                    CompanyTitle = o.CompanyTitle,
                                    CompanyColorCode = o.CompanyColorCode,
                                    companytopBorder = "10px solid" + " " + o.CompanyColorCode,
                                    poweredImageurl = "https://swasherpstorageaccount.blob.core.windows.net/kencentralmasterimage/logo.png",
                                }).ToList();
                if (_objbranding.ToList().Count > 0)
                {
                    cache.StringSet("cachecompanyBranding", JsonConvert.SerializeObject(_objbranding.ToList()), TimeSpan.FromMinutes(20));
                    return _objbranding;
                }
                return _objbranding;
            }
            else
            {
                var UserList = JsonConvert.DeserializeObject<List<ClsBackgroudLoadData>>(listvalueallCompanybranding);
                return UserList;
            }

        }
Step5- Delete Data from Cache ->

        #region..Clear Cache method....
        public void ClearCachedTeams()
        {
            cache.KeyDelete("cachecompanyBranding ");

        }

        #endregion

No comments:

Post a Comment