新增多语言转换 mqtt支持websocket
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:redis/redis.dart';
|
||||
class Redis {
|
||||
final RedisConfig _config;
|
||||
Command? _command;
|
||||
bool _connected = false;
|
||||
|
||||
Redis(this._config);
|
||||
|
||||
@@ -16,7 +17,12 @@ class Redis {
|
||||
_redis = redis;
|
||||
}
|
||||
|
||||
bool isConnected() {
|
||||
return this._connected;
|
||||
}
|
||||
|
||||
Future<void> connect({reconnect = false}) async {
|
||||
_connected = false;
|
||||
if (reconnect) {
|
||||
print("尝试重连Redis");
|
||||
}
|
||||
@@ -24,6 +30,7 @@ class Redis {
|
||||
try {
|
||||
_command = await RedisConnection().connect(_config.host, _config.port);
|
||||
print('Redis Connected successfully!');
|
||||
_connected = true;
|
||||
//定时检测是否断开连接
|
||||
Future.delayed(Duration(seconds: 1), () async {
|
||||
do {
|
||||
@@ -54,22 +61,42 @@ class Redis {
|
||||
}
|
||||
|
||||
Future<bool> set(String key, String value) async {
|
||||
var response = await _command?.send_object(["SET", key, value]);
|
||||
return response == "OK";
|
||||
try {
|
||||
var response = await _command?.send_object(["SET", key, value]);
|
||||
return response == "OK";
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> get(String key) async {
|
||||
return await _command?.send_object(["GET", key]);
|
||||
try {
|
||||
return await _command?.send_object(["GET", key]);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> delete(String key) async {
|
||||
return await _command?.send_object(["DEL", key]) == "OK";
|
||||
try {
|
||||
return await _command?.send_object(["DEL", key]) == "OK";
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> setWithExpiry(String key, String value, int ttlInSeconds) async {
|
||||
var response = await _command
|
||||
?.send_object(["SETEX", key, ttlInSeconds.toString(), value]);
|
||||
return response == "OK";
|
||||
try {
|
||||
var response = await _command
|
||||
?.send_object(["SETEX", key, ttlInSeconds.toString(), value]);
|
||||
return response == "OK";
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user