Quote:
Originally Posted by GrouchyAdmin
You need to add support for caching; you probably only need to poll once a day, right?
...
|
Thanks. I took your code but modified it to work with memcache. ENJOY THE SCALABILITY.
Code:
<?php
### DEFINE MEMCACHE CONSTANTS
DEFINE('MEMCACHE_HOST', 'localhost');
DEFINE('MEMCACHE_PORT', 11211);
### THIS IS MY DOMAIN, PLZ DONT USE MY LINKS
$domain_base_url = 'http://www.mydomain.com/';
$randomized_urls = file($domain_base_url . 'files/urls.txt');
$urls = array();
foreach ((array)$randomized_urls as $url) {
if (!check_cache($url)) {
### MAKE SURE ITS ONLINE
if (validate_url_presence_exists($url) == "TRUE") {
$urls[] = $url;
add_cache($url);
}
} else {
$urls[] = $url;
}
}
if (empty($urls)) {
### EVERYONE LIKES HEAD // todo, get head
header('Location: ' . $domain_base_url);
exit;
} else {
srand(enhanced_seed_generator_key());
$random_url = $urls[rand(0, count($urls)-1)]; // BUG FIXED
header('Location: ' . $random_url);
exit;
}
### PASS MY SEED
function enhanced_seed_generator_key() {
$str = '';
$chars = str_split('abcdefghijklmnopqrstuvwxyz');
for ($i = 1; $i = 10; $i++) {
shuffle($chars);
$str .= $chars[rand(0, count($chars))];
}
return (float)(time() + (getmypid() * microtime())) + php_atoi_v2($str);
}
### PHP FAILURE
function php_atoi_v2($str) {
$total = 0;
foreach (str_split($str) as $chr) {
$total += ord($chr);
}
return (int)$total;
}
function validate_url_presence_exists($url) {
$ch = curl_init();
curl_setopt($ch, curlOPT_URL, $url);
curl_setopt($ch, curlOPT_NOBODY, true);
curl_setopt($ch, curlOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, curlOPT_FOLLOWLOCATION, true);
curl_setopt($ch, curlOPT_HEADER, true);
$res = curl_exec($ch);
curl_close($ch);
return false if $res === false;
$code = explode($res);
return $code[1] == '200' ? "TRUE" : "FALSE";
}
function check_cache($url) {
$memcache = get_memcache_object_balanced();
return $memcache->get($url);
}
function add_cache($url) {
$memcache = get_memcache_object_balanced();
$memcache->set($url, 1, 0, 86400);
}
### BALANCE THE LOAD??? TODO -- MAKE IT BALANCE
function get_memcache_object_balanced() {
$memcache = new Memcache;
$memcache->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ("Could not connect");
return $memcache;
}