Browse Source

replace memcached stuff with redis

Andrew Dolgov 6 months ago
parent
commit
c7a3d2f1ef
2 changed files with 32 additions and 41 deletions
  1. 32 39
      backend.php
  2. 0 2
      config.php-dist

+ 32 - 39
backend.php

@@ -836,24 +836,21 @@
 		break;
 
 	case "urlmetadata":
-		if (defined('FETCH_URL_TITLES') && FETCH_URL_TITLES && class_exists("Memcached")) {
+		if (defined('FETCH_URL_TITLES') && FETCH_URL_TITLES) {
 
-			$srv = explode(":", MEMCACHE_SERVER, 2);
-
-			$memcache = new Memcached();
-			$memcache->addServer($srv[0], $srv[1]);
+			global $redis;
 
 			$base_url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 			$url = rewrite_relative_url($base_url, $_REQUEST["url"]);
-			$key = 'ttirc.urlcache:' . sha1($url);
-			$req_key = 'ttirc.urlcache-req:' . sha1($url);
+			$key = 'ttirc_urlcache,' . sha1($url);
+			$req_key = 'ttirc_urlcache-req,' . sha1($url);
 			$nocache = (bool) $_REQUEST["nocache"];
 
-			if (!$nocache && !$rv = $memcache->get($key)) {
+			if (!$nocache && !$rv = $redis->get($key)) {
 
 				$tries = 0;
 
-				while ($tries < PROXY_CONCUR_TRIES && $rc = $memcache->get($req_key)) {
+				while ($tries < PROXY_CONCUR_TRIES && $rc = $redis->get($req_key)) {
 					if (!$rc || $rc == 2)
 						break;
 
@@ -862,7 +859,7 @@
 				}
 			}
 
-			if ($nocache || !$rv = $memcache->get($key)) {
+			if ($nocache || !$rv = $redis->get($key)) {
 
 				$rv = [ ];
 
@@ -880,7 +877,8 @@
 					$options['http']['proxy'] = _HTTP_PROXY;
 				}
 
-				$memcache->set($req_key, PROXY_REQ_PROGRESS, CACHE_LIFETIME_MAX);
+				$redis->set($req_key, PROXY_REQ_PROGRESS);
+				$redis->expire($req_key, CACHE_LIFETIME_MAX);
 
 				if (strpos($url, "//") === 0) $url = "https:" . $url;
 
@@ -937,13 +935,17 @@
 							$rv[$k] = mb_substr($v, 0, 1024);
 						}
 
-						if ($rv['title'])
-							$memcache->set($key, $rv, CACHE_LIFETIME_MAX);
+						if ($rv['title']) {
+							$redis->set($key, $rv);
+							$redis->expire($key, CACHE_LIFETIME_MAX);
+						}
 					}
 
-					$memcache->set($req_key, PROXY_REQ_OK, CACHE_LIFETIME_MAX);
+					$redis->set($req_key, PROXY_REQ_OK);
+					$redis->expire($req_key, CACHE_LIFETIME_MAX);
 				} else {
-					$memcache->set($req_key, PROXY_REQ_FAILED, CACHE_LIFETIME_MAX);
+					$redis->set($req_key, PROXY_REQ_FAILED);
+					$redis->expire($req_key, CACHE_LIFETIME_MAX);
 				}
 			}
 
@@ -965,6 +967,8 @@
 
 	case "imgproxy";
 
+		global $redis;
+
 		$base_url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 		$url = rewrite_relative_url($base_url, $_REQUEST["url"]);
 		@$resize = (int) $_REQUEST['resize'];
@@ -1003,32 +1007,18 @@
 		$cache_key = "cache/imgproxy/" . sha1($url) . ($resize ? "-$resize" : "") . ".jpg";
 		$cache_key_fsize = "cache/imgproxy/" . sha1($url) . ".jpg";
 
-		$mem_key = 'ttirc.imgcache:' . sha1($cache_key);
-
-		$memcache = false;
-
-		if (class_exists("Memcached") && defined('MEMCACHE_SERVER') && MEMCACHE_SERVER) {
-			$srv = explode(":", MEMCACHE_SERVER, 2);
-
-			$memcache = new Memcached();
-			$memcache->addServer($srv[0], $srv[1]);
-		}
+		$mem_key = 'ttirc_imgcache,' . sha1($cache_key);
 
 		if (!file_exists($cache_key) || filesize($cache_key) == 0) {
 
-			if ($memcache) {
-				$tries = 0;
-
-				while ($tries < PROXY_CONCUR_TRIES && $rc = $memcache->get($mem_key)) {
-					if (!$rc || $rc == 2)
-						break;
+			$tries = 0;
 
-					$tries++;
-					sleep(1);
-				}
+			while ($tries < PROXY_CONCUR_TRIES && $rc = $redis->get($mem_key)) {
+				if (!$rc || $rc == 2)
+					break;
 
-			} else {
-				sleep(rand(1,3));
+				$tries++;
+				sleep(1);
 			}
 		}
 
@@ -1057,7 +1047,8 @@
 			}
 		} else {
 
-			if ($memcache) $memcache->set($mem_key, PROXY_REQ_PROGRESS, CACHE_LIFETIME_MAX);
+			$redis->set($mem_key, PROXY_REQ_PROGRESS);
+			$redis->expire($mem_key, CACHE_LIFETIME_MAX);
 
 			$options = ['http'=> [
 				'protocol_version'=> 1.1,
@@ -1120,11 +1111,13 @@
 					header("Content-Disposition: inline; filename=\"$req_filename\"");
 				}
 
-				if ($memcache) $memcache->set($mem_key, PROXY_REQ_OK, CACHE_LIFETIME_MAX);
+				$redis->set($mem_key, PROXY_REQ_OK);
+				$redis->expire($mem_key, CACHE_LIFETIME_MAX);
 
 				print $data;
 			} else {
-				if ($memcache) $memcache->set($mem_key, PROXY_REQ_FAILED, CACHE_LIFETIME_MAX);
+				$redis->set($mem_key, PROXY_REQ_FAILED);
+				$redis->expire($mem_key, CACHE_LIFETIME_MAX);
 			}
 		}
 		break;

+ 0 - 2
config.php-dist

@@ -39,8 +39,6 @@
 	// >= SESSION_COOKIE_LIFETIME_REMEMBER
 
 	define('FETCH_URL_TITLES', false);
-	define('MEMCACHE_SERVER', 'localhost:11211');
-	// Memcached server for URL title caching
 
 	// ***************************************
 	// *** Other settings (less important) ***