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. [CSP-S模拟测试]:Read(数学)

    题目描述 热爱看书的你有$N$本书,第$i$本书的种类为$A[i]$.你希望每天能够看一本书,但是不希望连续两天看种类相同的书.为了达成这个条件,你需要选择一些书不看,作为一个好学生,你希望不看的书尽 ...

  2. Linux系统结构 详解

    Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统.部分层次结构如图1-1所 ...

  3. webapi返回json格式优化 转载https://www.cnblogs.com/GarsonZhang/p/5322747.html

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 1 config.Formatters.Remove(config.F ...

  4. asp.net的处理机制(.ashx/.aspx)

    浅谈自己对asp.net 处理机制的图解 图解的内容有点多(包含asp.net 的处理机制和页面生命周期的重要事件,建议小伙伴把图片下载查看可好?) asp.net处理机制解说 当浏览器发送一条请求给 ...

  5. selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取

    代码要多敲 注释要清晰 哪怕再简单 #使用selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取 #地址:https://music.douban.com/chart #导入需要的模块 f ...

  6. python外星人入侵(游戏开发)

    实现的项目要求: 1.外星人游戏添加飞船上下移动功能: 2.为游戏添加背景音乐: 3.在玩家得分.最高得分.玩家等级前添加"Score"."High Score" ...

  7. FlowProtal jQuery 对比时间大小

    function ValidatoTime(source, args){ var StartTime = agent.calcExpress(null, 'SCTDB:AS_MeetingRoom.S ...

  8. [Java] 缓存池

    new Integer(123) 与 Integer.valueOf(123) 的区别在于: new Integer(123) 每次都会新建一个对象: Integer.valueOf(123) 会使用 ...

  9. exsi 回收虚拟机磁盘

    用客户端登陆服务端,用下面命令停止虚拟机机器 esxcli vm process list    用如下命令关闭一台虚拟机: esxcli vm process kill --type=[soft,h ...

  10. 删除文件时提示“找不到该项目”,怎么解决? 转摘自:http://jingyan.baidu.com/article/e4d08ffdf5ab470fd2f60df4.html

    故障现象:在使用Windows系统删除文件或者文件夹的时候,有时会出现“找不到该项目”的错误提示,可能再次“重试”也无济于事. 那么,接下来T库小编就为库友们简单概括一下出现该问题的原因. 故障原因: ...