Skip to content

Commit

Permalink
Merge pull request #149 from vindevoy/develop
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
vindevoy authored Apr 23, 2020
2 parents 65e73a4 + d002faa commit f0869a6
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 59 deletions.
18 changes: 18 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@

# VERSION 1.3.0

## Release info

- author: Yves Vindevogel (vindevoy)
- date: 2020-04-23

## Enhancements

- Page caching added, next to the data caching (which remains in place for common data)
- Caching is in a class now

### Github

For more information on this release, see the issues for this milestone:

- [https://github.com/vindevoy/cherryblog/milestone/8](https://github.com/vindevoy/cherryblog/milestone/8)

# VERSION 1.2.0

## Release info
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ readme:

history:
@echo '' > ./HISTORY.md
@echo '# VERSION 1.3.0' >> ./HISTORY.md
@echo '' >> ./HISTORY.md
@sed -n '/##/,$$p' ./src/data/posts/0008_version_1_3_0.md >> ./HISTORY.md
@echo '' >> ./HISTORY.md
@echo '# VERSION 1.2.0' >> ./HISTORY.md
@echo '' >> ./HISTORY.md
@sed -n '/##/,$$p' ./src/data/posts/0007_version_1_2_0.md >> ./HISTORY.md
Expand Down
71 changes: 51 additions & 20 deletions src/application/controller/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#
# Full history: see below
#
# Version: 2.1.0
# Date: 2020-04-15
# Version: 2.2.0
# Date: 2020-04-22
# Author: Yves Vindevogel (vindevoy)
#
# Added logging of the requests for performance insights
# Added page caching
#
###

Expand All @@ -18,6 +18,7 @@
from common.options import Options
from common.singleton import Singleton
from controller.data_loader import DataLoader
from controller.page_cacher import PageCacher
from view.templateloader import TemplateLoader


Expand All @@ -33,11 +34,17 @@ def index(self, page_index=1, **_):
request = '/index/{0}'.format(page_index)
start = datetime.now()

data = DataLoader().index_data(page_index)
data['url'] = request
if PageCacher().cached_already(request):
rendered = PageCacher().get_cached(request)

else:
data = DataLoader().index_data(page_index)
data['url'] = request

template = TemplateLoader().get_template('screen_index.html')
rendered = template.render(data=data)

template = TemplateLoader().get_template('screen_index.html')
rendered = template.render(data=data)
PageCacher().cache(request, rendered)

finished = datetime.now()
self.__logger.info('{0} {1}'.format(request, finished - start))
Expand All @@ -49,12 +56,18 @@ def pages(self, page, **_):
request = '/pages/{0}'.format(page)
start = datetime.now()

# page on the URL: http://www.yoursite.ext/pages/page
data = DataLoader().pages_data(page)
data['url'] = request
if PageCacher().cached_already(request):
rendered = PageCacher().get_cached(request)

template = TemplateLoader().get_template('screen_page.html')
rendered = template.render(data=data)
else:
# page on the URL: http://www.yoursite.ext/pages/page
data = DataLoader().pages_data(page)
data['url'] = request

template = TemplateLoader().get_template('screen_page.html')
rendered = template.render(data=data)

PageCacher().cache(request, rendered)

finished = datetime.now()
self.__logger.info('{0} {1}'.format(request, finished - start))
Expand All @@ -66,11 +79,17 @@ def posts(self, post, **_):
request = '/posts/{0}'.format(post)
start = datetime.now()

data = DataLoader().posts_data(post)
data['url'] = request
if PageCacher().cached_already(request):
rendered = PageCacher().get_cached(request)

else:
data = DataLoader().posts_data(post)
data['url'] = request

template = TemplateLoader().get_template('screen_post.html')
rendered = template.render(data=data)

template = TemplateLoader().get_template('screen_post.html')
rendered = template.render(data=data)
PageCacher().cache(request, rendered)

finished = datetime.now()
self.__logger.info('{0} {1}'.format(request, finished - start))
Expand All @@ -82,11 +101,17 @@ def tags(self, tag, page_index=1, **_):
request = '/tags/{0}/{1}'.format(tag, page_index)
start = datetime.now()

data = DataLoader().tags_data(tag, page_index)
data['url'] = request
if PageCacher().cached_already(request):
rendered = PageCacher().get_cached(request)

template = TemplateLoader().get_template('screen_tag.html')
rendered = template.render(data=data)
else:
data = DataLoader().tags_data(tag, page_index)
data['url'] = request

template = TemplateLoader().get_template('screen_tag.html')
rendered = template.render(data=data)

PageCacher().cache(request, rendered)

finished = datetime.now()
self.__logger.info('{0} {1}'.format(request, finished - start))
Expand Down Expand Up @@ -115,6 +140,12 @@ def tags(self, tag, page_index=1, **_):

###
#
# Version: 2.1.0
# Date: 2020-04-15
# Author: Yves Vindevogel (vindevoy)
#
# Added logging of the requests for performance insights
#
# Version: 2.0.0
# Date: 2020-04-13
# Author: Yves Vindevogel (vindevoy)
Expand Down
25 changes: 25 additions & 0 deletions src/application/controller/data_cacher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import logging

from common.options import Options
from common.singleton import Singleton


class DataCacher(metaclass=Singleton):
__logger = None

__cached_data = {}

def __init__(self):
self.__logger = logging.getLogger('DATA_CACHER')
self.__logger.setLevel(Options().default_logging_level)

def cached_already(self, key):
return key in self.__cached_data.keys()

def get_cached(self, key):
return self.__cached_data[key]

def cache(self, key, data):
self.__logger.info('Caching {0}'.format(key))
self.__cached_data[key] = data
64 changes: 28 additions & 36 deletions src/application/controller/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from common.options import Options
from common.singleton import Singleton
from controller.data_cacher import DataCacher
from model.codeversion import CodeVersion
from model.i8n import I8N
from model.important_news import ImportantNews
Expand All @@ -34,22 +35,13 @@ def __init__(self):
self.__logger = logging.getLogger('DATA_LOADER')
self.__logger.setLevel(Options().default_logging_level)

def __cached_already(self, key):
return key in self.__cached_data.keys()

def __get_cached(self, key):
return self.__cached_data[key]

def __cache(self, key, data):
self.__logger.info('Caching {0}'.format(key))
self.__cached_data[key] = data

def __get_data(self, key, cls, method):
if self.__cached_already(key):
return self.__get_cached(key)
@staticmethod
def __get_data(key, cls, method):
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

data = getattr(cls, method)
self.__cache(key, data)
DataCacher().cache(key, data)

return data

Expand All @@ -72,8 +64,8 @@ def code_version_data(self):
def common_data(self):
key = 'common_data'

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

data = {
'i8n': self.i8n,
Expand All @@ -86,7 +78,7 @@ def common_data(self):
'code_version': self.code_version_data
}

self.__cache(key, data)
DataCacher().cache(key, data)
return data

# important news
Expand All @@ -108,19 +100,19 @@ def index_main_menu(self):
def index_footer_menu(self):
return self.__get_data('index_footer_menu', Index(), 'footer_menu')

def index_data(self, page_index, language=None):
def index_data(self, page_index):
key = '/index/{0}'.format(page_index)

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

common = self.common_data
data, _ = Index().data(page_index, self.posts_directory, self.posts_count)

# We don't care yet about the raw intro
combined = self.__combine(common, data)

self.__cache(key, combined)
DataCacher().cache(key, combined)

return combined

Expand Down Expand Up @@ -148,14 +140,14 @@ def pages_count(self):
def pages_data(self, page):
key = '/pages/{0}'.format(page)

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

common = self.common_data
data, _, _ = Pages().data(page) # No catching the meta and raw data yet
combined = self.__combine(common, data)

self.__cache(key, combined)
DataCacher().cache(key, combined)

return combined

Expand All @@ -171,14 +163,14 @@ def posts_count(self):
def posts_data(self, post):
key = '/posts/{0}'.format(post)

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

common = self.common_data
data, _, _ = Posts().data(post) # We don't do anything with the meta and raw data yet
combined = self.__combine(common, data)

self.__cache(key, combined)
DataCacher().cache(key, combined)

return combined

Expand All @@ -191,25 +183,25 @@ def global_settings(self):
def tags_posts_count(self, tag):
key = 'tags_posts_count/{0}'.format(tag)

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

data = Tags().count_posts(self.posts_directory, tag)

self.__cache(key, data)
DataCacher().cache(key, data)

return data

@property
def tags_list(self):
key = 'tags_list'

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

tags_list = Tags().list(self.posts_directory)

self.__cache(key, tags_list)
DataCacher().cache(key, tags_list)

return tags_list

Expand All @@ -220,14 +212,14 @@ def tags_list_count(self):
def tags_data(self, tag, page_index):
key = 'tags_data/{0}/{1}'.format(tag, page_index)

if self.__cached_already(key):
return self.__get_cached(key)
if DataCacher().cached_already(key):
return DataCacher().get_cached(key)

common = self.common_data
data = Tags().data(self.posts_directory, tag, page_index, self.index_max_posts, self.tags_posts_count(tag))
combined = self.__combine(common, data)

self.__cache(key, combined)
DataCacher().cache(key, combined)

return combined

Expand Down
25 changes: 25 additions & 0 deletions src/application/controller/page_cacher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import logging

from common.options import Options
from common.singleton import Singleton


class PageCacher(metaclass=Singleton):
__logger = None

__cached_pages = {}

def __init__(self):
self.__logger = logging.getLogger('PAGE_CACHER')
self.__logger.setLevel(Options().default_logging_level)

def cached_already(self, key):
return key in self.__cached_pages.keys()

def get_cached(self, key):
return self.__cached_pages[key]

def cache(self, key, data):
self.__logger.info('Caching {0}'.format(key))
self.__cached_pages[key] = data
4 changes: 2 additions & 2 deletions src/data/codeversion/settings.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---

text:
- 'Production: v.1.2.0 (master branch)'
- 'Next version: v.1.3.0 (starting soon)'
- 'Production: v.1.3.0 (master branch)'
- 'Next version: v.1.4.0 (starting soon)'
Loading

0 comments on commit f0869a6

Please sign in to comment.