In PHP, if I create a singleton method for 5 different caching classes (memcache, apc, session, file, cookie)
Then I have a main cache class that basically routes the set () and get () methods to the corresponding cache class. Now let's say that I need to use the session, cookie, memcache and cache of files on one page. Then my main cache class will need to create a new instance 1 time for each of these types of caching using singleton code.
SO I would then need to repeatedly call my singleton methods on the page, if I were to set / get 30 different calls to the cache on 1 page, it would repeatedly call the singleton method.
I wonder if this is bad practice or is it not good to keep calling my singleton again and again on the page?
UPDATE
Below is the code I started, in it you can get a better example of what I'm trying to do ... If I had to add something to memcache 40 times on the page, it would call the singleton method for ym class memcache 40 times
public function set($type, $keys, $value = FALSE, $options_arr)
{
if (empty($keys))
return FALSE;
if ( ! is_array($keys))
{
$keys = array($keys => $val);
}
switch ($type) {
case "memcache":
$this->memcache = Memcache::getInstance();
$this->memcache->get($keys, $value, $options);
break;
case "apc":
$this->apc = APC::getInstance();
$this->apc->get($keys, $value, $options);
break;
case "session":
foreach ($keys as $key => $val)
{
$_SESSION[$key] = $val;
}
break;
case "cookie":
break;
case "file":
break;
}
}
source
share