Bladeren bron

Update:cache module

Zhu Jinhui 7 jaren geleden
bovenliggende
commit
747508ca94
3 gewijzigde bestanden met toevoegingen van 58 en 14 verwijderingen
  1. 39 0
      src/Cache/Apcu.php
  2. 9 8
      src/Cache/Memcached.php
  3. 10 6
      src/Cache/Redis.php

+ 39 - 0
src/Cache/Apcu.php

@@ -0,0 +1,39 @@
+<?php
+namespace Qii\Cache;
+
+class Apcu implements Qii_Cache_Intf
+{
+    const VERSION = '1.2';
+    public $policy = array('life_time' => 3600);//设置目录、过期时间、文件前缀
+
+    public function __construct(array $policy = null)
+    {
+        if (!empty($policy)) {
+            $this->policy = array_merge($this->policy, $policy);
+        }
+    }
+
+    public function set($key, $value, $policy)
+    {
+        if(in_array($policy))
+        {
+            $this->policy = array_merge($this->policy, $policy);
+        }
+        return apcu_store($key, $value, $this->policy['life_time']);
+    }
+
+    public function get($key)
+    {
+        return apcu_fetch($key);
+    }
+
+    public function exists($key)
+    {
+        return apcu_exists($key);
+    }
+    
+    public function del($key)
+    {
+        return apcu_delete($key);
+    }
+}

+ 9 - 8
src/Cache/Memcached.php

@@ -100,12 +100,15 @@ class Memcached implements Intf
      */
     public function set($id, $data, array $policy = null)
     {
-        if (!isset($policy['life_time'])) $policy['life_time'] = $this->_default_policy['life_time'];
-        if($this->_conn instanceof \Memcache){
+        if (is_array($policy)) {
+            $this->_default_policy = array_merge($this->_default_policy, $policy);
+        }
+
+        if($this->_conn instanceof \Memcache || $this->_conn instanceof \Memcached){
             $data = serialize($data);
-            return $this->_conn->set($id, $data, MEMCACHE_COMPRESSED, $policy['life_time']);
+            return $this->_conn->set($id, $data, MEMCACHE_COMPRESSED, $this->_default_policy['life_time']);
         }
-        return $this->_conn->set($id, $data, $policy['life_time']);
+        return $this->_conn->set($id, $data, $this->_default_policy['life_time']);
     }
 
     /**
@@ -118,7 +121,7 @@ class Memcached implements Intf
     public function get($id)
     {
         $data = $this->_conn->get($id);
-        if($this->_conn instanceof \Memcache){
+        if($this->_conn instanceof \Memcache || $this->_conn instanceof \Memcached){
             $data = unserialize($data);
         }
         return $data;
@@ -148,6 +151,4 @@ class Memcached implements Intf
     {
         return call_user_func_array(array($this->_conn, $method), $args);
     }
-}
-
-?>
+}

+ 10 - 6
src/Cache/Redis.php

@@ -52,11 +52,13 @@ class Redis implements Intf
      */
     public function hMset($id, $data, array $policy = null)
     {
-        if (!isset($policy['life_time'])) $policy['life_time'] = $this->policy['life_time'];
+        if (!empty($policy)) {
+            $this->policy = array_merge($this->policy, $policy);
+        }
         try {
             $this->redis->hMset($id, $data);
-            if (isset($policy['life_time']) && $policy['life_time'] > 0) {
-                $this->redis->setTimeout($id, $policy['life_time']);
+            if (isset($this->policy['life_time']) && $this->policy['life_time'] > 0) {
+                $this->redis->setTimeout($id, $this->policy['life_time']);
             }
         } catch (\CredisException $e) {
             throw new \Qii\Exceptions\Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
@@ -68,11 +70,13 @@ class Redis implements Intf
      */
     public function set($id, $value, array $policy = null)
     {
-        if (!isset($policy['life_time'])) $policy['life_time'] = $this->policy['life_time'];
+        if (!empty($policy)) {
+            $this->policy = array_merge($this->policy, $policy);
+        }
         try {
             $this->redis->set($id, $value);
-            if (isset($policy['life_time']) && $policy['life_time'] > 0) {
-                $this->redis->setTimeout($id, $policy['life_time']);
+            if (isset($this->policy['life_time']) && $this->policy['life_time'] > 0) {
+                $this->redis->setTimeout($id, $this->policy['life_time']);
             }
         } catch (\CredisException $e) {
             throw new \Qii\Exceptions\Errors(\Qii::i(-1, $e->getMessage()), __LINE__);