Files
easy_dart_module/lib/base/redis/redis.dart
2025-02-28 18:36:49 +08:00

48 lines
986 B
Dart

import 'package:redis/redis.dart';
class Redis {
final RedisConfig _config;
Command? _command;
Redis(this._config);
static late Redis _redis;
static Redis getInstance() {
return _redis;
}
static void setInstance(Redis redis) {
_redis = redis;
}
Future<void> connect() async {
_command = await RedisConnection().connect(_config.host, _config.port);
}
Future<bool> set(String key, String value) async {
var response = await _command?.send_object(["SET", key, value]);
return response == "OK";
}
Future<String?> get(String key) async {
return await _command?.send_object(["GET", key]);
}
Future<bool> setWithExpiry(String key, String value, int ttlInSeconds) async {
var response = await _command
?.send_object(["SETEX", key, ttlInSeconds.toString(), value]);
return response == "OK";
}
}
class RedisConfig {
String host;
int port;
RedisConfig({
required this.host,
this.port = 6379,
});
}