httpCache.svc.js 7.57 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
// web service proxy for menu
webservices.factory('httpCacheService', ['$http', '$q', 'apiConfig', 'CacheFactory', '$log', 'cacheService',
    function ($http, $q, apiConfig, CacheFactory, $log, cacheService) {
        'use strict';

        var deferred, promise;

        //Tell $http to use a cache created by CacheFactory for a specific request
        var getData = function (webApiUrl, isDbRequest) {

            deferred = $q.defer();
            promise = deferred.promise;
            var cacheName = constant.cache.cacheName;
            var start = new Date().getTime();
            var isFromCache = true;
            var cacheKey = constant.webapi.prefix + webApiUrl;
            var slashIndex = PWC.nIndexOf(webApiUrl, '/', 2);
            var questionMarkIndex = PWC.nIndexOf(webApiUrl, '?', 1);
            var routePrefix, dataCache;

            // Check to make sure the cache doesn't already exist
            //get method might be return error,so use the following way

            if (!CacheFactory.get(cacheName)) {
                CacheFactory(cacheName, {
                    storageMode: constant.cache.storageMode, // This cache will use `localStorage`.
                });
            }

            dataCache = CacheFactory.get(cacheName);

            if (slashIndex >= 0) {
                routePrefix = constant.webapi.prefix + webApiUrl.substring(0, slashIndex);
            } else if (questionMarkIndex >= 0) {
                routePrefix = constant.webapi.prefix + webApiUrl.substring(0, questionMarkIndex);
            } else {
                routePrefix = constant.webapi.prefix + webApiUrl;
                console.warn('need to verify the routePrefix in httpCache.svc.js');
            }

            // Now that control of inserting/removing from the cache is in our hands,
            // we can interact with the data in "dataCache" outside of this context,
            // e.g. Modify the data after it has been returned from the server and
            // save those modifications to the cache.
            this.finishedCache = false;
            if (dataCache.get(cacheKey) && !isDbRequest) {
                var data = dataCache.get(cacheKey);
                deferred.resolve(data.cacheResult);
                isFromCache = true;
                logTimeTakenInfo(cacheKey, start, isFromCache);

                promise = deferred.promise;
                this.finishedCache = true;
                return this;
            } else {
                var that = this;
                $http.get(webApiUrl, apiConfig.create()).success(function (data) {
                    cacheService.getCacheByKey(routePrefix).success(function (cache) {

                        var cacheResult = {};

                        isFromCache = false;
                        deferred.resolve(data);
                        logTimeTakenInfo(cacheKey, start, isFromCache);

                        //if could get the last modify time from cache, then align the same cache time
                        if (!cache && typeof (cache) != constant.common.undefined && cache != 0) {
                            cacheResult = { 'cacheResult': data, 'cacheTime': constant.cache.defaultCacheTime };

                        } else {
                            cacheResult = { 'cacheResult': data, 'cacheTime': cache.lastModifyTime };
                        }

                        promise = deferred.promise;
                        dataCache.put(cacheKey, cacheResult);
                        that.finishedCache = true;
                    });
                });
            }

            return this;
        };

        var promiseSuccess = function (fn) {
            promise.then(function (data) {
                fn(data);
            });
            return this;
        };

        var promiseError = function (fn) {
            promise.then(null, function (response) {
                fn(data);
            });
            return this;
        };

        var logTimeTakenInfo = function (cacheKey, start, isFromCache) {
            if (isFromCache) {
                $log.info('time taken from cache - ' + cacheKey + ': ' + (new Date().getTime() - start) + 'ms');
            } else {
                $log.info('time taken from server - ' + cacheKey + ': ' + (new Date().getTime() - start) + 'ms');
            }
        };


        // 加这个方法的原因是,getData 方法,在处理多个请求并发从后端取数据就会出现问题,可能是因为promise已经被替换成最新的了。
        var getCache = function (webApiUrl, isDbRequest, fn) {

            var cacheName = constant.cache.cacheName;
            var start = new Date().getTime();
            var isFromCache = true;
            var cacheKey = constant.webapi.prefix + webApiUrl;
            var slashIndex = PWC.nIndexOf(webApiUrl, '/', 2);
            var questionMarkIndex = PWC.nIndexOf(webApiUrl, '?', 1);
            var routePrefix, dataCache;

            // Check to make sure the cache doesn't already exist
            //get method might be return error,so use the following way

            if (!CacheFactory.get(cacheName)) {
                CacheFactory(cacheName, {
                    storageMode: constant.cache.storageMode, // This cache will use `localStorage`.
                });
            }

            dataCache = CacheFactory.get(cacheName);

            if (slashIndex >= 0) {
                routePrefix = constant.webapi.prefix + webApiUrl.substring(0, slashIndex);
            } else if (questionMarkIndex >= 0) {
                routePrefix = constant.webapi.prefix + webApiUrl.substring(0, questionMarkIndex);
            } else {
                routePrefix = constant.webapi.prefix + webApiUrl;
                console.warn('need to verify the routePrefix in httpCache.svc.js');
            }

            // Now that control of inserting/removing from the cache is in our hands,
            // we can interact with the data in "dataCache" outside of this context,
            // e.g. Modify the data after it has been returned from the server and
            // save those modifications to the cache.
            if (dataCache.get(cacheKey) && !isDbRequest) {
                var data = dataCache.get(cacheKey);

                isFromCache = true;
                logTimeTakenInfo(cacheKey, start, isFromCache);


                if (fn) {
                    fn(data.cacheResult);
                }
            } else {

                $http.get(webApiUrl, apiConfig.create()).success(function (data) {
                    cacheService.getCacheByKey(routePrefix).success(function (cache) {

                        var cacheResult = {};

                        isFromCache = false;


                        logTimeTakenInfo(cacheKey, start, isFromCache);

                        //if could get the last modify time from cache, then align the same cache time
                        if (!cache && typeof (cache) != constant.common.undefined && cache != 0) {
                            cacheResult = { 'cacheResult': data, 'cacheTime': constant.cache.defaultCacheTime };

                        } else {
                            cacheResult = { 'cacheResult': data, 'cacheTime': cache.lastModifyTime };
                        }

                        dataCache.put(cacheKey, cacheResult);
                        //promiseSuccess(thisObj.success);
                        if (fn) {
                            fn(data);
                        }
                    });
                });
            }
        };

        return {
            get: getData,
            success: promiseSuccess,
            error: promiseError,
            getCache: getCache
        };
    }]);