source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/contrib/redis_utils.py

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 1.8 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Developed by niphlod@gmail.com
5License MIT/BSD/GPL
6
7Serves as base to implement Redis connection object and various utils
8for redis_cache, redis_session and redis_scheduler in the future
9Should-could be overriden in case redis doesn't keep up (e.g. cluster support)
10to ensure compatibility with another - similar - library
11"""
12
13import logging
14from threading import Lock
15import time
16from gluon import current
17
18logger = logging.getLogger("web2py.redis_utils")
19
20try:
21    import redis
22    from redis.exceptions import WatchError as RWatchError
23    from redis.exceptions import ConnectionError as RConnectionError
24except ImportError:
25    logger.error("Needs redis library to work")
26    raise RuntimeError('Needs redis library to work')
27
28
29locker = Lock()
30
31
32def RConn(application=None, *args, **vars):
33    """
34    Istantiates a StrictRedis connection with parameters, at the first time
35    only
36    """
37    locker.acquire()
38    try:
39        if application is None:
40            application = current.request.application
41        instance_name = 'redis_conn_' + application
42        if not hasattr(RConn, instance_name):
43            setattr(RConn, instance_name, redis.StrictRedis(*args, **vars))
44        return getattr(RConn, instance_name)
45    finally:
46        locker.release()
47
48def acquire_lock(conn, lockname, identifier, ltime=10):
49    while True:
50        if conn.set(lockname, identifier, ex=ltime, nx=True):
51            return identifier
52        time.sleep(.01)
53
54
55_LUA_RELEASE_LOCK = """
56if redis.call("get", KEYS[1]) == ARGV[1]
57then
58    return redis.call("del", KEYS[1])
59else
60    return 0
61end
62"""
63
64
65def release_lock(instance, lockname, identifier):
66    return instance._release_script(
67        keys=[lockname], args=[identifier])
68
69
70def register_release_lock(conn):
71    rtn = conn.register_script(_LUA_RELEASE_LOCK)
72    return rtn
Note: See TracBrowser for help on using the repository browser.