-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcompute.php
More file actions
95 lines (83 loc) · 2.3 KB
/
compute.php
File metadata and controls
95 lines (83 loc) · 2.3 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
84
85
86
87
88
89
90
91
92
93
94
95
<?php
require 'vendor/autoload.php';
use MemCachier\MemcacheSASL;
// Make MemCachier connection
// ==========================
// parse config
$servers = explode(",", getenv("MEMCACHIER_SERVERS"));
for ($i = 0; $i < count($servers); $i++) {
$servers[$i] = explode(":", $servers[$i]);
}
// Using Memcached client (recommended)
// ------------------------------------
$m = new Memcached("memcached_pool");
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, TRUE);
// Enable no-block for some performance gains but less certainty that data has
// been stored.
$m->setOption(Memcached::OPT_NO_BLOCK, TRUE);
// Failover automatically when host fails.
$m->setOption(Memcached::OPT_AUTO_EJECT_HOSTS, TRUE);
// Adjust timeouts.
$m->setOption(Memcached::OPT_CONNECT_TIMEOUT, 2000);
$m->setOption(Memcached::OPT_POLL_TIMEOUT, 2000);
$m->setOption(Memcached::OPT_RETRY_TIMEOUT, 2);
$m->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD"));
if (!$m->getServerList()) {
// We use a consistent connection to memcached, so only add in the servers
// first time through otherwise we end up duplicating our connections to the
// server.
$m->addServers($servers);
}
// Enable MemCachier session support
session_start();
$_SESSION['test'] = 42;
// check if session info saved in memcached
// var_dump($m->get("memc.sess.key." . session_id()));
// Using MemcacheSASL client
// -------------------------
// $m->setSaslAuthData(getenv("MEMCACHIER_USERNAME"), getenv("MEMCACHIER_PASSWORD"));
// $m = new MemcacheSASL();
// if (!$m->getServerList()) {
// $m->addServers($servers);
// }
// Using the cache!
// ================
// 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;
}
// Get the value from the cache.
$in_cache = $m->get($n);
if ($in_cache) {
$message = "hit";
$prime = $in_cache;
} else {
$why = $m->getResultCode();
$message = "miss (".$why.")";
$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 ?>