Redis has a configuration option that writes the content of the database to disk, and when it restarts, load the data from disk into the database. The details on this option are in the docs here: https://redis.io/topics/persistence
If you need some data to always be present in Redis, then you'll either need to implement persistence above, or do something like this in your app:
# when retrieving something from Redis cache
if (item_is_in_cache('my_key') { #inexpensive operation
retrieve_item_from_cache('my_key'); #inexpensive operation
} else {
store_important_data_in_cache(); #expensive operation
}
What this pseudo-code does is first check that the required data is in the cache, and retrieve it if it is. Checking and retrieving data from a Redis cache is an inexpensive operation, meaning the resources required are low. If the data isn't in the cache (ie, the Redis server recently started), we have to put the important data in the cache. This can be an expensive operation (more resources used than checking/retrieving data) depending on the amount of data required.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…