<?php
class People implements IteratorAggregate {
private $c;
public function __construct($host, $port) {
// TODO handle errors
$this->c = memcache_connect($host, $port);
}
public function add($id, $value) {
memcache_set($this->c, "people_$id", $value);
// TODO race condition
$list = memcache_get($this->c, "peoples");
$list[$id] = true;
memcache_set($this->c, "peoples", $list);
}
public function getIterator() {
$list = memcache_get($this->c, "peoples");
foreach ($list as $item => $_) yield memcache_get($this->c, "people_$item");
}
}
$People = new People('localhost', 11211);
$People->add(1112, "first");
$People->add(1115, shell_exec("uname -v"));
$People->add(2000, microtime(true));
foreach ($People as $item) {
echo $item, "\n";
}