File: cache.py

package info (click to toggle)
murano-dashboard 1%3A10.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,620 kB
  • sloc: python: 15,632; javascript: 1,627; sh: 106; makefile: 38
file content (85 lines) | stat: -rw-r--r-- 2,659 bytes parent folder | download | duplicates (5)
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
#    Copyright (c) 2014 Mirantis, 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. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import functools
import os

from oslo_log import log as logging

from muranodashboard.common import utils
from muranodashboard.environments import consts


LOG = logging.getLogger(__name__)
OBJS_PATH = os.path.join(consts.CACHE_DIR, 'apps')

if not os.path.exists(OBJS_PATH):
    os.makedirs(OBJS_PATH)
    LOG.info('Creating apps cache directory located at {dir}'.
             format(dir=OBJS_PATH))

LOG.info('Using apps cache directory located at {dir}'.
         format(dir=OBJS_PATH))


def _get_entry_path(app_id):
    head, tail = app_id[:2], app_id[2:]
    head = os.path.join(OBJS_PATH, head)
    if not os.path.exists(head):
        os.mkdir(head)
    tail = os.path.join(head, tail)
    if not os.path.exists(tail):
        os.mkdir(tail)
    return tail


def _load_from_file(file_name):
    if os.path.isfile(file_name) and os.path.getsize(file_name) > 0:
        with open(file_name, 'rb') as f:
            p = utils.CustomUnpickler(f)
            return p.load()
    return None


def _save_to_file(file_name, content):
    dir_path = os.path.dirname(file_name)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    with open(file_name, 'wb') as f:
        p = utils.CustomPickler(f)
        p.dump(content)


def with_cache(*dst_parts):
    def _decorator(func):
        @functools.wraps(func)
        def __inner(request, app_id):
            path = os.path.join(_get_entry_path(app_id), *dst_parts)
            # Remove file extensions since file content is pickled and
            # could not be open as usual files
            path = os.path.splitext(path)[0] + '-pickled'
            content = _load_from_file(path)
            if content is None:
                content = func(request, app_id)
                if content:
                    LOG.debug('Caching value at {0}.'.format(path))
                    _save_to_file(path, content)
            else:
                LOG.debug('Using cached value from {0}.'.format(path))

            return content

        return __inner

    return _decorator