Commit f1891cd5 authored by berumuron's avatar berumuron
Browse files

add: Add cache for the articles

parent 1c676f10
...@@ -4,6 +4,7 @@ import os ...@@ -4,6 +4,7 @@ import os
import sys import sys
import distutils.dir_util import distutils.dir_util
import locale import locale
import pickle
import time import time
import boop.environment as environment import boop.environment as environment
...@@ -22,6 +23,11 @@ def main(environment): ...@@ -22,6 +23,11 @@ def main(environment):
output_path = os.path.join(os.curdir, "_site") output_path = os.path.join(os.curdir, "_site")
utils.init_output_folder(output_path) 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) # Load the configuration (it can be overidden by a configuration.yml file)
configuration_path = os.path.join(os.curdir, "configuration.yml") configuration_path = os.path.join(os.curdir, "configuration.yml")
configuration = config.load(configuration_path, environment, output_path) configuration = config.load(configuration_path, environment, output_path)
...@@ -50,6 +56,12 @@ def main(environment): ...@@ -50,6 +56,12 @@ def main(environment):
) )
pages.append(page) 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 # Check that articles directory exists and load Articles from it
articles_path = os.path.join(os.curdir, "articles") articles_path = os.path.join(os.curdir, "articles")
if os.path.isdir(articles_path): if os.path.isdir(articles_path):
...@@ -73,10 +85,21 @@ def main(environment): ...@@ -73,10 +85,21 @@ def main(environment):
serie_articles = [] serie_articles = []
for filename in utils.list_files(dirpath, only=["md"]): for filename in utils.list_files(dirpath, only=["md"]):
article_filepath = os.path.join(dirpath, filename) article_filepath = os.path.join(dirpath, filename)
article = utils.build_article_from_filepath(
article_filepath, configuration, serie reload_article = True
) if article_filepath in articles:
articles[article_filepath] = article # 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) serie_articles.append(article)
if serie: if serie:
...@@ -129,6 +152,10 @@ def main(environment): ...@@ -129,6 +152,10 @@ def main(environment):
distutils.dir_util.copy_tree(static_dirpath, output_path) distutils.dir_util.copy_tree(static_dirpath, output_path)
print(f"Static files copied") 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() ellapsed = time.perf_counter()
print(f"Generated in {ellapsed - start:.2f}s") print(f"Generated in {ellapsed - start:.2f}s")
......
...@@ -30,6 +30,7 @@ class Article: ...@@ -30,6 +30,7 @@ class Article:
self.meta[article_key] = values[0] self.meta[article_key] = values[0]
self.meta["ARTICLE_CONTENT"] = content self.meta["ARTICLE_CONTENT"] = content
self.meta["ARTICLE_SERIE"] = serie self.meta["ARTICLE_SERIE"] = serie
self.meta["ARTICLE_LAST_MODIFICATION"] = datetime.datetime.now()
def render(self): def render(self):
return self.template.render(self.meta) return self.template.render(self.meta)
...@@ -88,3 +89,6 @@ class Article: ...@@ -88,3 +89,6 @@ class Article:
def template(self): def template(self):
return self.meta["ARTICLE_TEMPLATE"] 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): ...@@ -24,12 +24,15 @@ def init_output_folder(output_path):
def build_article_from_filepath(filepath, configuration, serie=None): def build_article_from_filepath(filepath, configuration, serie=None):
"""Read a file and initialize an Article from it. """Read a file and initialize an Article from it.
""" """
last_modification = os.path.getmtime(filepath)
filepath_no_ext, _ = os.path.splitext(filepath) filepath_no_ext, _ = os.path.splitext(filepath)
slug = os.path.basename(filepath_no_ext) slug = os.path.basename(filepath_no_ext)
with open(filepath, "r") as article_file: with open(filepath, "r") as article_file:
return Article( article = Article(
slug, article_file.read(), configuration=configuration, serie=serie 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): 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