Skip to content

Commit

Permalink
fix: remove LRU cache
Browse files Browse the repository at this point in the history
  • Loading branch information
LogCreative committed Feb 27, 2024
1 parent cd79b0d commit a209890
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TikzEdt is the inspiration of this project, to create LaTeX TikZ graph in WYSIWY
- Install TeX distribution on your local computer.
- Install python and the dependencies by
```bash
pip install flask cachetools
pip install flask
```
- Start the server.
```bash
Expand Down
2 changes: 1 addition & 1 deletion deploy/gunicorn-deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def load(self):
'bind': '%s:%s' % ('0.0.0.0', '5678'),
'workers': number_of_workers(),
}
deployApp = create_app(max_size=20, timeout=30)
deployApp = create_app(timeout=30)
StandaloneApplication(deployApp, options).run()
44 changes: 14 additions & 30 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import signal
import platform
import glob
from cachetools import LRUCache
import multiprocessing

from flask import Flask, send_from_directory, render_template_string, Response, request

def create_app(max_size: int = 1000, timeout: int = 100000):
def create_app(timeout: int = 100000):

app = Flask(__name__, static_url_path='', static_folder=".", template_folder=".")

Expand Down Expand Up @@ -54,30 +53,13 @@ def get_header_body(tex: str, sessid: str):
return tex[:header_end] + "\\begin{document}\n\\end{document}\n", \
"%&{}\n".format(get_header_name(sessid)) + tex[header_end:]

def clear_files(file_prefix: str):
for filepath in glob.glob("{}/{}*".format(tmpdir, file_prefix)):
os.remove(filepath)

class HeaderLRUCache(LRUCache):
def popitem(self):
key, value = super().popitem()
clear_files(key) # only header related
return key, value

class BodyLRUCache(LRUCache):
def popitem(self):
key, value = super().popitem()
clear_files(key + ".") # only body related
return key, value

header_cache = HeaderLRUCache(maxsize=max_size)
body_cache = BodyLRUCache(maxsize=max_size)

def same_or_write(cache: LRUCache, filename: str, cur_content: str):
if filename in cache and cache[filename] == cur_content:
return False # same as before
else:
cache[filename] = cur_content
def same_or_write(filename: str, cur_content: str):
filepath = os.path.join(tmpdir, "{}.tex".format(filename))
if os.path.isfile(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
prev_content = f.read()
if prev_content == cur_content:
return False # the same as before
filepath = os.path.join(tmpdir, "{}.tex".format(filename))
pdfpath = os.path.join(tmpdir, "{}.pdf".format(filename))
if os.path.isfile(pdfpath):
Expand All @@ -93,7 +75,7 @@ def clean_log(filename: str):

def compile_header(cur_header: str, sessid: str):
header_name = get_header_name(sessid)
if same_or_write(header_cache, header_name, cur_header):
if same_or_write(header_name, cur_header):
clean_log(header_name)
clean_log(get_body_name(sessid))
run_cmd(
Expand All @@ -102,7 +84,7 @@ def compile_header(cur_header: str, sessid: str):

def compile_body(cur_body: str, sessid: str):
body_name = get_body_name(sessid)
if same_or_write(body_cache, body_name, cur_body):
if same_or_write(body_name, cur_body):
clean_log(body_name)
run_cmd('pdflatex -interaction=nonstopmode -halt-on-error {}.tex'.format(body_name))

Expand All @@ -125,16 +107,18 @@ def compile_tex(tex: str, sessid: str):
clean_files(sessid, pdf=False)
return pdf
except ValueError as e: # raise by LRUCache when the content is too large.
clean_files(sessid, pdf=True)
app.logger.warning("Content Length Error on Session {}: {}".format(sessid, e))
raise Exception("Content length is too large.")
except subprocess.TimeoutExpired as e:
clean_files(sessid, pdf=True)
app.logger.warning("Compilation timeout for session {}: {}".format(sessid, e))
raise Exception("Compilation timeout.")
except Exception as e:
clean_files(sessid, pdf=True)
app.logger.warning("Compilation error for session {}: {}".format(sessid, e))
raise Exception("Compilation Error.")
finally:
clean_files(sessid, pdf=True)
clean_files(sessid, pdf=True)
return None

def get_log(sessid: str):
Expand Down

0 comments on commit a209890

Please # to comment.