| 1 | <?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | class Cache_Memcache_Driver implements Cache_Driver { |
|---|
| 11 | |
|---|
| 12 | private $_mc = null; |
|---|
| 13 | private $flag = null; |
|---|
| 14 | |
|---|
| 15 | public function __construct() |
|---|
| 16 | { |
|---|
| 17 | if ( ! extension_loaded('memcache')) |
|---|
| 18 | throw new Kohana_Exception('cache.extension_not_loaded', 'memcache'); |
|---|
| 19 | |
|---|
| 20 | $this->_mc =& new Memcache; |
|---|
| 21 | $this->flag = (Config::item('cache_memcache.compression')) ? MEMCACHE_COMPRESSED : 0; |
|---|
| 22 | |
|---|
| 23 | foreach ( Config::item('cache_memcache.servers') as $server) |
|---|
| 24 | { |
|---|
| 25 | if ( ! isset($server['persistent'])) |
|---|
| 26 | $server['persistent'] = false; |
|---|
| 27 | |
|---|
| 28 | if ( ! isset($server['port'])) |
|---|
| 29 | $server['port'] = 11211; |
|---|
| 30 | |
|---|
| 31 | if( ! $this->_mc->addServer($server['host'], $server['port'], $server['persistent'])) |
|---|
| 32 | Log::add('error', 'Cache: Connection failed -> '.$server['host']); |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public function find($tag) |
|---|
| 37 | { |
|---|
| 38 | return false; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public function get($id) |
|---|
| 42 | { |
|---|
| 43 | return $this->_mc->get($id); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public function set($id, $data, $tags, $expiration) |
|---|
| 47 | { |
|---|
| 48 | $result = $this->_mc->set($id, $data, $this->flag, $expiration); |
|---|
| 49 | |
|---|
| 50 | if(count($tags) > 0) |
|---|
| 51 | Log::add('error', 'tags are unsupported by the Memcache driver'); |
|---|
| 52 | |
|---|
| 53 | return $result; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | public function delete($id, $tag = FALSE) |
|---|
| 57 | { |
|---|
| 58 | if($id === TRUE) |
|---|
| 59 | { |
|---|
| 60 | return $this->_mc->flush(); |
|---|
| 61 | } |
|---|
| 62 | elseif ($tag == FALSE) |
|---|
| 63 | { |
|---|
| 64 | return $this->_mc->delete($id); |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | { |
|---|
| 68 | return true; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public function delete_expired() |
|---|
| 73 | { |
|---|
| 74 | return true; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|