Removed dep on API
[ninja.git] / system / libraries / drivers / Cache / Memcache.php
blob5d5051556d1997f42eaa60a9f108f7f530b8f978
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Memcache-based Cache driver.
5 * $Id: Memcache.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Cache
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class Cache_Memcache_Driver implements Cache_Driver {
14 // Cache backend object and flags
15 protected $backend;
16 protected $flags;
18 public function __construct()
20 if ( ! extension_loaded('memcache'))
21 throw new Kohana_Exception('cache.extension_not_loaded', 'memcache');
23 $this->backend = new Memcache;
24 $this->flags = Kohana::config('cache_memcache.compression') ? MEMCACHE_COMPRESSED : 0;
26 $servers = Kohana::config('cache_memcache.servers');
28 foreach ($servers as $server)
30 // Make sure all required keys are set
31 $server += array('host' => '127.0.0.1', 'port' => 11211, 'persistent' => FALSE);
33 // Add the server to the pool
34 $this->backend->addServer($server['host'], $server['port'], (bool) $server['persistent'])
35 or Kohana::log('error', 'Cache: Connection failed: '.$server['host']);
39 public function find($tag)
41 return FALSE;
44 public function get($id)
46 return (($return = $this->backend->get($id)) === FALSE) ? NULL : $return;
49 public function set($id, $data, $tags, $lifetime)
51 count($tags) and Kohana::log('error', 'Cache: Tags are unsupported by the memcache driver');
53 // Memcache driver expects unix timestamp
54 if ($lifetime !== 0)
56 $lifetime += time();
59 return $this->backend->set($id, $data, $this->flags, $lifetime);
62 public function delete($id, $tag = FALSE)
64 if ($id === TRUE)
65 return $this->backend->flush();
67 if ($tag == FALSE)
68 return $this->backend->delete($id);
70 return TRUE;
73 public function delete_expired()
75 return TRUE;
78 } // End Cache Memcache Driver