Prechádzať zdrojové kódy

update StoreDriver/File

walkor 10 rokov pred
rodič
commit
1e52d99f84

+ 0 - 2
GatewayWorker/Lib/Store.php

@@ -57,8 +57,6 @@ class Store
         {
             if(!isset(self::$instance[$config_name]))
             {
-                // 关闭opcache
-                ini_set('opcache.enable', false);
                 self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\File($config_name);
             }
             return self::$instance[$config_name];

+ 10 - 55
GatewayWorker/Lib/StoreDriver/File.php

@@ -15,8 +15,6 @@ class File
     protected $dataCache = array();
     // 上次缓存时间
     protected $lastCacheTime = 0;
-    // 保存数据的文件
-    protected $dataFile = '';
     // 打开文件的句柄
     protected $dataFileHandle = null;
     
@@ -26,7 +24,6 @@ class File
      */
     public function __construct($config_name)
     {
-        $this->dataFile = \Config\Store::$storePath . "/$config_name.store.cache.php";
         if(!is_dir(\Config\Store::$storePath) && !@mkdir(\Config\Store::$storePath, 0777, true))
         {
             // 可能目录已经被其它进程创建
@@ -38,14 +35,10 @@ class File
                 throw new \Exception('cant not mkdir('.\Config\Store::$storePath.')');
             }
         }
-        if(!is_file($this->dataFile))
-        {
-            touch($this->dataFile);
-        }
         $this->dataFileHandle = fopen(__FILE__, 'r');
         if(!$this->dataFileHandle)
         {
-            throw new \Exception("can not fopen($this->dataFile, 'r')");
+            throw new \Exception("can not fopen dataFileHandle");
         }
     }
     
@@ -58,12 +51,7 @@ class File
      */
     public function set($key, $value, $ttl = 0)
     {
-        flock($this->dataFileHandle, LOCK_EX);
-        $this->readDataFromDisk();
-        $this->dataCache[$key] = $value;
-        $ret = $this->writeToDisk();
-        flock($this->dataFileHandle, LOCK_UN);
-        return $ret;
+        return file_put_contents(\Config\Store::$storePath.'/'.$key, serialize($value), LOCK_EX);
     }
     
     /**
@@ -74,10 +62,8 @@ class File
      */
     public function get($key, $use_cache = true)
     {
-        flock($this->dataFileHandle, LOCK_EX);
-        $this->readDataFromDisk();
-        flock($this->dataFileHandle, LOCK_UN);
-        return isset($this->dataCache[$key]) ? $this->dataCache[$key] : null;
+        $ret = @file_get_contents(\Config\Store::$storePath.'/'.$key);
+        return $ret ? unserialize($ret) : null;
     }
    
     /**
@@ -87,12 +73,7 @@ class File
      */
     public function delete($key)
     {
-        flock($this->dataFileHandle, LOCK_EX);
-        $this->readDataFromDisk();
-        unset($this->dataCache[$key]);
-        $ret = $this->writeToDisk();
-        flock($this->dataFileHandle, LOCK_UN);
-        return $ret;
+        return @unlink(\Config\Store::$storePath.'/'.$key);
     }
     
     /**
@@ -103,16 +84,11 @@ class File
     public function increment($key)
     {
         flock($this->dataFileHandle, LOCK_EX);
-        $this->readDataFromDisk();
-        if(!isset($this->dataCache[$key]))
-        {
-            flock($this->dataFileHandle, LOCK_UN);
-            return false;
-        }
-        $this->dataCache[$key] ++;
-        $this->writeToDisk();
+        $val = $this->get($key);
+        $val = !$val ? 1 : ++$val;
+        file_put_contents(\Config\Store::$storePath.'/'.$key, serialize($val));
         flock($this->dataFileHandle, LOCK_UN);
-        return $this->dataCache[$key];
+        return $val;
     }
     
     /**
@@ -120,28 +96,7 @@ class File
      */
     public function destroy()
     {
-        @unlink($this->dataFile);
-    }
-    
-    /**
-     * 写入磁盘
-     * @return number
-     */
-    protected function writeToDisk()
-    {
-        return file_put_contents($this->dataFile, "<?php \n return " . var_export($this->dataCache, true). ';');
+        
     }
     
-    /**
-     * 从磁盘读
-     */
-    protected function readDataFromDisk()
-    {
-        $cache = include $this->dataFile;
-        if(is_array($cache))
-        {
-            $this->dataCache = $cache;
-        }
-        $this->lastCacheTime = time();
-    }
 }