<?php
class video_mCachedb extends Memcache {
	static private $instance = null;
	static private $port = 11211;
	static function mGet() {
		if (self::$instance == null) {
			self::$instance = new Memcache;
			self::$instance->addServer("192.168.30.51", self::$port);
		}
		return self::$instance;
	}
}

function getVimeoVideo($search, $num = 5) {
	$key = md5($search.$num);
	if($videos = video_mCachedb::mGet()->get($key)) {
		return $videos;
	}
	
	include('Vimeo.php');
	
	$vimeo = new phpVimeo('8376aae43c6b11db0caa2e7a12428b058d0e587b', '801d76dc123e8d67d57b77ae10a4e7ae7f2909b6');
	$vimeos = $vimeo->call('vimeo.videos.search', array('full_response' => true, 'query' => $search, 'per_page' => 50));
	$videos = array();
	
	$i = 0;
	foreach($vimeos->videos->video as $video) {
		if($video->embed_privacy == 'anywhere') {
			$videos['Thumbnail'][$i] = $video->thumbnails->thumbnail[0]->_content;
			$videos['Title'][$i] = $video->title;
			$videos['Description'][$i] = $video->description;
			$videos['ID'][$i] = $video->id;
		$i++;
		}
	}
	
	if(is_array($videos)) {
		video_mCachedb::mGet()->set($key, $videos, 0, 60*60*24*30);
	}
	return array_slice($videos, 0, $num);
}
