<?php
// 31.08.2014 in 22:39[BLR]
	class cache {
		CONST ONLINE_TIME = 30;
		CONST USER_TIME = 10;
		CONST EXT = ".data";
		
		public static $count_cache_handler = array();
		
// Кэш. 04.09.2014 in 22:00[BLR]
		public static function run($cf, $t = 0, $q, $arr = array()) {
			/*
			 * $t = 0; // Счетчик.
			 * $t = 1; // Данные.
			 */
			$cf = CACHE.$cf.self :: EXT;
			if(@filemtime($cf) < (time() - ($t == 0 ? self :: ONLINE_TIME : self :: USER_TIME )))
			{
				if($t == 0 ) {
					$r = DB :: $pdo -> querySingle($q, $arr); $rf = $r;
				} else if($t == 1) {
					$r = DB :: $pdo -> queryFetch($q, $arr); $rf = @serialize($r);
				}
				@file_put_contents($cf, $rf);
			} else {
				if($t == 0) {
					$r = @file_get_contents($cf);
				} else if($t == 1) {
					$r = @unserialize(@file_get_contents($cf));
				}
				self :: $count_cache_handler[] = $cf;
			}
			return $r;
		}
		
// Кол-во людей онлайн.
		public static function count_people_online() {		
			global $config;
			return self :: run("counts/people_online", 0, "SELECT COUNT(*) FROM `users` WHERE `last_time` > '".(time()-$config['TIME_ONLINE'])."';");
		}
	
// Кол-во людей.	
		public static function count_people() {	
			global $config;	
			return self :: run("counts/people", 0, "SELECT COUNT(*) FROM `users` WHERE `id` <> '{$config['SYS_ID']}';");
		}

// Юзер данные. 04.09.2014 in 22:24[BLR]
		public static function user($sid) {
			return self :: run("people/".md5(md5($sid)."cache".md5($sid)), 1, "SELECT SQL_CACHE * FROM `users` WHERE `sid` = ? LIMIT 1;", array($sid));
		}
	}