move memcache connections to util.php

darcs-hash:20080926200102-5ed1f-debbc18981424d7724277ac82e5fbfd5e797090e.gz
This commit is contained in:
Evan Prodromou 2008-09-26 16:01:02 -04:00
parent 15c0fb7abd
commit 82084ab74c
4 changed files with 26 additions and 20 deletions

View File

@ -69,20 +69,7 @@ class Memcached_DataObject extends DB_DataObject
}
static function memcache() {
if (!common_config('memcached', 'enabled')) {
return NULL;
} else {
$cache = new Memcache();
$servers = common_config('memcached', 'server');
if (is_array($servers)) {
foreach($servers as $server) {
$cache->addServer($server);
}
} else {
$cache->addServer($servers);
}
return $cache;
}
return common_memcache();
}
static function cacheKey($cls, $k, $v) {

View File

@ -125,8 +125,8 @@ class Notice extends Memcached_DataObject
}
function blowSubsCache() {
$cache = new Memcache();
if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
$cache = common_memcache();
if ($cache) {
$user = new User();
$user->query('SELECT id ' .

View File

@ -196,10 +196,9 @@ class User extends Memcached_DataObject
function noticesWithFriendsWindow() {
$cache = new Memcache();
$res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
$cache = common_memcache();
if (!$res) {
if (!$cache) {
return NULL;
}

View File

@ -1727,4 +1727,24 @@ function common_message_form($content, $user, $to) {
common_element_end('p');
common_element_end('form');
}
}
function common_memcache() {
static $cache = NULL;
if (!common_config('memcached', 'enabled')) {
return NULL;
} else {
if (!$cache) {
$cache = new Memcache();
$servers = common_config('memcached', 'server');
if (is_array($servers)) {
foreach($servers as $server) {
$cache->addServer($server);
}
} else {
$cache->addServer($servers);
}
}
return $cache;
}
}