Last active
February 19, 2023 22:06
-
-
Save FaisalAbid/eda31448d3acbc21dd5e to your computer and use it in GitHub Desktop.
Redis Dart Connection Pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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