Sfoglia il codice sorgente

数据字典缓存到内存

seimin 3 anni fa
parent
commit
22bc57b737

+ 25 - 3
src/app/services/main.service.ts

@@ -2,6 +2,8 @@ import { Injectable } from "@angular/core";
2 2
 import { HttpClient, HttpHeaders } from "@angular/common/http";
3 3
 
4 4
 import host from "../../assets/js/http";
5
+import { resolve } from "q";
6
+import { Observable } from "rxjs";
5 7
 
6 8
 @Injectable({
7 9
   providedIn: "root",
@@ -45,14 +47,34 @@ export class MainService {
45 47
     });
46 48
   }
47 49
   // 字典表
50
+  cachedDictionary = {}; //缓存到内存的字典
48 51
   getDictionary(type, key): any {
49 52
     const data = {
50 53
       type: type,
51 54
       key: key,
52 55
     };
53
-    return this.http.post(host.host + "/common/common/getDictionary", data, {
54
-      headers: this.headers,
55
-    });
56
+    console.log(this.cachedDictionary, "------------db");
57
+    if (this.cachedDictionary[key]) {
58
+      return new Observable((observer) => {
59
+        observer.next(this.cachedDictionary[key]);
60
+      });
61
+    } else {
62
+      return new Observable((observer) => {
63
+        this.http
64
+          .post(host.host + "/common/common/getDictionary", data, {
65
+            headers: this.headers,
66
+          })
67
+          .subscribe((result) => {
68
+            this.cachedDictionary[key] = result;
69
+            observer.next(result);
70
+          });
71
+      });
72
+    }
73
+  }
74
+  //清除字典表缓存
75
+  clearDictionary() {
76
+    this.cachedDictionary = {};
77
+    console.log(this.cachedDictionary, "------clearDb");
56 78
   }
57 79
   // 列表搜索(通用)
58 80
   getFetchDataList(type, target, data): any {

+ 2 - 0
src/app/views/order-management/order-management.component.ts

@@ -124,6 +124,8 @@ export class OrderManagementComponent implements OnInit {
124 124
   // 获取关联类型
125 125
   getAssociation() {
126 126
     let that = this;
127
+    console.log(that.mainService
128
+      .getDictionary("list", "association_types"))
127 129
     that.mainService
128 130
       .getDictionary("list", "association_types")
129 131
       .subscribe((data) => {

+ 1 - 0
src/app/views/sys-config/sys-config.component.ts

@@ -73,6 +73,7 @@ export class SysConfigComponent implements OnInit {
73 73
       (result) => {
74 74
         this.loading3 = false;
75 75
         if (result.status == 200) {
76
+          this.mainService.clearDictionary();
76 77
           this.showPromptModal("刷新", true, "");
77 78
         } else {
78 79
           this.showPromptModal("刷新", false, "");