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 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;
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
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.
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
Redis’ cache can be persisted, Map is a memory object, and the data will be gone as soon as the program is restarted
Redis can implement distributed caching, and Map can only exist in the program that created it
Redis can handle millions of concurrency per second and is a professional caching service. Map is just an ordinary object
Redis cache has an expiration mechanism, Map itself does not have this function
Redis has rich API, Map is much simpler
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;
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