Skip to main content

#C# Data Caching


There are 3 types of caching in ASP.NET:
  1. Output Caching
  2. Fragmentation Caching
  3. Data Caching
We implemented "Data Caching" in Cost and scheduling tool.

Caching takes place where frequently-used data is stored so that the application can quickly access the data rather than accessing the data from the source. Caching can improve the performance and scalability of the application dramatically and can help us to remove the unnecessary requests to the external data sources for the data that changes infrequently.
This is the simplest way of caching. In this technique, cache is stored in the memory of the local Web Server.
An in-memory cache is stored into the Server memory which is hosting the ASP.NET application. In the case of Web farm (where the application is hosted on multiple servers) or cloud hosting environments, all servers may have different values of in-memory cache. So, in-memory caching cannot be used in web farm or cloud hosting environments. For this type of environment, distributed cache is best suited.

Session vs Caching

The first main difference between session and caching is: a session is per-user based but caching is not per-user based, so what does that mean? Session data is stored at the user level but caching data is stored at the application level and shared by all the users. It means that it is simply session data that will be different for the various users for all the various users, session memory will be allocated differently on the server but for the caching only one memory will be allocated on the server and if one user modifies the data of the cache for all, the user data will be modified.

Create Cache
using System.Runtime.Caching;
MemoryCache memoryCache = MemoryCache.Default;

Assign data to cache
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration =DateTime.UtcNow.AddMinutes(15);

memoryCache.Add(new CacheItem("AllFilterData", allTablesForFilter.Value), cacheItemPolicy);
Read data from Cache
if (memoryCache.Contains("AllFilterData"))
            {
                return (AllTablesForFilter)memoryCache.Get("AllFilterData");

            }

Comments

Popular posts from this blog

Use Log4Net in C# windows form Application

we are going to learn on how to use the Log4Net library for creating logs. Create a new windows form application in VS. Install Log4Net library Add to AssemblyInfo.cs  Configure in  App.config Use in code  Install Log4Net library Then install the Log4Net library from the Nuget Package library.           log4net by The Apache software foundation 2.0.8 (i installed the latest). Add to AssemblyInfo.cs  After installing this package, open up AssemblyInfo .cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.).    [assembly: log4net.Config.XmlConfigurator(Watch= true )]  Configure in  App.config Now, open the App.config file and enter required details for LogNet to work. <configSections>       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2....

#SQL: How to Drop Database in SQL Server by Closing Existing Connections

How to Drop Database in SQL Server when the users are connected to a SQL Server Database. You may need to closing existing connections in a database before restoring the database, before detaching database, get database in SINGLE_USER mode etc. If you try dropping a database when users are connected to the SQL Server Database you will receive the below mentioned error message. Error Message Drop failed for Database 'RajDB'. (Microsoft.SqlServer.Smo) Cannot drop database "" because it is currently in use. (Microsoft SQL Server, Error: 3702) USE [master] GO ALTER DATABASE RajDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO /* Query to Drop Database in SQL Server  */ DROP DATABASE RajDB GO