Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching to wiki entries and homepage #59

Merged
merged 1 commit into from
Sep 5, 2022
Merged

Conversation

kura
Copy link
Contributor

@kura kura commented Sep 3, 2022

Summary

Add caching to wiki entries and the homepage when they're first viewed.

Details

The way this caching works is really simple; when an entry is first viewed the HTML generated will be stored in a local cache. Each time the entry is viewed again, the HTML will be served from the cache, unless the source file has been modified since the cache was generated.
If the source file is newer than the cached HTML, the cache file will be deleted and the new content generated and cached.

This caching system could also be used to cache other thing, it'd be possible to reconfigure it further if required too, like moving the file modification time check done in cache.get, allowing caching things that do not have a physical file on disk but this would require moving the modification check to where the entry is read and returned to the user for example.

Checks

  • In case of new feature, add short overview in docs/<corresponding file>
  • Tested changes

@kura kura changed the title Add caching to wiki entries Add caching to wiki entries and homepage Sep 3, 2022
@kura kura force-pushed the caching branch 2 times, most recently from 6fc5243 to 61b6e0e Compare September 3, 2022 14:44
@Linbreux
Copy link
Owner

Linbreux commented Sep 3, 2022

Hi @kura

This is exactly how I planned on implementing it myself. Thanks! I'll test it first before merging it. I currently don't have my PC with me so I'll do it asap.

@kura
Copy link
Contributor Author

kura commented Sep 3, 2022

Cool, no worries. It's also currently being tested on my home lab wiki.

@Linbreux
Copy link
Owner

Linbreux commented Sep 4, 2022

Hi @kura
This works great, loading times are reduced dramatically!
But wouldn't it be nicer to have CACHE_DIR = "<path to wiki>/.cache" instead of CACHE_DIR = "/dev/shm/wikmd" by default?
I like to have everything in one place. Or how do you think about it?

@kura
Copy link
Contributor Author

kura commented Sep 4, 2022

Yeah it's a lot faster with a little caching. :D

I chose /dev/shm/ simply because it's a tmpfs that is mounted on pretty much every modern Linux. I don't mind changing it to something else.

When you say <path to wiki>/ what value should that be? Since caching is on by default this needs to be a path that can be hardcoded in YAML and work for basically all installs.

@Linbreux
Copy link
Owner

Linbreux commented Sep 4, 2022

Alright I think we can leave it then. I meant inside the cloned repo. wikmd/.cache for example. This could be an relative path then.

@kura
Copy link
Contributor Author

kura commented Sep 4, 2022

Alright I think we can leave it then. I meant inside the cloned repo. wikmd/.cache for example. This could be an relative path then.

Yeah that would work. I'll update the default.

@kura kura force-pushed the caching branch 3 times, most recently from 05f7f78 to 3238825 Compare September 4, 2022 12:56
@kura
Copy link
Contributor Author

kura commented Sep 4, 2022

I was digging in to why one of the tests was failing on GitHub Actions and found it's because of an interesting issue with using .cache rather than /dev/shm/wikmd. In the GitHub Actions worker .cache already exists and seems to be owned by another user, so a PermissionDenied exception is thrown.

I tried changing the cache path to .cache/wikmd hoping maybe the test would have permission to write there but sadly it does not. .cache is a directory used by a bunch of things, and if the Docker container has the wikmd directory setup as it's home directory and uses sudo pip install then .cache will be created and owned by root.

I'm going to change back to using /dev/shm/wikmd.

 [2022-09-04 14:28:26,839] INFO in git_manager: Initializing existing repo >>> /wiki ...
Traceback (most recent call last):
  File "/app/wikmd/wiki.py", line 50, in <module>
    cache = Cache(cfg.cache_dir)
  File "/app/wikmd/cache.py", line 12, in __init__
    os.makedirs(path)
  File "/usr/lib/python3.6/os.py", line 210, in makedirs
    makedirs(head, mode, exist_ok)
  File "/usr/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '.cache'

@kura kura force-pushed the caching branch 2 times, most recently from 9973072 to fbb4654 Compare September 4, 2022 17:42
@Linbreux Linbreux merged commit 5ecdacd into Linbreux:main Sep 5, 2022
@Linbreux
Copy link
Owner

Linbreux commented Sep 5, 2022

Alright perfect! Didn't know that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants