|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + |
| 5 | + * Website: http://www.phpfastcache.com |
| 6 | + * Example at our website, any bugs, problems, please visit http://faster.phpfastcache.com |
| 7 | + * |
| 8 | + * ssdb official website: |
| 9 | + * http://ssdb.io/ |
| 10 | + */ |
| 11 | + |
| 12 | +class phpfastcache_ssdb extends BasePhpFastCache implements phpfastcache_driver { |
| 13 | + |
| 14 | + private $checked_ssdb = false; |
| 15 | + |
| 16 | + function checkdriver() { |
| 17 | + // Check memcache |
| 18 | + $this->required_extension("SSDB.php"); |
| 19 | + if (class_exists("SimpleSSDB")) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + $this->fallback = true; |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + function __construct($config = array()) { |
| 27 | + $this->setup($config); |
| 28 | + if (!$this->checkdriver() && !isset($config['skipError'])) { |
| 29 | + $this->fallback = true; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + function connectServer() { |
| 34 | + |
| 35 | + $server = isset($this->config['ssdb']) ? $this->config['ssdb'] : array( |
| 36 | + "host" => "127.0.0.1", |
| 37 | + "port" => 8888, |
| 38 | + "password" => "", |
| 39 | + "timeout" => 2000, |
| 40 | + ); |
| 41 | + |
| 42 | + if ($this->checked_ssdb === false) { |
| 43 | + $host = $server['host']; |
| 44 | + $port = isset($server['port']) ? (Int) $server['port'] : 8888; |
| 45 | + $password = isset($server['password']) ? $server['password'] : ""; |
| 46 | + $timeout = isset($server['timeout']) ? (Int) $server['timeout'] : 2000; |
| 47 | + $this->instant = new SimpleSSDB($host, $port, $timeout); |
| 48 | + if (!empty($password)) { |
| 49 | + $this->instant->auth($password); |
| 50 | + } |
| 51 | + $this->checked_ssdb = true; |
| 52 | + if (!$this->instant) { |
| 53 | + $this->fallback = true; |
| 54 | + return false; |
| 55 | + } else { |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + function driver_set($keyword, $value = "", $time = 300, $option = array()) { |
| 64 | + if ($this->connectServer()) { |
| 65 | + if (isset($option['skipExisting']) && $option['skipExisting'] == true) { |
| 66 | + $x = $this->instant->get($keyword); |
| 67 | + if($x === false) { |
| 68 | + return false; |
| 69 | + }elseif(!is_null($x)) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + $value = $this->encode($value); |
| 74 | + return $this->instant->setx($keyword, $value, $time); |
| 75 | + } else { |
| 76 | + return $this->backup()->set($keyword, $value, $time, $option); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function driver_get($keyword, $option = array()) { |
| 81 | + if ($this->connectServer()) { |
| 82 | + // return null if no caching |
| 83 | + // return value if in caching' |
| 84 | + $x = $this->instant->get($keyword); |
| 85 | + if($x == false) { |
| 86 | + return null; |
| 87 | + } else { |
| 88 | + return $this->decode($x); |
| 89 | + } |
| 90 | + } else { |
| 91 | + $this->backup()->get($keyword, $option); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + function driver_delete($keyword, $option = array()) { |
| 96 | + if ($this->connectServer()) { |
| 97 | + $this->instant->del($keyword); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + function driver_stats($option = array()) { |
| 102 | + if ($this->connectServer()) { |
| 103 | + $res = array( |
| 104 | + "info" => "", |
| 105 | + "size" => $this->instant->dbsize(), |
| 106 | + "data" => $this->instant->info(), |
| 107 | + ); |
| 108 | + |
| 109 | + return $res; |
| 110 | + } |
| 111 | + |
| 112 | + return array(); |
| 113 | + } |
| 114 | + |
| 115 | + function driver_clean($option = array()) |
| 116 | + { |
| 117 | + //Is not supported, only support command line operations |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + function driver_isExisting($keyword) |
| 122 | + { |
| 123 | + if ($this->connectServer()) { |
| 124 | + $x = $this->instant->exists($keyword); |
| 125 | + if ($x == null) { |
| 126 | + return false; |
| 127 | + } else { |
| 128 | + return true; |
| 129 | + } |
| 130 | + } else { |
| 131 | + return $this->backup()->isExisting($keyword); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments