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. scrapy工作流程

    整个scrapy流程,我们可以用去超市取货的过程来比喻一下 两个采购员小王和小李开着采购车,来到一个大型商场采购公司月饼.到了商场之后,小李(spider)来到商场前台,找到服务台小花(引擎)并对她说 ...

  2. 分布式架构下的会话追踪实践【基于Cookie和Redis实现】

    分布式架构下的会话追踪实践[基于Cookie和Redis实现] 博客分类: NoSQL/Redis/MongoDB session共享rediscookie分布式架构session 在单台Tomcat ...

  3. mysql隔离级别相关

    1.原子性.隔离性.一致性.持久性 2.mysql并发控制可能出现的问题: 脏读(A事务读取到B事务未commit的数据后,B事务回滚) 不可重复读(A事务第一次读到的数据,被B事务更新数据后,第二次 ...

  4. 【python3】装饰器

    参考文章: 理解Python装饰器(Decorator) 关键点: 写装饰器一定要搞定楚函数名后面带小括号和不带小括号的含义.带小括号,表示调用这个函数,而不带小括号,则表示的是该函数引用地址 简单装 ...

  5. 查找android so文件中绕过c/c++ api直接调用SYSCALL的方法位置

    很多应用会通过arm汇编,自行调用syscall,直接操作系统内核,来绕过c层的hook,保证程序安全 所以想hook的话只能找到这些方法的c入口分别hook 可以通过查找bxls指令找到这些位置

  6. Java学习之线程通信(多线程(Lock))--生产者消费者

    SDK1.5版本以后对synchronized的升级,其思想是:将同步和锁封装成对象,其对象中包含操作锁的动作. 代码: //1.导入包 import java.util.concurrent.loc ...

  7. play framework 在idea简单运行配置(mac为例)

    文章目录 play 最基本的构建 在idea中配置 配置jdk相关 配置play 运行 运行 play 最基本的构建 https://blog.csdn.net/dataiyangu/article/ ...

  8. PAT_A1085#Perfect Sequence

    Source: PAT A1085 Perfect Sequence (25 分) Description: Given a sequence of positive integers and ano ...

  9. 实验报告(五)&第七周学习总结

    实验目的 理解抽象类与接口的使用: 了解包的作用,掌握包的设计方法. 实验要求 掌握使用抽象类的方法. 掌握使用系统接口的技术和创建自定义接口的方法. 了解 Java 系统包的结构. 掌握创建自定义包 ...

  10. 44-python基础-python3-字符串-常用字符串方法(二)-isalpha()-isalnum()-isdigit()-isspace()-istitle()

    3-isX 字符串方法   序号 方法 条件 返回结果1 返回结果2 1 isalpha() 如果字符串只包含字母,并且非空; True False 2 isalnum() 如果字符串只包含字母和数字 ...