What are the advantages of Memcached compared to .NET Cache system?

前端 未结 2 1212
日久生厌
日久生厌 2021-02-20 02:03

I saw some people in a development team using Memcached to cache database results, instead of built-in .NET Framework Cache system. I do not understand clearly why would they us

2条回答
  •  生来不讨喜
    2021-02-20 02:30

    Memcached is distributed - crucially this means that if I have a cluster of servers accessing the cache, all of them are essentially reading from and writing to the same cache. The built in .Net cache does not have this feature (at least I know the ASP.Net one doesn't).

    As each machine has its own independent cache this means that (for example):

    • If machine A computes / reads a value and places it in the cache, machine B doesn't get any benefit from this. Each machine needs to independently maintain and fill its own cache. This also potentially wastes space as the same entry may be present in the cache of many different machines.
    • If machine B invalidates a cache entry, machine A won't be aware of this invalidation and may continue to use stale / out of date data

    Memcached has neither of those problems - once an entry is placed in the cache all machines in the cluster can retrieve the same cached item. Invalidating an entry in the cache invalidates it for everyone.

    Disadvantages

    If your application needs to function on a cluster of machines then it is very likely that you will benefit from a distributed cache, however if your application only needs to run on a single machine then you won't gain any benefit from using a distributed cache and will probably be better off using the built-in .Net cache.

    • Accessing a memcached cache requires interprocess / network communication, which will have a small performance penalty over the .Net caches which are in-process.
    • Memcached works as an external process / service, which means that you need to install / run that service in your production environment. Again the .Net caches don't need this step as they are hosted in-process.

提交回复
热议问题