Skip to content

Instantly share code, notes, and snippets.

@FaisalAbid
Last active February 19, 2023 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FaisalAbid/eda31448d3acbc21dd5e to your computer and use it in GitHub Desktop.
Save FaisalAbid/eda31448d3acbc21dd5e to your computer and use it in GitHub Desktop.
Redis Dart Connection Pool
library db.redis;
import 'dart:async';
import 'package:redis_client/redis_client.dart';
import 'package:connection_pool/connection_pool.dart';
class Redis {
static Future<RedisClient> getRedis() async {
ManagedConnection managedConnection = await new RedisPool().getConnection();
return managedConnection.conn;
}
}
/// Redis pool to manage connections
class RedisPool extends ConnectionPool<RedisClient> {
static int poolSize = 10;
static var connectionString = "";
@override
void closeConnection(RedisClient conn) {
print("recycle connection");
conn.close();
}
@override
Future<RedisClient> openNewConnection() async{
print("new connection");
RedisClient r = await RedisClient.connect(connectionString);
return r;
}
static final RedisPool _singleton = new RedisPool._internal();
factory RedisPool() {
return _singleton;
}
RedisPool._internal() : super(poolSize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment