ODM2 / CZ-Manager

CZ Manager (formerly ODM2 Admin) is an application for site level data management of environmental observations using Observation Data Model 2 (ODM2) for documentation and a detailed walkthrough see:
http://odm2.github.io/CZ-Manager
MIT License
9 stars 12 forks source link

Added the use of Redis to cache get features result #156

Closed lsetiawan closed 7 years ago

miguelcleon commented 7 years ago

So, just starting to look into this. Why should we use redis instead of the Django native memcached? https://docs.djangoproject.com/en/1.11/topics/cache/

lsetiawan commented 7 years ago

Checkout

Great articles! I think for long term and scalability Redis wins out.

miguelcleon commented 7 years ago

Seems like redis is the right choice. So adding django-redis to requirements.txt should be part of this then eh?

lsetiawan commented 7 years ago

Ah yes, thanks for catching that!

miguelcleon commented 7 years ago

Ok, so it is setup here http://dev-odm2admin.cuahsi.org/ODM2-Admintest/mapdata.html the map loads very quickly :-) How and when is the cache rebuilt?

lsetiawan commented 7 years ago

CACHE_TTL specifies how often the cache should be refreshed. I defaulted to 15 minutes from the example on this link: https://realpython.com/blog/python/caching-in-django-with-redis/

miguelcleon commented 7 years ago

Ok, yes I see, Is there a way to tigger a cache rebuild on saving a sampling feature? I'm looking at the documentation but I haven't found something like that yet.

lsetiawan commented 7 years ago

Flush all method: https://niwinz.github.io/django-redis/latest/#_testing_with_django_redis

miguelcleon commented 7 years ago

I'd assume that will clear the entire cache, which is ok when there is only one thing in the cache, but won't be good when we add more things. Maybe persist and expire can be used https://niwinz.github.io/django-redis/latest/#_expire_persist ?

lsetiawan commented 7 years ago

Still not 100% sure on how to implement all that, but for now the caching for get_features for the map is a good first step, it makes the map loads faster the 2nd time. I think this is safe to merge.

miguelcleon commented 7 years ago

seems like the issue highlighted in the article below could be a potential issue, I'm going to try this on another instance of ODM2 Admin on the same server. Seems like I might just have to set a different KEY_PREFIX for the cache.

http://chrislaskey.com/blog/342/running-multiple-redis-instances-on-the-same-server/

miguelcleon commented 7 years ago

no, seems fine, probably fine due to different URLs for the get_features page.

http://dev-odm2admin.cuahsi.org/CZIMEA/mapdata.html

miguelcleon commented 7 years ago

@lsetiawan I'll add a release, docker will also need to be updated? or it should work due to requirements.txt being updated?

lsetiawan commented 7 years ago

@miguelcleon Docker will also need to be updated to add Redis-server in the django compose stack. Have you take a look at my other PR's? Maybe those should be merged if they're good before a release to keep the version confusions, I don't want to have many different development happening, starting to get confusing. Thanks