first working version
authorS.Çağlar Onur <caglar@cs.princeton.edu>
Tue, 9 Nov 2010 03:16:50 +0000 (22:16 -0500)
committerS.Çağlar Onur <caglar@cs.princeton.edu>
Tue, 9 Nov 2010 03:16:50 +0000 (22:16 -0500)
php/plc_api.php

index cacce90..61fb298 100644 (file)
@@ -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;
     }
   }