1 module hunt.framework.provider.RedisServiceProvider;
2 
3 import hunt.framework.provider.ServiceProvider;
4 import hunt.framework.config.ApplicationConfig;
5 
6 import hunt.collection.HashSet;
7 import hunt.collection.Set;
8 import hunt.logging;
9 import hunt.redis;
10 import poodinis;
11 
12 import core.time;
13 
14 import std.conv;
15 import std.string;
16 
17 /**
18  * 
19  */
20 class RedisServiceProvider : ServiceProvider {
21 
22     override void register() {
23         // container.register!(RedisPoolOptions)().singleInstance();
24 
25         container.register!RedisPool.initializedBy({
26             ApplicationConfig config = container.resolve!ApplicationConfig();
27 
28             auto redisOptions = config.redis;
29             auto redisPoolOptions = redisOptions.pool;
30 
31             if (redisOptions.enabled) {
32                 RedisPoolOptions poolConfig = new RedisPoolOptions();
33                 poolConfig.host = redisOptions.host;
34                 poolConfig.port = cast(int) redisOptions.port;
35                 poolConfig.password = redisOptions.password;
36                 poolConfig.database = cast(int) redisOptions.database;
37                 poolConfig.soTimeout = cast(int) redisPoolOptions.idleTimeout;
38                 poolConfig.connectionTimeout =  redisOptions.timeout;
39                 poolConfig.size = redisPoolOptions.maxPoolSize;
40                 poolConfig.maxWaitQueueSize = redisPoolOptions.maxWaitQueueSize;
41                 poolConfig.waitTimeout = msecs(redisPoolOptions.waitTimeout);
42 
43                 infof("Initializing RedisPool: %s", poolConfig.toString());
44 
45                 RedisFactory factory = new RedisFactory(poolConfig);
46 
47                 return factory.pool(); 
48             } else {
49                 // warning("RedisPool has been disabled.");
50                 // return new RedisPool();
51                 throw new Exception("The Redis is disabled.");
52             }
53         }).singleInstance();
54 
55         container.register!RedisCluster.initializedBy({
56             ApplicationConfig config = container.resolve!ApplicationConfig();
57 
58             auto redisOptions = config.redis;
59             auto redisPoolOptions = redisOptions.pool;
60             auto clusterOptions = config.redis.cluster;
61 
62             if(clusterOptions.enabled) {
63 
64                 RedisPoolOptions poolConfig = new RedisPoolOptions();
65                 poolConfig.host = redisOptions.host;
66                 poolConfig.port = cast(int) redisOptions.port;
67                 poolConfig.password = redisOptions.password;
68                 poolConfig.database = cast(int) redisOptions.database;
69                 poolConfig.soTimeout = cast(int) redisOptions.timeout;
70                 poolConfig.connectionTimeout =  redisOptions.timeout;
71                 poolConfig.waitTimeout = msecs(redisPoolOptions.waitTimeout);
72                 poolConfig.size = redisPoolOptions.maxPoolSize;
73 
74                 infof("Initializing RedisCluster: %s", poolConfig.toString());  
75 
76                 string[] hostPorts = clusterOptions.nodes;
77                 HostAndPort[] clusterNode;
78                 foreach(string item; hostPorts) {
79                     string[] hostPort = item.split(":");
80                     if(hostPort.length < 2) {
81                         warningf("Wrong host and port: %s", item);
82                         continue;
83                     }
84 
85                     version(HUNT_DEBUG) {
86                         tracef("Cluster host: %s", hostPort);
87                     }
88 
89                     try {
90                         int port = to!int(hostPort[1]);
91                         clusterNode ~= new HostAndPort(hostPort[0], port);
92                     } catch(Exception ex) {
93                         warning(ex);
94                     }
95                 }
96 
97                 RedisCluster jc = new RedisCluster(clusterNode, poolConfig);
98 
99                 return jc;
100 
101             } else {
102                 throw new Exception("The RedisCluster is disabled.");
103             }
104 
105         // }).newInstance();
106         }).singleInstance();
107     }
108 }