Skip to content

Commit

Permalink
Merge pull request #62 from stevevega/reuse-cache-conn
Browse files Browse the repository at this point in the history
Reuse cache connection
  • Loading branch information
kenntchan authored Sep 7, 2021
2 parents 55be8a7 + 7646980 commit 950e17b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions library/Kima/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ class Cache
const DEFAULT_KEY = 'default';
const ENABLED = 'enabled';

/**
* Connection pool
*/
private static $instances = [];

/**
* private construct
*/
private function __construct() {}
private function __construct() {}

/**
* Get an instance of the required cache system
Expand All @@ -62,35 +67,45 @@ public static function get_instance($type = null, array $options = [])
return new NullObject($options);
}

// reuse connection if possible
if (isset($type) && isset(self::$instances[$type])) {
return self::$instances[$type];
}

$instance = null;
switch ($type) {
case self::DEFAULT_KEY:
case '':
case null:
if (isset($options[self::DEFAULT_KEY])) {
return self::get_instance($options[self::DEFAULT_KEY], $options);
$instance = self::get_instance($options[self::DEFAULT_KEY], $options);
} else {
Error::set(self::ERROR_DEFAULT_NOT_SET, false);
}
break;
case self::APC:
return new Apc($options);
$instance = new Apc($options);
break;
case self::FILE:
return new File($options);
$instance = new File($options);
break;
case self::MEMCACHED:
return new Memcached($options);
$instance = new Memcached($options);
break;
case self::REDIS:
return new Redis($options);
$instance = new Redis($options);
break;
case self::NULL_OBJECT:
return new NullObject($options);
$instance = new NullObject($options);
break;
default:
Error::set(sprintf(self::ERROR_INVALID_CACHE_SYSTEM, $type));
break;
}

self::$instances[$type] = $instance;

return $instance;
}

}

0 comments on commit 950e17b

Please sign in to comment.