From 82084ab74c4050620d69a90cbacc33383a0b7e5f Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 26 Sep 2008 16:01:02 -0400 Subject: [PATCH] move memcache connections to util.php darcs-hash:20080926200102-5ed1f-debbc18981424d7724277ac82e5fbfd5e797090e.gz --- classes/Memcached_DataObject.php | 15 +-------------- classes/Notice.php | 4 ++-- classes/User.php | 5 ++--- lib/util.php | 22 +++++++++++++++++++++- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php index 7ca2c7db21..84529ce9a6 100644 --- a/classes/Memcached_DataObject.php +++ b/classes/Memcached_DataObject.php @@ -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) { diff --git a/classes/Notice.php b/classes/Notice.php index f3debe7420..b42a8726e3 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -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 ' . diff --git a/classes/User.php b/classes/User.php index 94fcff9c8e..de260035ab 100644 --- a/classes/User.php +++ b/classes/User.php @@ -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; } diff --git a/lib/util.php b/lib/util.php index 621fdc1371..20fe4ced67 100644 --- a/lib/util.php +++ b/lib/util.php @@ -1727,4 +1727,24 @@ function common_message_form($content, $user, $to) { common_element_end('p'); common_element_end('form'); -} \ No newline at end of file +} + +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; + } +} -- 2.39.5