Browse Source

added 10 minutes cache

tags/1.0.0
Robin Thoni 8 years ago
parent
commit
5c325199f1
1 changed files with 59 additions and 12 deletions
  1. 59
    12
      sdk/Business/WeeksBusiness.js

+ 59
- 12
sdk/Business/WeeksBusiness.js View File

@@ -10,6 +10,38 @@
10 10
 
11 11
             var Business = {};
12 12
 
13
+            Business.cachedWeeks = [];
14
+
15
+            Business.currentWeekId = null;
16
+
17
+            Business.cacheWeek = function(week, data)
18
+            {
19
+                data = {
20
+                    week: week,
21
+                    data: data,
22
+                    time: new Date()
23
+                };
24
+                Business.cachedWeeks.push(data);
25
+            };
26
+
27
+            Business.getCachedWeek = function(data)
28
+            {
29
+                var limitDate = new Date();
30
+                limitDate = new Date(limitDate.getTime() - 10 * 60000);
31
+
32
+                var week = Business.cachedWeeks.find(function(week)
33
+                {
34
+                    return data.group_id == week.data.group_id &&
35
+                        data.type_id == week.data.type_id &&
36
+                        data.week_id == week.data.week_id &&
37
+                        limitDate <= week.time;
38
+                });
39
+                if (week != null) {
40
+                    return week.week;
41
+                }
42
+                return null;
43
+            };
44
+
13 45
             Business.formatWeek = function(data)
14 46
             {
15 47
                 data.DayList.forEach(function(day) {
@@ -53,24 +85,39 @@
53 85
             Business.getWeek = function(data, promise)
54 86
             {
55 87
                 var defer = $q.defer();
56
-                WeeksDataAccess.getWeek(data, promise)
57
-                    .then(function(data)
58
-                    {
59
-                        Business.formatWeek(data);
60
-                        defer.resolve(data);
61
-                    }, defer.reject);
88
+                var week = Business.getCachedWeek(data);
89
+                if (week == null) {
90
+                    WeeksDataAccess.getWeek(data, promise)
91
+                        .then(function (week) {
92
+                            Business.formatWeek(week);
93
+                            Business.cacheWeek(week, data);
94
+                            defer.resolve(week);
95
+                        }, defer.reject);
96
+                }
97
+                else {
98
+                    defer.resolve(week);
99
+                }
62 100
                 return defer.promise;
63 101
             };
64 102
 
65 103
             Business.getCurrentWeek = function(data, promise)
66 104
             {
105
+                data.week_id = Business.currentWeekId;
67 106
                 var defer = $q.defer();
68
-                WeeksDataAccess.getCurrentWeek(data, promise)
69
-                    .then(function(data)
70
-                    {
71
-                        Business.formatWeek(data);
72
-                        defer.resolve(data);
73
-                    }, defer.reject);
107
+                var week = Business.getCachedWeek(data);
108
+                if (week == null) {
109
+                    WeeksDataAccess.getCurrentWeek(data, promise)
110
+                        .then(function (week) {
111
+                            Business.formatWeek(week);
112
+                            Business.currentWeekId = week.Id;
113
+                            data.week_id = week.Id;
114
+                            Business.cacheWeek(week, data);
115
+                            defer.resolve(week);
116
+                        }, defer.reject);
117
+                }
118
+                else {
119
+                    defer.resolve(week);
120
+                }
74 121
                 return defer.promise;
75 122
             };
76 123
 

Loading…
Cancel
Save