Skip to content
This repository
Browse code

Added directive max-connections-per-server to MongoClient and Memcach…

…eClient.
  • Loading branch information...
commit 05735750b52906347c43002e3670d643c9ec8ea1 1 parent 44f54dc
Vasily Zorin authored February 05, 2011

Showing 1 changed file with 92 additions and 0 deletions. Show diff stats Hide diff stats

  1. 92  app-examples/ExampleWithMemcache.php
92  app-examples/ExampleWithMemcache.php
... ...
@@ -0,0 +1,92 @@
  1
+<?php
  2
+
  3
+/**
  4
+ * @package Examples
  5
+ * @subpackage Memcache
  6
+ *
  7
+ * @author Zorin Vasily <kak.serpom.po.yaitsam@gmail.com>
  8
+ */
  9
+class ExampleWithMemcache extends AppInstance {
  10
+
  11
+	/**
  12
+	 * Called when the worker is ready to go.
  13
+	 * @return void
  14
+	 */
  15
+
  16
+	public function onReady() {
  17
+		$this->memcache = Daemon::$appResolver->getInstanceByAppName('MemcacheClient');
  18
+	}
  19
+	
  20
+	/**
  21
+	 * Creates Request.
  22
+	 * @param object Request.
  23
+	 * @param object Upstream application instance.
  24
+	 * @return object Request.
  25
+	 */
  26
+	public function beginRequest($req, $upstream) {
  27
+		return new ExampleWithMemcacheRequest($this, $upstream, $req);
  28
+	}
  29
+	
  30
+}
  31
+
  32
+class ExampleWithMemcacheRequest extends HTTPRequest {
  33
+
  34
+	public $stime;
  35
+	public $queryResult;
  36
+	public $sql;
  37
+	public $runstate = 0;
  38
+
  39
+	/**
  40
+	 * Constructor.
  41
+	 * @return void
  42
+	 */
  43
+	public function init() {
  44
+		$req = $this;
  45
+		$this->stime = microtime(true);
  46
+		$this->appInstance->memcache->stats(function($m) use ($req) {
  47
+			$req->queryResult = $m->result;
  48
+			$req->wakeup(); // wake up the request immediately
  49
+		});
  50
+	}
  51
+
  52
+	/**
  53
+	 * Called when request iterated.
  54
+	 * @return integer Status.
  55
+	 */
  56
+	public function run() {
  57
+		if (
  58
+			!$this->queryResult 
  59
+			&& ($this->runstate++ === 0)
  60
+		) {
  61
+			// sleep for 5 seconds or until wakeup
  62
+			$this->sleep(5);
  63
+		} 
  64
+		
  65
+		try {
  66
+			$this->header('Content-Type: text/html');
  67
+		} catch (Exception $e) {}
  68
+
  69
+			?>
  70
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  71
+<html xmlns="http://www.w3.org/1999/xhtml">
  72
+<head>
  73
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  74
+<title>Example with Memcache</title>
  75
+</head>
  76
+<body>
  77
+<?php
  78
+if ($this->queryResult) {
  79
+	echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>';
  80
+	var_dump($this->queryResult);
  81
+	echo '</pre>';
  82
+} else {
  83
+	echo '<h1>Something went wrong! We have no result.</h1>';
  84
+}
  85
+echo '<br />Request (http) took: '.round(microtime(TRUE)-$this->stime,6);
  86
+?>
  87
+</body>
  88
+</html>
  89
+<?php
  90
+	}
  91
+	
  92
+}

0 notes on commit 0573575

Please sign in to comment.
Something went wrong with that request. Please try again.