mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make various global caches used by the server thread-safe
This commit is contained in:
parent
8405be4ebb
commit
043d345287
@ -8,6 +8,7 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
import os
|
||||
from io import BytesIO
|
||||
from threading import Lock
|
||||
|
||||
from calibre import fit_image
|
||||
from calibre.constants import config_dir
|
||||
@ -27,6 +28,7 @@ from calibre.utils.magick.draw import thumbnail, Image
|
||||
plugboard_content_server_value = 'content_server'
|
||||
plugboard_content_server_formats = ['epub', 'mobi', 'azw3']
|
||||
update_metadata_in_fmts = frozenset(plugboard_content_server_formats)
|
||||
lock = Lock()
|
||||
|
||||
# Get book formats/cover as a cached filesystem file {{{
|
||||
|
||||
@ -45,6 +47,8 @@ def create_file_copy(ctx, rd, prefix, library_id, book_id, ext, mtime, copy_func
|
||||
fname = os.path.join(base, bname)
|
||||
do_copy = True
|
||||
mtime = timestampfromdt(mtime)
|
||||
|
||||
# TODO: Implement locking for this cache
|
||||
try:
|
||||
ans = lopen(fname, 'r+b')
|
||||
do_copy = os.fstat(ans.fileno()).st_mtime < mtime
|
||||
@ -167,6 +171,7 @@ def icon(ctx, rd, which):
|
||||
return lopen(path, 'rb')
|
||||
except EnvironmentError:
|
||||
raise HTTPNotFound()
|
||||
with lock:
|
||||
tdir = os.path.join(rd.tdir, 'icons')
|
||||
cached = os.path.join(tdir, '%d-%s.png' % (sz, which))
|
||||
try:
|
||||
|
@ -67,6 +67,7 @@ class Context(object):
|
||||
self.opts = opts
|
||||
self.library_broker = LibraryBroker(libraries)
|
||||
self.testing = testing
|
||||
self.lock = Lock()
|
||||
|
||||
def init_session(self, endpoint, data):
|
||||
pass
|
||||
@ -80,6 +81,7 @@ class Context(object):
|
||||
def allowed_book_ids(self, data, db):
|
||||
# TODO: Implement this based on data.username caching result on the
|
||||
# data object
|
||||
with self.lock:
|
||||
ans = data.allowed_book_ids.get(db.server_library_id)
|
||||
if ans is None:
|
||||
ans = data.allowed_book_ids[db.server_library_id] = db.all_book_ids()
|
||||
@ -88,6 +90,7 @@ class Context(object):
|
||||
def get_categories(self, data, db, restrict_to_ids=None):
|
||||
if restrict_to_ids is None:
|
||||
restrict_to_ids = self.allowed_book_ids(data, db)
|
||||
with self.lock:
|
||||
cache = self.library_broker.category_caches[db.server_library_id]
|
||||
old = cache.pop(restrict_to_ids, None)
|
||||
if old is None or old[0] <= db.last_modified():
|
||||
@ -102,6 +105,7 @@ class Context(object):
|
||||
def search(self, data, db, query, restrict_to_ids=None):
|
||||
if restrict_to_ids is None:
|
||||
restrict_to_ids = self.allowed_book_ids(data, db)
|
||||
with self.lock:
|
||||
cache = self.library_broker.search_caches[db.server_library_id]
|
||||
key = (query, restrict_to_ids)
|
||||
old = cache.pop(key, None)
|
||||
|
Loading…
x
Reference in New Issue
Block a user