mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Cache json encoded form of tag browser
This commit is contained in:
parent
30a8f1298b
commit
c28d3d5025
@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import os
|
import os, json
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
@ -137,7 +137,10 @@ class Context(object):
|
|||||||
old = cache.pop(key, None)
|
old = cache.pop(key, None)
|
||||||
if old is None or old[0] <= db.last_modified():
|
if old is None or old[0] <= db.last_modified():
|
||||||
categories = db.get_categories(book_ids=restrict_to_ids, sort=opts.sort_by, first_letter_sort=opts.collapse_model == 'first letter')
|
categories = db.get_categories(book_ids=restrict_to_ids, sort=opts.sort_by, first_letter_sort=opts.collapse_model == 'first letter')
|
||||||
cache[key] = old = (utcnow(), render(db, categories))
|
data = json.dumps(render(db, categories), ensure_ascii=False)
|
||||||
|
if isinstance(data, type('')):
|
||||||
|
data = data.encode('utf-8')
|
||||||
|
cache[key] = old = (utcnow(), data)
|
||||||
if len(cache) > self.CATEGORY_CACHE_SIZE:
|
if len(cache) > self.CATEGORY_CACHE_SIZE:
|
||||||
cache.popitem(last=False)
|
cache.popitem(last=False)
|
||||||
else:
|
else:
|
||||||
|
@ -18,6 +18,9 @@ default_methods = frozenset(('HEAD', 'GET'))
|
|||||||
|
|
||||||
def json(ctx, rd, endpoint, output):
|
def json(ctx, rd, endpoint, output):
|
||||||
rd.outheaders['Content-Type'] = 'application/json; charset=UTF-8'
|
rd.outheaders['Content-Type'] = 'application/json; charset=UTF-8'
|
||||||
|
if isinstance(output, bytes):
|
||||||
|
ans = output # Assume output is already UTF-8 encoded json
|
||||||
|
else:
|
||||||
ans = jsonlib.dumps(output, ensure_ascii=False)
|
ans = jsonlib.dumps(output, ensure_ascii=False)
|
||||||
if not isinstance(ans, bytes):
|
if not isinstance(ans, bytes):
|
||||||
ans = ans.encode('utf-8')
|
ans = ans.encode('utf-8')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user