<?php
$module["cache"]["name"]="Cache Class";
$module["cache"]["ver"]="0.9.10";
/**
 * Project:     BeCast WebEngine - simple site engine
 * File:        /inc/cache.class.php
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.becast.at
 * @copyright 2009-2025 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package BcWe core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: b601fdb3b3b6a18250440a6e5c09b910a45629c0 $
 */
/*
Already defined by PHP. I'll leave it here for Info
define("LOG_EMERG", 0);
define("LOG_ALERT", 1);
define("LOG_CRIT", 2);
define("LOG_ERR", 3);
define("LOG_WARNING", 4);
define("LOG_INFO", 6);
define("LOG_DEBUG", 7);
*/

class cache {
	var $server;
	var $prefix;
	var $obj;
	var $port;
	var $exp;
	    /////////////////////////////////////////
	    // Module data
	    /////////////////////////////////////////
 
	    //
	    // __construct
	    //
	    // Buid logger
	    //
	function __construct() {
		global $config, $logger;
		$this->server=$config['MEMCACHE_SERVER'];
		$this->port=$config['MEMCACHE_PORT'];
		$this->prefix=$config['MEMCACHE_PREFIX'];
		$this->exp=$config['MEMCACHE_EXPIRATION'];
		$this->obj = new Memcached($this->prefix); 		
		$con = $this->connect($this->server,$this->port);
		if(!$con){
			return false;
		}else{
			return true;
		}
		
		
	}

 	public function connect($host , $port){
		$servers = $this->obj->getServerList();
		if(is_array($servers)) {
			foreach ($servers as $server) {
				if($server['host'] == $host and $server['port'] == $port){
					return true;
				} else {
					return $this->obj->addServer($host , $port);
				}
			}
        } 
	}

	function __destruct() {
		$this->obj->quit();
	}

	function set($key,$var,$exp=null): void
    {
        if($exp==null){
			$expiration = $this->exp;
		}else{
			$expiration = $exp;
		}
		$this->obj->set($this->prefix.$key,$var,$expiration);
	}


	function get($key){
		return $this->obj->get($this->prefix.$key);
   	}

}
?>
