You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2019/06/17 14:23:16 UTC

[airavata-django-portal] 03/11: AIRAVATA-2990 Adding simple response cache mechanism

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit 0ad795d5453b6c61f2f4f32159a98aefb5ba7848
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Thu Jun 6 15:49:09 2019 -0400

    AIRAVATA-2990 Adding simple response cache mechanism
---
 .../js/services/ServiceFactory.js                  |  8 ++--
 .../static/django_airavata_api/js/utils/Cache.js   | 53 ++++++++++++++++++++++
 .../django_airavata_api/js/utils/FetchUtils.js     | 33 ++++++++++++--
 3 files changed, 87 insertions(+), 7 deletions(-)

diff --git a/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js b/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js
index 855d600..94db8b2 100644
--- a/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js
+++ b/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js
@@ -211,9 +211,10 @@ class ServiceFactory {
       let queryParamsMapping = parseQueryMapping(config.queryParams);
       serviceObj[functionName] = function(
         params = {},
-        { ignoreErrors, showSpinner } = {
+        { ignoreErrors, showSpinner, cache } = {
           ignoreErrors: false,
-          showSpinner: true
+          showSpinner: true,
+          cache: false
         }
       ) {
         let url = config.url;
@@ -287,7 +288,8 @@ class ServiceFactory {
             } else {
               return FetchUtils.get(url, queryParams, {
                 ignoreErrors,
-                showSpinner
+                showSpinner,
+                cache
               }).then(paginationHandler);
             }
           case putKey:
diff --git a/django_airavata/apps/api/static/django_airavata_api/js/utils/Cache.js b/django_airavata/apps/api/static/django_airavata_api/js/utils/Cache.js
new file mode 100644
index 0000000..da6728b
--- /dev/null
+++ b/django_airavata/apps/api/static/django_airavata_api/js/utils/Cache.js
@@ -0,0 +1,53 @@
+const DEFAULT_EXPIRATION_TIME_MS = 5 * 60 * 1000;
+
+class CacheEntry {
+  constructor(value, expireDate) {
+    this._value = value;
+    this._expireDate = expireDate;
+  }
+
+  get value() {
+    return this._value;
+  }
+
+  get isExpired() {
+    return this._expireDate.getTime() < Date.now();
+  }
+}
+
+export default class Cache {
+  constructor() {
+    this._cache = {};
+  }
+
+  get(key) {
+    if (this.has(key)) {
+      const cacheEntry = this._cache[key];
+      return cacheEntry.value;
+    } else {
+      return null;
+    }
+  }
+
+  put({
+    key,
+    value,
+    expireDate = new Date(Date.now() + DEFAULT_EXPIRATION_TIME_MS)
+  }) {
+    this._cache[key] = new CacheEntry(value, expireDate);
+  }
+
+  has(key) {
+    if (this._cache.hasOwnProperty(key)) {
+      const cacheEntry = this._cache[key];
+      if (cacheEntry.isExpired) {
+        delete this._cache[key];
+        return false;
+      } else {
+        return true;
+      }
+    } else {
+      return false;
+    }
+  }
+}
diff --git a/django_airavata/apps/api/static/django_airavata_api/js/utils/FetchUtils.js b/django_airavata/apps/api/static/django_airavata_api/js/utils/FetchUtils.js
index 11efe6b..d863345 100644
--- a/django_airavata/apps/api/static/django_airavata_api/js/utils/FetchUtils.js
+++ b/django_airavata/apps/api/static/django_airavata_api/js/utils/FetchUtils.js
@@ -1,4 +1,5 @@
 import UnhandledErrorDispatcher from "../errors/UnhandledErrorDispatcher";
+import Cache from "./Cache";
 
 var count = 0;
 const parseQueryParams = function(url, queryParams = "") {
@@ -37,6 +38,8 @@ const decrementCount = function() {
   }
 };
 
+const responseCache = new Cache();
+
 export default {
   enableSpinner: function() {},
   disableSpinner: function() {},
@@ -70,7 +73,11 @@ export default {
     url,
     body,
     queryParams = "",
-    { mediaType = "application/json", ignoreErrors = false, showSpinner = true } = {}
+    {
+      mediaType = "application/json",
+      ignoreErrors = false,
+      showSpinner = true
+    } = {}
   ) {
     var headers = this.createHeaders(mediaType);
     // Browsers automatically handle content type for FormData request bodies
@@ -93,7 +100,11 @@ export default {
   put: function(
     url,
     body,
-    { mediaType = "application/json", ignoreErrors = false, showSpinner = true } = {}
+    {
+      mediaType = "application/json",
+      ignoreErrors = false,
+      showSpinner = true
+    } = {}
   ) {
     var headers = this.createHeaders(mediaType);
     return this.processFetch(url, {
@@ -111,7 +122,12 @@ export default {
   get: function(
     url,
     queryParams = "",
-    { mediaType = "application/json", ignoreErrors = false, showSpinner = true } = {}
+    {
+      mediaType = "application/json",
+      ignoreErrors = false,
+      showSpinner = true,
+      cache = false
+    } = {}
   ) {
     if (queryParams && typeof queryParams != "string") {
       queryParams = Object.keys(queryParams)
@@ -124,14 +140,23 @@ export default {
     if (queryParams) {
       url = url + "?" + queryParams;
     }
+    if (cache) {
+      if (responseCache.has(url)) {
+        return responseCache.get(url);
+      }
+    }
     var headers = this.createHeaders(mediaType);
-    return this.processFetch(url, {
+    const fetchRequest = this.processFetch(url, {
       method: "get",
       headers: headers,
       credentials: "same-origin",
       ignoreErrors,
       showSpinner
     });
+    if (cache) {
+      responseCache.put({ key: url, value: fetchRequest });
+    }
+    return fetchRequest;
   },
   delete: function(url, { ignoreErrors = false, showSpinner = true } = {}) {
     var headers = this.createHeaders();