forked from memcachier/examples-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute.php
More file actions
83 lines (76 loc) · 1.81 KB
/
compute.php
File metadata and controls
83 lines (76 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?Php
include('MemcacheSASL.php');
// pass 'n' argument
if (!isset($_GET["n"])) {
echo "N must be set!";
exit;
}
$n = intval($_GET["n"]);
if ($n <= 1) {
echo "N must be greater than 1";
exit;
} else if ($n > 10000) {
$n = 10000;
}
// ==========================
// Make MemCachier connection
// ==========================
//
// Using MemcacheSASL client (recommended)
// =======================================
// $m = new MemcacheSASL;
// $servers = explode(",", getenv("MEMCACHIER_SERVERS"));
// foreach ($servers as $s) {
// $parts = explode(":", $s);
// $m->addServer($parts[0], $parts[1]);
// }
// $m->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD"));
// Using Memcached client
// ======================
$m = new Memcached();
if (!$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true)) {
echo "Error switching to memcached binary protocol!";
exit;
}
$servers = explode(",", getenv("MEMCACHIER_SERVERS"));
for ($i = 0; $i < count($servers); $i++) {
$servers[$i] = explode(":", $servers[$i]);
}
$m->addServers($servers);
foreach ($servers as $s) {
$parts = explode(":", $s);
if (!$m->addServer($parts[0], $parts[1])) {
echo "Error adding in MemCachier servers!";
exit;
}
}
$m->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD"));
// ================
// Using the cache!
// ================
// Get the value from the cache.
$in_cache = $m->get($n);
if ($in_cache) {
$message = "hit";
$prime = $in_cache;
} else {
$message = "miss";
$prime = 1;
for ($i = $n; $i > 1; $i--) {
$is_prime = true;
for ($j = 2; $j < $i; $j++) {
if ($i % $j == 0) {
$is_prime = false;
break;
}
}
if ($is_prime) {
$prime = $i;
break;
}
}
$m->add($n, $prime);
}
?>
<?= $prime ?><br />
cache: <?= $message ?>