All pastes #1818191 Raw Edit

Something

public text v1 · immutable
#1818191 ·published 2010-03-02 00:56 UTC
rendered paste body
--- oldcache.php	2010-03-01 20:04:54.000000000 -0500
+++ newcache.php	2010-03-01 20:05:17.000000000 -0500
@@ -1,34 +1,9 @@
 <?
-/*************************************************************************|
-|--------------- Caching class -------------------------------------------|
-|*************************************************************************|
-
-This class is a wrapper for the Memcache class, and it's been written in
-order to better handle the caching of full pages with bits of dynamic
-content that are different for every user.
-
-As this inherits memcache, all of the default memcache methods work -
-however, this class has page caching functions superior to those of
-memcache.
-
-Also, Memcache::get and Memcache::set have been wrapped by
-CACHE::get_value and CACHE::cache_value. get_value uses the same argument
-as get, but cache_value only takes the key, the value, and the duration
-(no zlib).
-
-//unix sockets
-memcached -d -m 5120 -s /var/run/memcached.sock -a 0777 -t16 -C -u root
-
-//tcp bind
-memcached -d -m 8192 -l 10.10.0.1 -t8 -C
-
-|*************************************************************************/
-
-if (!extension_loaded('memcache')) {
-	error('Memcache Extension not loaded.');
+if (!extension_loaded('memcached')) {
+	error('Memcached Extension not loaded.');
 }
 
-class CACHE extends Memcache {
+class CACHE extends Memcached {
 	public $CacheHits = array();
 	public $MemcacheDBArray = array();
 	public $MemcacheDBKey = '';
@@ -36,21 +11,34 @@
 	public $Time = 0;
 
 	function __construct() {
-		$this->pconnect(MEMCACHED_HOST, MEMCACHED_PORT);
+		parent::__construct("lolgazelle");
+		Memcached::setOption(Memcached::OPT_COMPRESSION, false);
+		Memcached::setOption(Memcached::OPT_NO_BLOCK, true);
+		Memcached::setOption(Memcached::OPT_CACHE_LOOKUPS, true);
+		Memcached::setOption(Memcached::OPT_DISTRIBUTION,  Memcached::DISTRIBUTION_CONSISTENT);
+		Memcached::setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
+		
+		//uncomment if you have the json extension loaded
+		//Memcached::setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_JSON);
+		
+		//uncomment if you have memcached >=1.3.0 and libmemcached >=0.28
+		//Memcached::setOptions(Memcached::OPT_BINARY_PROTOCAL, true);
+		
+		Memcached::setOption(Memcached::OPT_PREFIX_KEY, SERVER_ROOT); //lolz, change plz
+		
+		if(count($this->getServerList()) < 1) {
+			$this->addServer(MEMCACHED_HOST, MEMCACHED_PORT);
+		}
+		
 		//$this->connect('localhost', 11211);
 	}
 
-	//---------- Caching functions ----------//
-
-	// Allows us to set an expiration on otherwise perminantly cache'd values
-	// Useful for disabled users, locked threads, basically reducing ram usage
 	public function expire_value($Key, $Duration=2592000) {
 		$StartTime=microtime(true);
 		$this->set($Key, $this->get($Key), $Duration);
 		$this->Time+=(microtime(true)-$StartTime)*1000;
 	}
 
-	// Wrapper for Memcache::set, with the zlib option removed and default duration of 1 hour
 	public function cache_value($Key, $Value, $Duration=2592000) {
 		$StartTime=microtime(true);
 		if (empty($Key)) {
@@ -236,6 +224,16 @@
 		}
 	}
 
+	public function lolbutts() {
+		$val = $cache->get("this_is_a_key", function($memcached_instance, $key, &$val) {
+			if(OORZA_IS_AWESOME) { //always true, but still...
+				$val = "Holy shit this is a new value OMFG";
+				return true; //HOLY FUCK MEMCACHED JUST READ THROUGH AND SET THE KEY __AND__ RETURNED IT! AIN'T THAT SOME SHIT
+			} else {
+				return false; //Poor lil $val gets ignored and the key doesn't get set
+			}
+		});
+	}
 	// Insert a value at the beginning of the array
 	public function insert_front($Key, $Value) {
 		if (!$this->InTransaction) {