[xiph-commits] r14906 - in branches/dir.xiph.org: . cronjobs inc

balbinus at svn.xiph.org balbinus at svn.xiph.org
Sat May 17 10:34:28 PDT 2008


Author: balbinus
Date: 2008-05-17 10:34:28 -0700 (Sat, 17 May 2008)
New Revision: 14906

Added:
   branches/dir.xiph.org/inc/lib.statslog.php
Modified:
   branches/dir.xiph.org/cronjobs/check_servers.php
   branches/dir.xiph.org/inc/class.server.php
   branches/dir.xiph.org/listen.php
Log:
Reworked listen.php, it's now using Server::retrieveByMountpointId, which now uses memcache. It also makes use of the new statsLog library (just moving code around so that it's more cleanly organized). And finally cleanup in check_servers.php (no behavior change).

Modified: branches/dir.xiph.org/cronjobs/check_servers.php
===================================================================
--- branches/dir.xiph.org/cronjobs/check_servers.php	2008-05-17 16:21:55 UTC (rev 14905)
+++ branches/dir.xiph.org/cronjobs/check_servers.php	2008-05-17 17:34:28 UTC (rev 14906)
@@ -10,15 +10,6 @@
 {
     throw new EnvironmentUndefinedException();
 }
-/*$lock_file = '/tmp/'.ENVIRONMENT.'_check_servers.lock';
-if (1 && file_exists($lock_file))
-{
-   die("Another instance is already running.\n");
-}
-else
-{
-    touch($lock_file);
-}*/
 
 // Database connection
 $db = DirXiphOrgDBC::getInstance();
@@ -139,6 +130,4 @@
     echo "OK.\n";
 }
 
-//unlink($lock_file);
-
 ?>

Modified: branches/dir.xiph.org/inc/class.server.php
===================================================================
--- branches/dir.xiph.org/inc/class.server.php	2008-05-17 16:21:55 UTC (rev 14905)
+++ branches/dir.xiph.org/inc/class.server.php	2008-05-17 17:34:28 UTC (rev 14906)
@@ -4,7 +4,7 @@
 {
     protected $server_id = 0;
     protected static $table_name = 'server';
-    protected $cache_expiration = 60;
+    protected static $cache_expiration = 60;
     public $loaded = false;
     
     protected $mountpoint_id;
@@ -125,31 +125,50 @@
         // MySQL Connection
 		$db = DirXiphOrgDBC::getInstance();
 		
-		try
+		// Memcache connection
+		$mc = DirXiphOrgMCC::getInstance();
+		
+		// Try to get from cache
+		$key = self::getListCacheKey('serversbympid_'.$mp_id);
+		$servers = $mc->get($key);
+		if (!$servers)
 		{
-		    $sql = "SELECT `id` FROM `%s` WHERE `mountpoint_id` = %d;";
-		    $sql = sprintf($sql, self::$table_name, $mp_id);
-		    $res = $db->selectQuery($sql);
+		    // Cache miss, hit the database
+		    try
+		    {
+		        $sql = "SELECT `id` FROM `%s` WHERE `mountpoint_id` = %d;";
+		        $sql = sprintf($sql, self::$table_name, $mp_id);
+		        $res = $db->selectQuery($sql);
+		        
+		        $servers = array();
+        		while (!$res->endOf())
+        		{
+        		    $servers[] = intval($res->current('id'));
+        		    $res->next();
+    		    }
+    		    
+    		    // Save into cache
+    		    $mc->set($key, $servers, false, self::$cache_expiration);
+	        }
+	        catch (SQLNoResultException $e)
+		    {
+		        return false;
+		    }
 	    }
-	    catch (SQLNoResultException $e)
-		{
-		    return false;
-		}
 		
+		// If we've been asked to load the servers data, do it
 		$data = array();
-		while (!$res->endOf())
+		if ($load_servers)
 		{
-		    if ($load_servers)
-		    {
-    		    $data[] = self::retrieveByPk(intval($res->current('id')));
+            foreach ($servers as $s)
+            {
+		        $data[] = self::retrieveByPk($s);
 		    }
-		    else
-		    {
-		        $data[] = intval($res->current('id'));
-		    }
-		    
-		    $res->next();
-		}
+	    }
+	    else
+	    {
+	        $data =& $servers;
+	    }
 		
 		return $data;
     }
@@ -280,7 +299,7 @@
         
         $a = $this->__toArray();
         
-        return $cache->set($this->getCacheKey(), $a, false, $this->cache_expiration);
+        return $cache->set($this->getCacheKey(), $a, false, self::$cache_expiration);
     }
     
     /**
@@ -317,7 +336,7 @@
     }
     
     /**
-     * Builds a cache key for this mountpoint.
+     * Builds a cache key for this server.
      * 
      * @return string
      */
@@ -332,6 +351,21 @@
     }
     
     /**
+     * Builds a cache key for a list of servers.
+     * 
+     * @return string
+     */
+    protected static function getListCacheKey($list_id)
+    {
+        if (!defined('ENVIRONMENT'))
+        {
+            throw new EnvironmentUndefinedException();
+        }
+        
+        return sprintf("%s_serverlist_%s", ENVIRONMENT, jenkins_hash_hex($list_id));
+    }
+    
+    /**
      * Serializes the data into an array
      * 
      * @return array

Added: branches/dir.xiph.org/inc/lib.statslog.php
===================================================================
--- branches/dir.xiph.org/inc/lib.statslog.php	                        (rev 0)
+++ branches/dir.xiph.org/inc/lib.statslog.php	2008-05-17 17:34:28 UTC (rev 14906)
@@ -0,0 +1,22 @@
+<?php
+
+class statsLog
+{
+    public static function playlistAccessed($mountpoint_id, $stream_name)
+    {
+        $db = DirXiphOrgLogDBC::getInstance();
+
+        $ip = utils::getRealIp();
+        $ip = $ip !== false ? $ip : '127.0.0.1';
+
+        $db = DirXiphOrgLogDBC::getInstance();
+        $sql = "INSERT INTO `playlist_log_%s` (`mountpoint_id`, `stream_name_hash`, `accessed_by`) "
+              ."VALUES (%d, '%s', INET_ATON('%s'));";
+        $sql = sprintf($sql, date('Ymd'), $mountpoint_id,
+                        $db->escape(hash('md5', $stream_name)),
+                        $db->escape($ip));
+        $db->query($sql);
+    }
+}
+
+?>

Modified: branches/dir.xiph.org/listen.php
===================================================================
--- branches/dir.xiph.org/listen.php	2008-05-17 16:21:55 UTC (rev 14905)
+++ branches/dir.xiph.org/listen.php	2008-05-17 17:34:28 UTC (rev 14906)
@@ -69,57 +69,37 @@
 // Memcache connection
 $memcache = DirXiphOrgMCC::getInstance();
 
-// Check the memcache server
-$playlist = $memcache->get(ENVIRONMENT.'_playlist_'.$p_id);
-if ($playlist === false)
+// Get the servers associated with this mountpoint
+$servers = Server::retrieveByMountpointId($this->mountpoint_id, false);
+
+// Build the playlist
+$playlist = array();
+if ($servers !== false && $servers !== array())
 {
-	// Database connection
-	$db = DirXiphOrgDBC::getInstance();
-	
-	// Cache miss, query the database
-	$query = 'SELECT `listen_url` FROM `server` WHERE `mountpoint_id` = %d;';
-	$query = sprintf($query, $p_id);
-	
-	try
-	{
-		$result = $db->selectQuery($query);
-		$playlist = array();
-		
-		while (!$result->endOf())
-		{
-			$playlist[] = $result->current('listen_url');
-			$result->next();
-		}
-		
-		// Store into memcache
-		$memcache->set(ENVIRONMENT.'_playlist_'.$p_id, $playlist, 60);
-	}
-	catch (SQLNoResultException $e)
-	{
-		header('HTTP/1.1 404 Not Found', true);
-		die("No such PID.\n");
-	}
+    foreach ($servers as $s)
+    {
+		$playlist[] = $s->getListenUrl();
+    }
+}		
+else
+{
+    header('HTTP/1.1 404 Not Found', true);
+    die("No such PID.\n");
 }
 
 // Logging
 try
 {
-	$mountpoint = Mountpoint::retrieveByPk($p_id);
-	if ($mountpoint instanceOf Mountpoint)
-	{
-		$sn = $mountpoint->getStreamName();
-		$db = DirXiphOrgLogDBC::getInstance();
-		$sql = "INSERT INTO `playlist_log_%s` (`mountpoint_id`, `stream_name_hash`, `accessed_by`) "
-		."VALUES (%d, '%s', INET_ATON('%s'));";
-		$sql = sprintf($sql, date('Ymd'), $p_id,
-				$db->escape(hash('md5', $sn)),
-				$db->escape(utils::getRealIp()));
-		$db->query($sql);
-	}
+    $mountpoint = Mountpoint::retrieveByPk($p_id);
+    if ($mountpoint instanceOf Mountpoint)
+    {
+        $sn = $mountpoint->getStreamName();
+        statsLog::playlistAccessed($p_id, $sn);
+    }
 }
 catch (SQLException $e)
 {
-	// Nothing to do, it's just logging after all...
+    // Nothing to do, it's just logging after all...
 }
 
 /******************************************************************************/



More information about the commits mailing list