System.Web.Caching & System.Runtime.Caching against IIS App Pool restarts - asp.net

In Asp.net Forms or MVC application, cache items which use System.Web.Caching get clear in an app pool restart.
But what happens if caching was achieved using System.Runtime.Caching ?

But what happens if caching was achieved using System.Runtime.Caching ?
If you are using the MemoryCache default implementation then exactly the same will happen because the items will be stored in memory of the AppDomain. If you want your cache items to survive application pool restarts you might need to use a distributed cache system such as Redis or Memcached. By using a distributed cache system all the nodes of your web cluster will have access to those cache items and might take advantage of them.

Related

ASP.NET MemoryCache Sharing Across Applications

I have been reading all over the place about the new MemoryCache class starting in .Net Framework 4.0. From what I've read, you can access the MemoryCache across different .Net applications. I am trying to share an object between an Asp.Net application and a standard windows forms .Net application. If I add the object to the MemoryCache in the .Net application, the Asp.Net application does not see it. Is there any way to accomplish this? Thank you for your time, it is greatly appreciated.
Windows Form App:
Dim cache As ObjectCache = MemoryCache.Default
Dim policy As New CacheItemPolicy()
policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(60)
cache.Set("testcache", TestObj, policy)
Asp.Net App:
Dim cache As ObjectCache = MemoryCache.Default
If IsNothing(cache("testcache")) Then Response.Write("TestCache Is Nothing")
Thanks -
Ryan
No, that's not possible. MemoryCache is not a distributed caching solution. So it will only be available locally.
If you are looking for a distributed cache alternative you may want to look into AppFabric or Redis.
However, it does sound a bit like an odd architecture to want to share the cache that way.
Maybe exposing a shared services layer, that both the asp.net and winforms consume, and have just the services implement the caching would seem more logical (take into account I actually know nothing about the problem you are trying to solve, so I could be wrong).
Caching is more commonly used for performance reasons, not as a way to share data among applications.
MySQL memory tables are working great and having stellar performance. 20/30 inserts a second and only around 1% CPU load.
I realize this advice is not timely, but for others reading this question, another possibility is Interprocess Communication (IPC) between the two programs. This way the two programs can exchange messages/data directly without going thru an intermediate.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx
From the documentation above, here are some of your options.
Clipboard
COM
Data Copy
DDE
File Mapping
Mailslots
Pipes
RPC
Windows Sockets
In your case, mapped memory files might be the best approach as it allows for a shared memory space between applications. Fundamentally, your MySql/Redis approach is probably not that different.
if you are interested in :
single server multiple applications - memory share
you should consider create web api at the same node and consume it (simple key value API)
and use simple Memory Cache provided by .net framework
multiple servers - memory share
free $$ solution
you can use "Distributed SQL Server Cache", allows the
distributed cache to use a SQL Server database as its backing store.
To create a SQL Server cached item table in a SQL Server instance,
you can use the sql-cache tool. The tool creates a table with the
name and schema that you specify.
if you have a cluster of nodes you can use NCache it is
an open source in-memory distributed cache developed natively in
.NET and .NET Core. NCache works both locally and configured as a
distributed cache cluster for an ASP.NET Core app running in Azure
or on other hosting platforms.
solutions that require paying money:
NCache mentioned previously, there's Professional and Enterprise solutions,
or
Redis Cache - Redis is an open-source in-memory data store, which is often used as a distributed cache. You can configure an Azure Redis Cache for an Azure-hosted ASP.NET Core app, and use an Azure Redis Cache for local development.

ASP.NET warmup/initialize

I'm trying to eliminate (or at least minimize) startup/warmup times for my .NET applications. I'm not really sure on how to do this even though it's a common concern.
There's a ton of questions about slow startup of .NET applications. These are easily explained by pool recycles, worker process startup, dynamic compilation of .aspx files, JIT etc. In addition, there are more things that may need to be initialized within the application such as EntityFramework and application caches.
I've found alot of different solutions such as:
ASP.NET Precompilation
IIS 8 Application Initialization (and for IIS 7.5)
Auto-Start ASP.NET Applications
However, I'm not entirely satisfied with any of the solutions above. Furthermore I'm deploying my applications to Azure Websites (in most cases) so I have limited access to the IIS.
I know that there are some custom "warmup scripts" that uses various methods for sending requests to the application (e.g. wget/curl). My idea is to create a "Warmup.aspx" page in each of my ASP.NET applications. Then I have a warmup service that sends an HTTP GET to the Warmup.aspx of each site every ... 5 minutes. This service could be a WorkerRole in Azure or a Windows Service in an on-premise installation. Warmup.aspx will will then do the following:
Send an HTTP GET to each .aspx-file within the application (to
dynamically compile the page)
This could be avoided by precompiling the .aspx pages using aspnet_compiler.exe
Send a query to the database to
initialize EntityFramework
Initialize application caches etc
So, my final question is whether there are better alternatives than my "Warmup.aspx" script? And is it a good approach or do you recommend some other method? I would really like some official method that would handle the above criteria.
Any and all suggestions are welcome, thanks!
Did you try this IIS Auto-Start feature described here ?
https://www.simple-talk.com/blogs/2013/03/05/speeding-up-your-application-with-the-iis-auto-start-feature/
You could have two instances of the site. When you need to deploy a new version, and therefore suffer a startup cycle, remove one instance out of load balancer rotation, deploy and start it, set it in and do the same for instance 2. A rolling deployment.

ASP.NET persist Cache when AppDomain is refreshed

When code changes are published to .NET websites the re-compilation process involves restarting the AppDomain, this in turn wipes the application cache.
Are there any events raised when this happens? Is there any way to manually serialise portions of my cached data and save it to disk, then subsequently re-initialise the cache when the application is loaded again?
you can use MS AppFabric caching, it's a free component for Windows Server and solves this by decoupling the caching from the ISS app domain.
see question and answers here: AppFabric vs System.Runtime.Caching
if you keep using ASP.NET Caching you are exposed at the issues you have described above and to my knowledge there is no solution.

Determine if asp.net application support load balancing environment

I am new to asp.net, I have been tasked to upgrade the physical architecture to load balancing environment to support the application.
I done some reading the session state should be configure to Out-Process instead of In-Proc to support load balancing.
Is there any other issues I need to take note for asp.net web application to run in the load balanced environment?
if you are going to have your session out of proc, your session objects should be serializable. If you want a quick solution, sticky sessions are an option. You can read about it here

Extending ASP.NET data cache to be shared across web farm

I have an ASP.NET application that makes extensive use of ASP.NET cache API for caching commonly-used data. In addition, I am using polling-based sql cache dependency to track expiration.
The drawback of the current design is that, in the web farm environment, each web server has its own data cache that is not shared across servers.
Is there a way I can simply migrate the code to share data cache across multiple servers?
I've thought of using memcached however that would not work with sql cache dependency, right?
Any other solutions?
Have you looked at Windows Server AppFabric (Formerly Velocity).
http://blogs.msdn.com/velocity/
You could use following options -
Use the approach described here for synchronizing the data across web farms -
http://www.eggheadcafe.com/articles/20030420.asp
Use distributed caching approach
Use enterprise library cache.
It seems to me that Windows Server AppFabric is exactly what you are looking for. (AKA "Velocity"). From the introductory documentation:
Windows Server AppFabric provides a
distributed in-memory application
cache platform for developing
scalable, available, and
high-performance applications.
AppFabric fuses memory across multiple
computers to give a single unified
cache view to applications.
Applications can store any
serializable CLR object without
worrying about where the object gets
stored. Scalability can be achieved by
simply adding more computers on
demand. The cache also allows for
copies of data to be stored across the
cluster, thus protecting data against
failures. It runs as a service
accessed over the network. In
addition, Windows Server AppFabric
provides seamless integration with
ASP.NET that enables ASP.NET session
objects to be stored in the
distributed cache without having to
write to databases. This increases
both the performance and scalability
of ASP.NET applications.

Resources