Single instance of a MemoryCache across multiple application pools on the same server [duplicate]

You could create a separate Web Api project and host your implementation of the MemoryCache there. Then expose an api that gets / sets items from the cache that all of your apps can use.

Though, there are caveats with this approach. For example, if you use MemoryCache.Default then your cache lives as long as your application pool lives - the default application pool idle time-out is 20 minutes which is less than 1 hour. What I'm getting to is that I think you will have to keep a lot of the App Pool settings in sync and I believe this will lead to issues.

A better approach is to implement a distributed cache. I recommend using Redis and the StackExchange.Redis client. There are other alternatives such as memcached.

Suggestions for simple .NET distributed caching solution

解答1

memcached along with an ASP.NET provider is a popular choice. Bear in mind though that in .NET 4.0 the recommended way to do caching is to use the new ObjectCache instead of HttpRuntime.Cache. There is a built in memory implementation into the .NET framework (MemoryCache) and also you may checkout an implementation for memcached.

解答2

Simple, fast, lightweight and safe sound like things like redis and memcached, which can be very effective as a central cache. For stackoverflow we use redis via BookSleeve (client) but most stores will work similarly. There is also an AppFabric cache, but that is considerably more complex.

Key points though:

  • your data will need to be serializable in some way
  • if you are currently using cache of large objects (like a big DataTable) you'll need to consider bandwidth implications, or make it more granular
  • you'd probably benefit from a 2-tier cache (one local memory, with the central store as secondary)
  • which means you'd also need to consider invalidation (we do that via the pub/sub API in redis)

Sharing cache between apps (asp.net) on the same physical machine

Can not be done. Multiple asp.net apps live in their own appdomain, and the appdomain is IIS controlled and may cycle at any time. This means any caching HAS to use networking scenarios (remoting etc.) and the cache better be suited outside the ASP.NET control (system service).

ASP.Net Cache sharing

 

While others have mentioned the built in ASP.NET Cache (System.Web.Caching), please note that .NET 4.0 introduces a whole new caching framework designed to work outside of the System.Web.Caching namespace:

System.Runtime.Caching

http://msdn.microsoft.com/en-us/library/system.runtime.caching(VS.100).aspx

Now, this is much more of a beast than the simple System.Web.Caching's Cache item. But, you get the advantage of having multiple cache stores, locking of cache objects/keys, and the extensibility points of creating your own external cache providers - such as one for Microsoft's new Velocity distributed caching system. It comes with a MemoryCache provider built-in.

It's really not that difficult to use. Straight from MSDN's article on the built-in MemoryCache provider (again, you can implement your own) using it in a WinForms application (you'd change the code for your web app):

http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string; if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy(); List<string> filePaths = new List<string>();
filePaths.Add("c:\\cache\\example.txt"); policy.ChangeMonitors.Add(new
HostFileChangeMonitor(filePaths)); // Fetch the file contents.
fileContents =
File.ReadAllText("c:\\cache\\example.txt"); cache.Set("filecontents", fileContents, policy);
} Label1.Text = fileContents;

I've implemented NCache (mentioned above), as well as Memcached. I can tell you that Microsoft's Velocity is really the way to go for partitioning the data and setting up redundancy within the cache partitions itself (very cool). Not to mention, it's free!

share memory cache across multi web application的更多相关文章

  1. ThreadLocal Memory Leak in Java web application - Tomcat

    ThreadLocal variables are infamous for creating memory leaks. A memory leak in Java is amount of mem ...

  2. The web application registered the JDBC driver * but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

    最近使用了最新版的tomcat9,使用jdbc链接mysql数据库.关闭tomcat过程中出现警告 13-Sep-2017 22:22:54.369 WARNING [main] org.apache ...

  3. Why does the memory usage increase when I redeploy a web application?

    That is because your web application has a memory leak. A common issue are "PermGen" memor ...

  4. The web application [ ] registered the JDBC driver [net.sourceforge.jtds.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver

    出现以下错误时,我找了很多方法,都未解决,网上有很多,最后我实在无奈,怀疑会不会是Tomcat的原因,更换了一个版本之后就好了.The web application [ ] registered t ...

  5. 严重: The web application [] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDB

    idea项目启动报如下错误, 网上的方法都试了都没用, 一直没解决, 干掉项目, 重新从svn检出就好了...坑 啊 Root WebApplicationContext: initializatio ...

  6. registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

    问题是tomcat的版本问题,tomcat新检测机制导致的这个问题,换版本可以解决问题,但不建议这么做,租用服务器不是你说换就换的.其实问题根源是BasicDataSource,BasicDataSo ...

  7. 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

    tomcat reload显示错误:SEVERE: The web application [/Interceptor] created a ThreadLocal with key of type ...

  8. 解决:The web application [] registered the JDBC driver [] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

    问题描述 在将Spring Boot程序打包生成的war包部署到Tomcat后,启动Tomcat时总是报错,但是直接在IDEA中启动Application或者用"java -jar" ...

  9. Creating an API-Centric Web Application[转]

    Creating an API-Centric Web Application 转自 http://hub.tutsplus.com/tutorials/creating-an-api-centric ...

随机推荐

  1. 纯CSS 常见3D实例

    一.正方体 我认为正方体可以算是3D图像的基础吧,首先正方体是由六个相同的面所组成,其次就需要我们依次构造.(据体构造在代码中)成平图如下: 代码如下: <!DOCTYPE html> & ...

  2. 简单js表单验证

     简单js表单验证demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org ...

  3. post方式请求数据

    post方式请求数据 分析: 1.将请求方式改成post conn.setRequestMethod("POST"); 2.设置连接可以输出 conn.setDoOutput(tr ...

  4. 用 Flask 来写个轻博客 (30) — 使用 Flask-Admin 增强文章管理功能

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 实现文章管理功能 实现效果 前文列表 用 Flask 来写个 ...

  5. PS操作

    笔:B 橡皮:E 新建:C+N 选取套索:L 套索后按Alt,移动 放大:Ctrl + “+” 或 Z 缩小:Ctrl + “-” 或 Alt Z / Alt ,然后用滑轮控制大小 撤销:Ctrl + ...

  6. Python笔记(七)_全局变量与局部变量

    全局变量与局部变量:在函数外部或内部定义的变量 1. 函数内部的变量名首次出现,且在=号左边 不管这个变量在全局域中有没有定义该变量名,都被视为一个局部变量 例1: >>>num=1 ...

  7. JVM系列(一) — Jvm内存模型

    总结自<深入理解java虚拟机> 很多博客在讲虚拟机内存模型时,比较宽泛或者粗化,甚者,不准确,以下是我的一个笔记照片 运行时数据区可以分为两部分:线程共享区和线程私有区 一.线程共享区 ...

  8. FastJSON 远程执行漏洞,速速升级!

    相信大家用 FastJSON 的人应该不少,居然有漏洞,还不知道的赶紧往下看,已经知道此漏洞的请略过-- 2019年6月22日,阿里云云盾应急响应中心监测到FastJSON存在0day漏洞,攻击者可以 ...

  9. Python里面search()和match()的区别?

    match()函数只检测RE是不是在string的开始位置匹配,search()会扫描整个string查找匹配, 也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话, ...

  10. rabbitmq-5-案例1-简单的案例

    最简单的案例: https://gitee.com/n_zhe/rabbitmq-demo 通过简单的例子分析mq是怎样发送和拉取消息的:  quickStart中的简单案例     通过简单的案例来 ...