<?php
//
// TorrentTrader v2.x
// Caching code
// Author: TorrentialStorm
//
// http://www.torrenttrader.org
//
//
$GLOBALS["TTCache"] = new TTCache;
class TTCache {
function TTCache () {
GLOBAL $site_config;
$this->cachedir = $site_config["cache_dir"];
$this->type = strtolower(trim($site_config["cache_type"]));
switch ($this->type) {
case "memcache":
$this->obj = new Memcache;
if (!@$this->obj->Connect($site_config["cache_memcache_host"], $site_config["cache_memcache_port"]))
$this->type = "disk";
break;
case "apc":
if (function_exists("apc_store"))
break;
default:
$this->type = "disk";
}
}
function Set ($var, $val, $expire = 0) {
if ($expire == 0)
return;
switch ($this->type) {
case "memcache":
return $this->obj->set($var, $val, 0, $expire);
break;
case "apc":
return apc_store($var, $val, $expire);
break;
case "disk":
$fp = fopen($this->cachedir."/cache_$var.txt", "w");
fwrite($fp, serialize($val));
fclose($fp);
return;
break;
}
}
function Get ($var, $expire = 0) {
if ($expire == 0)
return false;
switch ($this->type) {
case "memcache":
return $this->obj->get($var);
break;
case "apc":
return apc_fetch($var);
break;
case "disk":
$file = $this->cachedir."/cache_$var.txt";
if (file_exists($file) && (time() - filemtime($file)) < $expire)
return unserialize(file_get_contents($file));
return false;
break;
}
}
}