From 66f65498177dfa03f2a042b13c0e86dddb20b326 Mon Sep 17 00:00:00 2001 From: erbalito <45042116+erbalito@users.noreply.github.com> Date: Wed, 14 Nov 2018 12:47:04 -0300 Subject: [PATCH] Update redis_cache.py to allow sharing cache between different applications I'm not sure if this would help someone else. I've always had this scenario where some of my web2py applications share the models, and therefore, need to share the cache (I use Redis). In the past I've asked if there was any possibility to share cache between applications, but I got no answers: https://groups.google.com/forum/#!searchin/web2py/share$20cache$20application%7Csort:date/web2py/iNCzMq8ADnw/ufyPBWajBQAJ Anyway, I've noticed that I could make a simple change to redis_cache.py to achieve what I was looking for: I've added the optional argument "application" to RedisCache(). If you inspect the RedisCache() code, you will notice that previously it was forced to use current.request.application for the instance_name. With this simple change I did, it's possible to provide an application name to the RedisCache() constructor. This has worked like a charm for my specific scenario, and I think I can say that it has backwards compatibility. Still, if this proposal isn't accepted, I would like to hear about some alternative to share cache between applications. Thanks! --- gluon/contrib/redis_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gluon/contrib/redis_cache.py b/gluon/contrib/redis_cache.py index ddfd3a67..c44a5b7c 100644 --- a/gluon/contrib/redis_cache.py +++ b/gluon/contrib/redis_cache.py @@ -22,7 +22,7 @@ logger = logging.getLogger("web2py.cache.redis") locker = Lock() -def RedisCache(redis_conn=None, debug=False, with_lock=False, fail_gracefully=False, db=None): +def RedisCache(redis_conn=None, debug=False, with_lock=False, fail_gracefully=False, db=None, application=None): """ Usage example: put in models:: @@ -83,7 +83,7 @@ def RedisCache(redis_conn=None, debug=False, with_lock=False, fail_gracefully=Fa locker.acquire() try: - instance_name = 'redis_instance_' + current.request.application + instance_name = 'redis_instance_' + (application or current.request.application) if not hasattr(RedisCache, instance_name): setattr(RedisCache, instance_name, RedisClient(redis_conn=redis_conn, debug=debug,