redis learning - memory configuration information

The configuration information for all programs in the system are stored in redis which, when needed acquired directly from the redis in, whether it is a good idea?

Scenario Design

Due to the company's system development version, temporarily out of service for some time, this time if the user has access to the system, you need to get a 系统正在维护message. Intolerant and when the user the trouble of crazy refresh system UI, while trying to access the system should this case be considered to deal with the case design.

In redis server stores a key indicating whether the system is being maintained, when the user requests the system resources, returns the value of the current state of the system is determined by this function., And a request limit plus 1 second.

Configuration information in a distributed system architecture places because of the power of a shared access

Establish a redis server configuration information as the server?

Design a set configfunction:

def set_config(conn, type, component, config):
    conn.set(
        'config:%s:%s'%(type, component),
        json.dumps(config))

There are get config:

Use python decorator to design a mechanism to automatically connect redis server management

The above code inside either set_config or get_config, parameters have a conn. Conn on which to get a connection means that calling xx_config caller must first corresponding server.

redis automatic connection decorator:

REDIS_CONNECTIONS = {}

def redis_connection(component, wait=1):
    key = 'config:redis:' + component
    def wrapper(function):
        def call(*args, **kwargs):
            old_config = CONFIGS.get(key, object())
            config = get_config(
                config_connection, 'redis', component, wait)

            if config != old_config:
                REDIS_CONNECTIONS[key] = redis.Redis(**config)

            return function(
                REDIS_CONNECTIONS.get(key), *args, **kwargs)
        return call
    return wrapper 

Code analysis:

  • There is a function called redis_connection
  • Parameters component, type should be string, it will be by splicing a key value.
  • It defines an internal nested function wrapper, the parameter is functionskipped.
  • return wrapper , Redis_connection return value is the need to perform the function wrapper, wrapper function obtained return value is returned.
  • enterwrapper
  • It defines an internal nested function call, default parameters in the form of parameters, *args, **kwargsand continue to skip
  • return call, call here call, and return its value returned.
  • entercall
  • Get config old connection string, the connection string config obtain new, old and new contrast, if not equal, then the value is updated REDIS_CONNECTIONS
  • Otherwise execution function(REDIS_CONNECTIONS.get(key), *args, **kwargs), function is a function, the first parameter is REDIS_CONNECTIONS.get (key) that is conn, love parameter is valid and what is behind
  • call the return value is the value after the execution of the function
  • The return value is the value wrapper call returned
  • redis_connection return value is the return value of the wrapper
  • Parsed completely.

For example use:

@redis_connection('logs')                   #A
def log_recent(conn, name, message, severity=logging.INFO, pipe=None):
    severity = str(SEVERITY.get(severity, severity)).lower()    #B
    destination = 'recent:%s:%s'%(name, severity)               #C
    message = time.asctime() + ' ' + message                    #D
    pipe = pipe or conn.pipeline()                              #E
    pipe.lpush(destination, message)                            #F
    pipe.ltrim(destination, 0, 99)                              #G
    pipe.execute()                                              #H

log_recent('main', 'User 235 logged in')    #C

Decorator advantage is that the method call the user need not consider the issue of the connection server.

Guess you like

Origin www.cnblogs.com/it-dennis/p/12575875.html