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

#EF : DbEntityValidationException - How can I easily tell what caused the error?

While calling  SaveChanges  on my  DbContext , I get the following exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. This is all fine and dandy, but I don't want to attach a debugger every time this exception occurs. More over, in production environments I cannot easily attach a debugger so I have to go to great lengths to reproduce these errors. How can I see the details hidden within the  DbEntityValidationException ? Answer :  The easiest solution is to override SaveChanges on your entities class. You can catch the DbEntityValidationException, unwrap the actual errors and create a new DbEntityValidationException with the improved message. Create a partial class next to your SomethingSomething.Context.cs file. Use the code at the bottom of this post. That's it. Your implementation will automatically use the overriden Save...

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....

#MVC Authorize Attribute

Authorization is the process of determining the rights of an authenticated user for accessing the application's resources. The Asp.Net MVC Framework has a AuthorizeAttribute filter for filtering the authorized user to access a resource. Authorize Attribute Properties Properties Description Roles Gets or sets the roles required to access the controller or action method. Users Gets or sets the user names required to access the controller or action method. Filtering Users by Users Property Suppose you want to allow the access of AdminProfile to only shailendra and mohan users then you can specify the authorize users list to Users property as shown below. [ Authorize ( Users = "Raj,Deena,Siva,Gow" )] public ActionResult AdminProfile () { return View (); } Filtering Users by Roles Property Suppose you want to allow the access of AdminProfile action to only Admin and SubAdmin roles then you can specify the authorize roles...