Config列表内容["redis_host = 127.0.0.1","redis_port = 6379"]
时间: 2023-11-11 08:06:50 浏览: 44
问题出在 Config 列表的元素格式上。在 Erlang 中,元素应该是由键和值组成的元组。因此,你需要将 Config 列表中的每个元素转换成元组,再执行 keyfind 函数。下面是一个例子:
```
Config = [{"redis_host", "127.0.0.1"}, {"redis_port", "6379"}],
{ok, RedisHost} = lists:keyfind("redis_host", 1, Config).
```
这样就可以正确地找到 "redis_host" 键名对应的值,赋值给 RedisHost 变量。
相关问题
redis.config文件的数据是通过以下代码写进去的redis_write() -> %% 设置Redis的地址和端口 Address = "127.0.0.1", Port = 6379, %% 将Redis的地址和端口写入redis.config文件 {ok, File} = file:open("redis.config", [write]), io:fwrite(File, "redis_host = ~s~n", [Address]), io:fwrite(File, "redis_port = ~B~n", [Port]), file:close(File).
根据您提供的代码,`redis_write()` 函数的作用是将 Redis 服务器的主机名和端口号写入到 `redis.config` 文件中。通过这个函数写入到 `redis.config` 文件中的数据应该是符合 `redis_read()` 函数期望的格式的。
在这种情况下,您可以检查一下以下几点来解决问题:
1. 确保 `redis.config` 文件存在,并且数据已经成功地写入到文件中。
2. 确保 `redis_read()` 函数中解析 `redis.config` 文件的代码正确,并且没有其他语法错误或者格式错误。
3. 确保 Redis 服务器已经正确地安装和配置,并且正在运行。
如果您已经检查了上述问题,还是无法解决问题,可以尝试在代码中添加一些调试信息,以便更好地定位问题。例如,您可以在读取 `redis.config` 文件之前打印一下文件的路径,以确保它是正确的。
flask_redis如何设置decode_responses=True
在使用 Flask-Redis 时,可以通过在创建 Redis 实例时设置 `decode_responses=True` 来实现自动解码 Redis 中的数据。具体的做法是,在 Flask 的配置中增加以下项:
```python
REDIS_SETTINGS = {
'host': '127.0.0.1',
'port': 6379,
'db': 0,
'decode_responses': True,
}
app = Flask(__name__)
app.config['REDIS_SETTINGS'] = REDIS_SETTINGS
redis_store = FlaskRedis(app)
```
其中,`decode_responses=True` 的作用是将 Redis 中的数据解码成字符串,这样可以直接使用字符串操作 Redis 中的数据,而不需要手动进行解码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)