summaryrefslogtreecommitdiffstats
path: root/share/common/utils/aai_cache.py
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2018-12-25 10:42:52 +0000
committerBin Yang <bin.yang@windriver.com>2018-12-25 10:42:52 +0000
commite628d07247f788d20c06e8d310070a5bc66917f7 (patch)
tree0efc08d1181bd26b268190c36e06ba50fa146ea2 /share/common/utils/aai_cache.py
parent7994c7757b13e052bffc2c3774b70f0a5ad7da79 (diff)
Refactor the AAI cache for multicloud plugin3.0.1-ONAP1.2.3
Fix the cache issue of cloud region which result in failure of cloud region registration Change-Id: Id13a40124efb92bd818686e069c9335ecd0d07c1 Issue-ID: MULTICLOUD-431 Signed-off-by: Bin Yang <bin.yang@windriver.com>
Diffstat (limited to 'share/common/utils/aai_cache.py')
-rw-r--r--share/common/utils/aai_cache.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/share/common/utils/aai_cache.py b/share/common/utils/aai_cache.py
new file mode 100644
index 00000000..53298bb8
--- /dev/null
+++ b/share/common/utils/aai_cache.py
@@ -0,0 +1,53 @@
+# Copyright (c) 2017-2018 Wind River Systems, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
+import json
+import logging
+from django.core.cache import cache
+
+logger = logging.getLogger(__name__)
+
+# note: memcached key length should be < 250, the value < 1MB
+
+def flush_cache_by_url(resource_url):
+ try:
+ cache.delete("AAI_" + resource_url)
+ except:
+ pass # silently drop any exception
+
+
+def get_cache_by_url(resource_url):
+ try:
+ if (filter_cache_by_url(resource_url)):
+ value = cache.get("AAI_" + resource_url)
+ return json.loads(value) if value else None
+ else:
+ return None
+ except:
+ return None
+
+
+def set_cache_by_url(resource_url, resource_in_json):
+ try:
+ # filter out unmanaged AAI resource
+ if filter_cache_by_url(resource_url):
+ # cache the resource for 24 hours
+ logger.debug("Cache the resource: "+ resource_url)
+ cache.set("AAI_" + resource_url, json.dumps(resource_in_json), 3600 * 24)
+ except:
+ pass
+
+def filter_cache_by_url(resource_url):
+ # hardcoded filter: cloud region only
+ if resource_url.find(r"cloud-infrastructure/cloud-regions/cloud-region") > 0:
+ return True
+ else:
+ return False \ No newline at end of file