Ugarit Ebla on 8 Aug 2008 09:11:19 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] wanted to share with this


I wanted to share this cool script with you.  It's a php script which 
puts google's webpage on the localhost's memory and is retrieved from 
memory instead of the harddrive.  I have set the memory to expire after 
7 seconds.

You will need apache/php/memcached service and have it started and php's 
memcached library.

$id can be anything not just a URI, a local file perhaps.

If you reload you'll see that the first key is unix_t and will only 
change every seven seconds.

<?php

var_dump(fetch_foo("http://google.com";));

function fetch_foo($id) {
  $ttl=7;
  $memcached = new Memcache;
  $memcached->connect('localhost', 11211) or die ("Could not connect");
  $value=$memcached->get("$id");
  if ($value) {return $value;}

  $ts=mktime();
 
  $fh=fopen("$id","r");
  $content=stream_get_contents($fh);
  fclose($fh);
  $value["$ts"]=$content;

  $memcached->set($id, $value, false, $ttl);
  return $value;
}

?>
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug