With an OO language I often use a factory design pattern to connect to a DB:
// Connect to Redis ( PHP )
protected function get_redis() {
static $redis = false;
if( $redis === false ) {
$redis = new Predis\Client(Configure::read('redis.db'));
}
return $redis;
}
I done quite a lot of C but never any big projects. I want to know how to connect to a Redis DB (or any DB) in C without opening more than one connection. I know I can use a global variable but is there a better way ? I don’t want to force the factory pattern – it’s OO. Has anyone got any examples ?
Many thanks.
I am going to disagree with you here. Factory method does not necessarily put an OO stamp on your code. It’s just a pattern for creating things.
If you don’t want to create more than one connection, you can control that in the
get_connectionfunction. This is another application for this pattern.