为什么要用redis而不用map做缓存?
PHP中文网
PHP中文网 2017-04-28 09:04:05
0
5
1222

为什么要使用redis来做缓存呢?
redis是key-value的形式,map也是,可以用Map替代redis吗?
如果不能替代,有什么场景或需求是只有redis能实现的呢?

PHP中文网
PHP中文网

认证0级讲师

reply all(5)
过去多啦不再A梦

Cache is divided into local cache and distributed cache. Taking Java as an example, local caching is implemented using the built-in map or guava. The main feature is that it is lightweight and fast. The life cycle ends with the destruction of the jvm, and in the case of multiple instances, each instance Each cache needs to be saved, and the cache is not consistent.

Using redis or memcached is called distributed cache. In the case of multiple instances, each instance shares a cache of data, and the cache is consistent. The disadvantage is that the redis or memcached service needs to be kept highly available, and the entire program architecture is relatively complex.

我想大声告诉你
  1. Redis can use dozens of gigabytes of memory for caching, but Map cannot. Generally, JVM only needs a few gigabytes of data to be large enough

  2. Redis’ cache can be persisted, Map is a memory object, and the data will be gone as soon as the program is restarted

  3. Redis can implement distributed caching, and Map can only exist in the program that created it

  4. Redis can handle millions of concurrency per second and is a professional caching service. Map is just an ordinary object

  5. Redis cache has an expiration mechanism, Map itself does not have this function

  6. Redis has rich API, Map is much simpler

phpcn_u1582

1. Redis data can be saved persistently. If you want to continue using some caches after restarting the program, then map cannot achieve it.
2. Redis can achieve distributed deployment. As long as multiple machines and processes are involved, map cannot achieve it.
3. Redis has many data structures that are easy to operate, such as hash set list sort-set, etc. In some scenarios, it is more convenient to operate than map

阿神

1. If your cache needs to load a lot of content, it will take a long time when you start it;
2. The JVM memory is too large and it is easy to hang;
3. redis is written in C, which has better stability and performance;
4. The current redis already supports cluster mode, persistence and more features;
5. The API of redis is already very simple and easy to get started with;

Ty80

Redis can be deployed independently, so that the data cached by redis will still be there after the website code is updated, and the local memory will be released every time the website is updated.
The data is stored in redis, and the cached data can be shared between multiple projects. If it is local memory, it cannot be cross-project. Shared

Local cache is not easy to view and modify. Redis has rich tools to manage cache data

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template