Commit f1891cd5 authored by Marien Fressinaud's avatar Marien Fressinaud

add: Add cache for the articles

parent 1c676f10
......@@ -4,6 +4,7 @@ import os
import sys
import distutils.dir_util
import locale
import pickle
import time
import boop.environment as environment
......@@ -22,6 +23,11 @@ def main(environment):
output_path = os.path.join(os.curdir, "_site")
utils.init_output_folder(output_path)
# Init cache dir
cache_path = os.path.join(os.curdir, "_cache")
if not os.path.exists(cache_path):
os.mkdir(cache_path)
# Load the configuration (it can be overidden by a configuration.yml file)
configuration_path = os.path.join(os.curdir, "configuration.yml")
configuration = config.load(configuration_path, environment, output_path)
......@@ -50,6 +56,12 @@ def main(environment):
)
pages.append(page)
# Load articles from the cache
articles_cache_path = os.path.join(cache_path, "articles.pickle")
if os.path.exists(articles_cache_path):
with open(articles_cache_path, "rb") as cache_file:
articles = pickle.load(cache_file)
# Check that articles directory exists and load Articles from it
articles_path = os.path.join(os.curdir, "articles")
if os.path.isdir(articles_path):
......@@ -73,10 +85,21 @@ def main(environment):
serie_articles = []
for filename in utils.list_files(dirpath, only=["md"]):
article_filepath = os.path.join(dirpath, filename)
article = utils.build_article_from_filepath(
article_filepath, configuration, serie
)
articles[article_filepath] = article
reload_article = True
if article_filepath in articles:
# the article is in cache
article = articles[article_filepath]
last_modification = os.path.getmtime(article_filepath)
# if the two last_modification are different, the file has
# changed and we need to reload the article from the file.
reload_article = article.last_modification() != last_modification
if reload_article:
article = utils.build_article_from_filepath(
article_filepath, configuration, serie
)
articles[article_filepath] = article
serie_articles.append(article)
if serie:
......@@ -129,6 +152,10 @@ def main(environment):
distutils.dir_util.copy_tree(static_dirpath, output_path)
print(f"Static files copied")
# STEP 4: save cache!
with open(articles_cache_path, "wb") as cache_file:
pickle.dump(articles, cache_file)
ellapsed = time.perf_counter()
print(f"Generated in {ellapsed - start:.2f}s")
......
......@@ -30,6 +30,7 @@ class Article:
self.meta[article_key] = values[0]
self.meta["ARTICLE_CONTENT"] = content
self.meta["ARTICLE_SERIE"] = serie
self.meta["ARTICLE_LAST_MODIFICATION"] = datetime.datetime.now()
def render(self):
return self.template.render(self.meta)
......@@ -88,3 +89,6 @@ class Article:
def template(self):
return self.meta["ARTICLE_TEMPLATE"]
def last_modification(self):
return self.meta["ARTICLE_LAST_MODIFICATION"]
......@@ -24,12 +24,15 @@ def init_output_folder(output_path):
def build_article_from_filepath(filepath, configuration, serie=None):
"""Read a file and initialize an Article from it.
"""
last_modification = os.path.getmtime(filepath)
filepath_no_ext, _ = os.path.splitext(filepath)
slug = os.path.basename(filepath_no_ext)
with open(filepath, "r") as article_file:
return Article(
article = Article(
slug, article_file.read(), configuration=configuration, serie=serie
)
article.meta["ARTICLE_LAST_MODIFICATION"] = last_modification
return article
def build_page_from_filepath(filepath, configuration, basename=None, slug=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment