cache the data client-side to speed things up

This commit is contained in:
ziirish 2018-01-10 17:35:26 +01:00
parent c545e68200
commit 5fd3fbd835
No known key found for this signature in database
GPG key ID: 72DB229A64B54E46
2 changed files with 27 additions and 2 deletions

View file

@ -32,11 +32,12 @@ EXEMPT_METHODS = set(['OPTIONS'])
def cache_key():
key = '{}-{}-{}-{}-{}'.format(
key = '{}-{}-{}-{}-{}-{}'.format(
session.get('login', uuid.uuid4()),
request.path,
request.values,
request.headers.get('X-Session-Tag', ''),
request.cookies,
session.get('language', '')
)
key = hashlib.sha256(to_bytes(key)).hexdigest()

View file

@ -13,13 +13,14 @@ from . import api, cache_key
from ..server import BUIServer # noqa
from .custom import fields, Resource
from .custom.inputs import boolean
from ..decorators import browser_cache
from ..ext.cache import cache
from ..exceptions import BUIserverException
from ..utils import NOTIF_ERROR
from six import iteritems
from flask_restplus.marshalling import marshal
from flask import current_app
from flask import current_app, request
from flask_login import current_user
bui = current_app # type: BUIServer
@ -154,9 +155,18 @@ class ClientTree(Resource):
required=False,
default=False
)
parser.add_argument(
'init',
type=boolean,
help='First call to load the root of the tree',
nullable=True,
required=False,
default=False
)
@cache.cached(timeout=3600, key_prefix=cache_key)
@ns.marshal_list_with(node_fields, code=200, description='Success')
@browser_cache(3600)
@ns.expect(parser)
@ns.doc(
responses={
@ -224,6 +234,20 @@ class ClientTree(Resource):
not current_user.acl.is_client_allowed(name, server):
self.abort(403, 'Sorry, you are not allowed to view this client')
from_cookie = None
if args['init'] and not root_list:
from_cookie = request.cookies.get('fancytree-1-expanded', '')
if from_cookie:
args['recursive'] = True
_root = bui.client.get_tree(name, backup, agent=server)
root_list = [x['name'] for x in _root]
for path in from_cookie.split('~'):
if not path.endswith('/'):
path += '/'
if path not in root_list:
root_list.append(path)
root_list = sorted(root_list)
try:
root_list_clean = []