Friday 4 August 2017

Azure Redis Cache Implementation on Asp.net

Redis Cache

Azure Redis Cache is based on the popular open source Redis cache. It gives you access to a secure, dedicated Redis cache, managed by Microsoft and accessible from any application within Azure.
Azure Redis Cache is available in the following tiers:
  • Basic—Single node, multiple sizes, ideal for development/test and non-critical workloads. The Basic tier has no SLA.
  • Standard—A replicated cache in a two-node primary/secondary configuration managed by Microsoft, with a high-availability SLA.
  • Premium—All of the Standard tier features, including a high-availability SLA, as well as better performance over Basic and Standard-tier caches, bigger workloads, disaster recovery, enhanced security and more.
    Additional Premium tier features include:
    • Redis persistence allows you to persist data stored in Redis cache. You can also take snapshots and back up the data which you can load in case of a failure.
    • Redis cluster automatically shards data across multiple Redis nodes, so you can create workloads of bigger memory sizes (greater than 53 GB) and get better performance.
    • Microsoft Azure Virtual Network deployment gives you enhanced security and isolation for your Azure Redis Cache, as well as subnets, access control policies and other features to further restrict access.
Process To Implement On Programme -:

   Add a Class Layer on your Soloution. Which name is RadisCacheLayer. And Next add this Code.


 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=sewewe34434324242rweffsfds=");
        });

        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. ";
        }
    }
==============================
Next on Page You write this code :

 InductionNewEntities obj = new InductionNewEntities();

        IDatabase cache = RadisCacheLayer.Connection.GetDatabase();



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            string value2 = cache.StringGet("loginlis");

            if (value2 != null)
            {

                var UserList = JsonConvert.DeserializeObject<List<UserRegistration>>(value2);

                GridView1.DataSource = UserList;
                GridView1.DataBind();

            }
            else
            {
                var data = (from a in obj.UserRegistrations
                            select new
                            {
                                a.Company_Name,
                                a.Order_Name,
                                a.Status
                            }).ToList();

                GridView1.DataSource = data;
                GridView1.DataBind();

                cache.StringSet("loginlis", JsonConvert.SerializeObject(data.ToList()), TimeSpan.FromMinutes(1));

            }


        }




        #region...Insert data into database....
        protected void Button3_Click(object sender, EventArgs e)
        {
            UserRegistration mn = new UserRegistration();
            mn.Company_Name = TextBox2.Text;
            mn.Order_Name = TextBox1.Text;
            obj.UserRegistrations.Add(mn);
            obj.SaveChanges();
            TextBox2.Text = "";
            TextBox1.Text = "";
            #region....Clean Cache......
            ClearCachedTeams();
            #endregion
            BindGrid();
        }
        #endregion

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

        }
        #endregion
    }