Redis返回数据乱码解决方案(redis返回数据乱码)

Recently,some of my friends had the issue when using Redis in their projects. They found that the data returns from Redis were garbled, which is a headache. Let’s take a look at the solution to this problem.

First and foremost, we need to understand the reason why the data is garbled. Generally speaking, this garbled problem occurs when the data is encoded in different coding. For example, if the data obtned from the database is encoded in UTF-8 and you decode it with GBK, it will show the garbled characters.

In order to solve this problem, it is important to understand the coding of the data. To do this, we need to modify the configuration file of Redis which is redis.conf. This file is located in the installation directory of Redis; find and open it with a text editor. Then, find the line “# character-set-server=” in the configuration file, and set the correct coding behind it. Save the configuration file and restart Redis.

Here, I would like to give an example of how to set the coding in redis.conf. For example, if you want to set Redis’s encoding to GBK, you need to add “character-set-server=GBK” in redis.conf. After that, save the configuration file,and restart Redis.

Besides, if you are using a language like Java, you can also use its own coding tools to parse the data and decode it in the coding you would like to use. Here is an example of the code. The following code decode the garbled data from Redis to the set coding “UTF-8”.

String result = new String(value.getBytes(“ISO-8859-1″),”utf-8”);

In conclusion, the problem of garbled data from Redis can be easily solved by setting the correct coding when reading the data. It can also be solved by using the language’s own coding tools to decode the data.


数据运维技术 » Redis返回数据乱码解决方案(redis返回数据乱码)