From 55bfc3962a889b183530c3d896e5c650ef882338 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=2E=C3=87a=C4=9Flar=20Onur?= Date: Mon, 8 Nov 2010 22:16:50 -0500 Subject: [PATCH] first working version --- php/plc_api.php | 60 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/php/plc_api.php b/php/plc_api.php index cacce90a..61fb2987 100644 --- a/php/plc_api.php +++ b/php/plc_api.php @@ -74,37 +74,67 @@ class PLCAPI return ((float) $usec + (float) $sec); } - function call($method, $args = NULL) + function generateKey($method, $args) + { + $excluded_API_functions = array("AddSession", "GetSession", "DeleteSession", "AuthCheck", "VerifyPerson"); + + if (array_search($method, $excluded_API_functions)) + return NULL; + + // Key is the md5sum of method + (arguments list - session variables) + $arguments = $args; + unset($arguments[0]); + return md5($method . serialize(arguments)); + } + + function lookup($method, $args = NULL) { + $key = $this->generateKey($method, $args); + if ($key == NULL) + return NULL; + $memcache = new Memcache; $memcache->connect($this->server, 11211) or die ("Could not connect"); - $key = $method; - if (gettype($args[1]) == "array") - { - foreach(array_values($args[1]) as $arg) - $key .= $arg; + $result = $memcache->get($key); + + if ($result == FALSE) { + $this->error_log ("MEMCACHE: " . $method . " with " . count($args) . " args lookup failed for " . $key); } else - $key .= $args[1]; - - $result = $memcache->get($key); - if ($result != FALSE) { - if ($result == "NULL") - return NULL; + { + $this->error_log ("MEMCACHE: " . $method . " with " . count($args) . " args lookup succeded for " . $key); if (gettype($result) == "array") return $result; + else if ($result == "NULL") + return NULL; else return unserialize($result); } + return NULL; + } + function call($method, $args = NULL) + { if ($this->multicall) { $this->calls[] = array ('methodName' => $method, - 'params' => $args); + 'params' => $args); return NULL; } else { - $result = $this->internal_call ($method, $args, 3); - $memcache->set($key, $result == NULL ? "NULL" : $result, false, 600); + $result = $this->lookup($method, $args); + if ($result == NULL) + { + $memcache = new Memcache; + $memcache->connect($this->server, 11211) or die ("Could not connect"); + + $result = $this->internal_call ($method, $args, 3); + + $key = $this->generateKey($method, $args); + if ($key != NULL) + { + $memcache->set($key, $result == NULL ? "NULL" : $result, false, 6000); + } + } return $result; } } -- 2.47.0