Selaa lähdekoodia

Use a cache. Provide a pre-cache service.

Zachary Kotlarek 12 vuotta sitten
vanhempi
sitoutus
13b7fa4258
4 muutettua tiedostoa jossa 37 lisäystä ja 5 poistoa
  1. 4 2
      web/includes/config.php
  2. 22 2
      web/includes/series.php
  3. 10 0
      web/precache.php
  4. 1 1
      web/seasonMaint.php

+ 4 - 2
web/includes/config.php

@@ -1,8 +1,10 @@
 <?
 
 ### Config
-$TV_PATH       = '/mnt/media/TV';	# Root path for all TV series
-$MAX_AGE       = 86400 * 90;		# Consider a series "old" if it has not been updated for this many seconds
+$TV_PATH    = '/mnt/media/TV';	# Root path for all TV series
+$MAX_AGE    = 86400 * 90;		# Consider a series "old" if it has not been updated for this many seconds
+$CACHE_AGE  = 300;
+$CACHE_FILE = '/var/tmp/php/tv_web.cache';
 
 ### TVDB
 $ENABLE_TVDB      = true;	# Master enable/disable flag

+ 22 - 2
web/includes/series.php

@@ -12,14 +12,34 @@ function seriesPath($series) {
 }
 
 # Find all series and seasons under a given path
-function allSeriesSeasons($base) {
+function allSeriesSeasons($base, $use_cache = true) {
+	global $CACHE_AGE;
+	global $CACHE_FILE;
 	$retval = array();
-	$all_series = allSeries($base);
 
+	# Use the cache file if it exists, is fresh and contains at least 1 series
+	if ($use_cache && file_exists($CACHE_FILE)) {
+		if (filemtime($CACHE_FILE) > time() - $CACHE_AGE) {
+			$retval = @unserialize(@file_get_contents($CACHE_FILE));
+			if (is_array($retval) && count($retval) > 0) {
+				return $retval;
+			}
+		}
+	}
+
+	# Read live data from the disk
+	$all_series = allSeries($base);
 	foreach ($all_series as $series) {
 		$retval[ $series ] = findSeasons($series);
 	}
 
+	# Always update the cache if we read from disk
+	$temp = @tempnam(dirname($CACHE_FILE), 'tv_web.cache');
+	if ($temp) {
+		file_put_contents($temp, serialize($retval));
+		rename($temp, $CACHE_FILE);
+	}
+
 	return $retval;
 }
 

+ 10 - 0
web/precache.php

@@ -0,0 +1,10 @@
+#!/usr/local/bin/php
+<?
+
+set_time_limit(0);
+require_once 'includes/main.php';
+
+global $TV_PATH;
+allSeriesSeasons($TV_PATH, false);
+
+?>

+ 1 - 1
web/seasonMaint.php

@@ -5,7 +5,7 @@ set_time_limit(0);
 require_once 'includes/main.php';
 
 global $TV_PATH;
-$all_series = allSeriesSeasons($TV_PATH);
+$all_series = allSeriesSeasons($TV_PATH, false);
 $old_series = array();
 $added_seasons = array();
 $multi_seasons = array();