• share memory cache across multi web application


    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!

  • 相关阅读:
    2月4日进度
    每日总结3-6
    每日总结3-5
    每日总结3-4
    每日总结3-2
    本周计划
    本周计划
    假期每日总结2-13
    假期每日总结2-12
    假期每日总结2-11
  • 原文地址:https://www.cnblogs.com/chucklu/p/10833168.html
Copyright © 2020-2023  润新知