<?php
/*
 * klasa obslugi memcache
 */
class MfMemcache {

	private static $host = 'unix:///var/tmp/memcached.sock';
	private static $port = '0';

	private static $connection = false;

	/*
	 * pobiera wartosc
	 */
	public static function get($key) {
		self::connect();
		if(is_a(self::$connection, 'Memcache'))
		return self::$connection->get($key);
	}

	/*
	 * ustawia wartosc
	 */
	public static function set($key, $data) {
		self::connect();
		//if(!is_a(self::$connection, 'Memcache'))
		self::$connection->set($key, $data);
	}
	/*
	 * usuwa wartosc
	 */
	public static function remove($key) {
		self::connect();
		if(is_a(self::$connection, 'Memcache'))
		self::$connection->delete($key);
	}

	/*
	 * sprawdza czy istnieje
	 */
	public static function exists($key) {
		self::connect();
		if(self::get($key) == FALSE) {
			return false;
		} else {
			return true;
		}
	}

	/*
	 * zamienia wartosc
	 */
	public static function replace($key, $value) {
		self::connect();
		if(is_a(self::$connection, 'Memcache'))
		self::$connection->replace($key, $value);

	}

	/*
	 * inkrementuje
	 */
	public static function increment($key, $int) {
		self::connect();
		if(!is_a(self::$connection, 'Memcache'))
		return self::$connection->increment($key, $int);
	}

	/*
	 * laczy z demonem
	 */
	private static function connect() {
		if(!is_a(self::$connection, 'Memcache')) {
			try {
			self::$connection = new Memcache();
			self::$connection->pconnect(self::$host, self::$port);
			} catch (Exception $e){

				MFLog::Error('Can\'t connect to memcache');

			}
			


		}
	}

}

?>