浏览代码

省市区缓存模块完成

songjiyuan 9 年之前
父节点
当前提交
105f3eb2ea
共有 2 个文件被更改,包括 33 次插入3 次删除
  1. 2 0
      admin/control/cache.php
  2. 31 3
      data/model/area.model.php

+ 2 - 0
admin/control/cache.php

@@ -97,6 +97,8 @@ class cacheControl extends SystemControl
             // 省市区地址
             if (in_array('area', $todo)) {
                 dkcache('area');
+                dkcache('area_toplevelareas');  // 省级别缓存处理
+                dkcache('area_cityprovince');   // 市级别缓存处理
             }
         }
 

+ 31 - 3
data/model/area.model.php

@@ -36,13 +36,25 @@ class areaModel extends Model {
      * @return array 键为id 值为名称字符串
      */
     public function getTopLevelAreas() {
-        $data = $this->getCache();
 
+        // 对象属性中有数据则返回
+        if ($this->cachedTopLevelAreas !== null)
+            return $this->cachedTopLevelAreas;
+
+        // 缓存中有数据则返回
         $arr = array();
+        if ($arr = rkcache('area_toplevelareas')) {
+            $this->cachedTopLevelAreas = $arr;
+            return $arr;
+        }
+
+        $data = $this->getCache();
         foreach ($data['children'][0] as $i) {
             $arr[$i] = $data['name'][$i];
         }
 
+        wkcache('area_toplevelareas', $arr);
+        $this->cachedTopLevelAreas = $arr;
         return $arr;
     }
 
@@ -52,15 +64,29 @@ class areaModel extends Model {
      * @return array 键为市级id 值为省级id
      */
     public function getCityProvince() {
-        $data = $this->getCache();
 
+        // 对象属性中有数据则返回
+        if ($this->cachedCityProvince !== null)
+            return $this->cachedCityProvince;
+
+        // 缓存中有数据则返回
         $arr = array();
+        if ($arr = rkcache('area_cityprovince')) {
+            $this->cachedCityProvince = $arr;
+            return $arr;
+        }
+
+        // 生成数据
+        $data = $this->getCache();
         foreach ($data['parent'] as $k => $v) {
             if ($v && $data['parent'][$v] == 0) {
                 $arr[$k] = $v;
             }
         }
 
+        wkcache('area_cityprovince', $arr);
+        $this->cachedCityProvince = $arr;
+
         return $arr;
     }
 
@@ -205,5 +231,7 @@ class areaModel extends Model {
        return null;
     }
 
-    protected $cachedData;
+    protected $cachedData;  // 地区表缓存
+    protected $cachedTopLevelAreas;   // 省名字缓存
+    protected $cachedCityProvince;    // 市id缓存
 }