Projet

Général

Profil

0001-add-a-layer-of-cache-using-x-combo-page-id-for-remot.patch

Benjamin Dauvergne, 01 octobre 2016 11:48

Télécharger (3,99 ko)

Voir les différences:

Subject: [PATCH] add a layer of cache using x-combo-page-id for remote
 templates

 hobo/context_processors.py | 47 +++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
hobo/context_processors.py
30 30
    template_vars.update({'statics_hash': statics_hash.hexdigest()})
31 31
    return template_vars
32 32

  
33

  
33 34
class RemoteTemplate(object):
34 35
    def __init__(self, source):
35 36
        self.source = source
36 37

  
37 38
    @property
38 39
    def cache_key(self):
39
         return hashlib.md5(urlparse.urlunparse(
40
             urlparse.urlparse(self.source)[:3] + ('', '', ''))).hexdigest()
40
        return hashlib.md5(urlparse.urlunparse(
41
            urlparse.urlparse(self.source)[:3] + ('', '', ''))).hexdigest()
41 42

  
42 43
    def get_template(self):
43
        item = cache.get(self.cache_key)
44
        if item is None:
44
        x_combo_page_id = cache.get(self.cache_key)
45
        if x_combo_page_id is None:
45 46
            template_body = self.update_content()
46 47
            if template_body is None:
47 48
                raise Exception('Failed to retrieve theme')
48 49
        else:
49
            template_body, expiry_time = item
50
            if expiry_time < datetime.datetime.now():
51
                # stale value, put it back into the cache for other consumers and
52
                # update the content in a different thread
53
                self.cache(template_body)
54
                threading.Thread(target=self.update_content).start()
50
            combo_page_id, combo_page_id_expiry_time = x_combo_page_id
51

  
52
            item = cache.get(self.combo_page_id_cache_key(combo_page_id))
53
            if item is None:
54
                template_body = self.update_content()
55
                if template_body is None:
56
                    raise Exception('Failed to retrieve theme')
57
            else:
58
                template_body, expiry_time = item
59
                if max(expiry_time, combo_page_id_expiry_time) < datetime.datetime.now():
60
                    # stale value, put it back into the cache for other consumers and
61
                    # update the content in a different thread
62
                    # we do not put the template body back in the cache as we do not know if this
63
                    # page is going to resolve to the same X-Combo-Page-Id for this update round
64
                    self.cache(template_body, update_combo_page_id_cache=False)
65
                    threading.Thread(target=self.update_content).start()
55 66
        return Template(template_body)
56 67

  
57 68
    def update_content(self):
......
59 70
        if r.status_code != 200:
60 71
            logger.error('failed to retrieve theme')
61 72
            return None
62
        self.cache(r.text)
73
        self.cache(r.headers['x-combo-page-id'], r.text)
63 74
        return r.text
64 75

  
65
    def cache(self, template_body):
76
    def combo_page_id_cache_key(self, combo_page_id):
77
        return hashlib.md5('combo-page-id-' + combo_page_id).hexdigest()
78

  
79
    def cache(self, combo_page_id, template_body, update_combo_page_id_cache=True):
66 80
        expiry_time = datetime.datetime.now() + datetime.timedelta(seconds=CACHE_REFRESH_TIMEOUT)
67
        cache.set(self.cache_key,
68
                (template_body, expiry_time),
69
                2592000) # bypass cache level expiration time
81
        cache.set(self.cache_key, (combo_page_id, expiry_time),
82
                  2592000)  # bypass cache level expiration time
83
        if update_combo_page_id_cache:
84
            cache.set(self.combo_page_id_cache_key(combo_page_id), (template_body, expiry_time),
85
                      2592000)  # bypass cache level expiration time
86

  
70 87

  
71 88
def theme_base(request):
72 89
    # this context processor adds two variables to context:
73
-