Переглянути джерело

update memcache to memcached

Jinhui Zhu 7 роки тому
батько
коміт
07bee1b76c

+ 1 - 1
Qii/Autoloader/Import.php

@@ -20,7 +20,7 @@ class Import
                 return self::requires($n);
             }, $file);
         }
-        $file = str_replace(array('\\', '/'), DS, $file);
+        $file = str_replace(DS . DS , DS, str_replace(array('\\', '/'), DS, $file));
         if (self::getFileLoaded($file)) return true;
         if (file_exists($file)) {
             self::setFileLoaded($file);

+ 2 - 2
Qii/Cache/Loader.php

@@ -35,9 +35,9 @@ class Loader
     public function setCache($cache)
     {
         $this->cache = $cache;
-        $cacheFile = dirname(__FILE__) . DS . 'Cache' . DS . ucwords($cache) . '.php';
+        $cacheFile = dirname(__FILE__) . DS . DS . ucwords($cache) . '.php';
         if (!is_file($cacheFile)) {
-            $cacheFile = dirname(__FILE__) . DS . 'Cache' . DS . 'Memcache.php';
+            throw new \Exception('Unsupport cache class '. $cacheFile);
         }
         \Qii\Autoloader\Import::requires($cacheFile);
     }

+ 4 - 5
Qii/Cache/Memcache.php → Qii/Cache/Memcached.php

@@ -6,7 +6,7 @@
  */
 namespace Qii\Cache;
 
-class Memcache implements Intf
+class Memcached implements Intf
 {
     const VERSION = '1.2';
     /**
@@ -64,7 +64,7 @@ class Memcache implements Intf
 
     public function __construct(array $policy = null)
     {
-        if (!extension_loaded('memcache')) {
+        if (!extension_loaded('memcached')) {
             return \Qii::setError(false, __LINE__, 1004);
         }
         if (is_array($policy)) {
@@ -74,7 +74,7 @@ class Memcache implements Intf
             $this->_default_policy['servers'][] = $this->_default_server;
         }
         if (!isset($this->_default_policy['persistent'])) $this->_default_policy['persistent'] = '';
-        $this->_conn = new \Memcache();
+        $this->_conn = new \Memcached();
         foreach ($this->_default_policy['servers'] as $server) {
             $result = $this->_conn->addServer($server['host'], $server['port'], $this->_default_policy['persistent']);
             if (!$result) {
@@ -93,10 +93,9 @@ class Memcache implements Intf
      */
     public function set($id, $data, array $policy = null)
     {
-        if (!isset($policy['compressed'])) $policy['compressed'] = $this->_default_policy['compressed'];
         if (!isset($policy['life_time'])) $policy['life_time'] = $this->_default_policy['life_time'];
 
-        return $this->_conn->set($id, $data, $policy['compressed'] ? MEMCACHE_COMPRESSED : 0, $policy['life_time']);
+        return $this->_conn->set($id, $data, $policy['life_time']);
     }
 
     /**

+ 11 - 0
Qii/Driver/Base.php

@@ -596,6 +596,17 @@ class Base
 	{
 		return $this->_response;
 	}
+
+	public function iconv($str)
+	{
+		if (is_array($str)) {
+            return array_map(function ($n) {
+                return iconv('GB2312', 'UTF-8', $n);
+            }, $str);
+        }
+
+		return iconv('GB2312', 'UTF-8', $str);
+	}
 	/**
 	 * 如果不存在指定的方法则调用提示错误
 	 *

+ 1 - 1
Qii/Driver/Mysql/Driver.php

@@ -234,7 +234,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
 	{
 		if (\mysql_errno($this->db['CURRENT'])) {
 			$this->_errorInfo[$this->queryTimes]['sql'] = $this->sql;
-			$this->_errorInfo[$this->queryTimes]['error'][2] = iconv('GB2312', 'UTF-8', \mysql_error($this->db['CURRENT']));
+			$this->_errorInfo[$this->queryTimes]['error'][2] = $this->iconv(\mysql_error($this->db['CURRENT']));
 			$this->response = \Qii\Driver\Response::Fail('mysql.error', $this->_errorInfo);
 		}
 	}

+ 1 - 1
Qii/Driver/Mysqli/Driver.php

@@ -255,7 +255,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
 	{
 		if (\mysqli_errno($this->db['CURRENT'])) {
 			$this->_errorInfo[$this->queryTimes]['sql'] = $this->sql;
-			$this->_errorInfo[$this->queryTimes]['error'][2] = iconv('GB2312', 'UTF-8', \mysqli_error($this->db['CURRENT']));
+			$this->_errorInfo[$this->queryTimes]['error'][2] = $this->iconv(\mysqli_error($this->db['CURRENT']));
 			$this->response = \Qii\Driver\Response::Fail('mysqli.error', $this->_errorInfo);
 		}
 	}

+ 1 - 1
Qii/Driver/Pdo/Driver.php

@@ -284,7 +284,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
     {
         if ($this->connection->getConnectionBySQL($this->sql)->errorCode() != '00000') {
             $this->_errorInfo[$this->queryTimes]['sql'] = $this->sql;
-            $this->_errorInfo[$this->queryTimes]['error'] = iconv('GB2312', 'UTF-8', $this->connection->getConnectionBySQL($this->sql)->errorInfo());
+            $this->_errorInfo[$this->queryTimes]['error'] = $this->iconv($this->connection->getConnectionBySQL($this->sql)->errorInfo());
             $this->response = \Qii\Driver\Response::Fail('pdo.error', $this->_errorInfo);
         }
     }

+ 3 - 2
Qii/Library/Third/WhichBrowser/Cache.php

@@ -89,7 +89,7 @@ trait Cache
             }
         }
 
-        if ($this->cache instanceof \Qii\Cache\Memcache) {
+        if ($this->cache instanceof \Qii\Cache\Memcached) {
             $cacheId = 'whichbrowser_' . md5(serialize($headers));
             $item = unserialize($this->cache->get($cacheId));
             if ($item) {
@@ -98,7 +98,8 @@ trait Cache
                 $analyser = new Analyser($headers, $options);
                 $analyser->setdata($this);
                 $analyser->analyse();
-                $this->cache->set($cacheId, serialize($this->retrieveCachedData()), ['expired' => time() + $this->expires]);
+                $data = serialize($this->retrieveCachedData());
+                $this->cache->set($cacheId, $data, ['life_time' => time() + $this->expires]);
             }
 
             return true;

+ 4 - 4
_cli/app.ini

@@ -44,7 +44,7 @@ view[smarty.compile] = tmp/compile
 view[smarty.cache] = tmp/cache
 view[smarty.lifetime] = 300
 ;缓存类型
-cache = memcache
+cache = memcached
 
 ;是否开启安全验证,enable:是否开启安全验证;key:POST数据的时候安全字符串加密用到的key
 security[enable] = true
@@ -52,9 +52,9 @@ security[name] = security_sid
 security[expired] = 3600
 security[key] = 4cd780a986d5c30e03bdcb67d16c8320
 
-;memcache配置,多个服务器IP和端口以;隔开
-memcache[servers] = 127.0.0.1
-memcache[ports] = 11211
+;memcached配置,多个服务器IP和端口以;隔开
+memcached[servers] = 127.0.0.1
+memcached[ports] = 11211
 ;redis配置,多个服务器IP和端口以;隔开
 redis[servers] = 127.0.0.1
 redis[ports] = 6379