<redis actual combat >5.4.3 Redis automatic connection management

Source: Internet
Author: User
Tags redis server



Original: https://redislabs.com/ebook/redis-in-action/part-2-core-concepts-2/ chapter-5-using-redis-for-application-support/5-4-service-discovery-and-configuration/5-4-3- Automatic-redis-connection-management






Creating a Redis connection manually can be difficult, not just to keep reading the configuration, and if we use the configuration management function from the previous section, we still need to read the configuration, connect to Redis, and use this connection to do some sort of processing. To simplify all connections, we wrote an adorner to take care of all redis connections (in addition to the server where the configuration was saved)





Adorner , Python has a syntax that supports passing a function x to another function, Y. This function, Y, is called an adorner, and the adorner provides the opportunity to change the behavior of function x, some adorner validation parameters, some registration callback functions, and some like the administrative connection we intend to do.





Our decorator uses a parameter called a configuration that generates a wrapper that automatically connects to the appropriate Redis server when the actual function is called. And this connection will be passed to the wrapper function along with the other parameters we provide later, the Redis_connection () function is as follows


#https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#L451
REDIS_CONNECTIONS = {}

# Pass the name of the application component to the decorator.
Def redis_connection(component, wait=1): #A
    # Because the function needs to get this configuration key every time it is called, so we simply cache it.
    Key = ‘config:redis:‘ + component #B
    # The wrapper takes a function as a parameter and wraps this function with another function.
    Def wrapper(function): #C
        # Copy some useful metadata from the wrapped function to the configuration processor.
        @functools.wraps(function) #D
        # Create a function responsible for managing connection information.
        Def call(*args, **kwargs): #E
            # If there is an old configuration, get it.
            Old_config = CONFIGS.get(key, object()) #F
            # If a new configuration exists, get it.
            _config = get_config( #G
                Config_connection, ‘redis‘, component, wait) #G

            Config = {}
            # Process the configuration and use it to create a Redis connection.
            For k, v in _config.iteritems(): #L
                Config[k.encode(‘utf-8‘)] = v #L

            # If the old and new configurations are not the same, create a new connection.
            If config != old_config: #H
                REDIS_CONNECTIONS[key] = redis.Redis(**config) #H

            # Pass the Redis connection and other matching parameters to the wrapped function, then call the function and return the execution result.
            Return function( #I
                REDIS_CONNECTIONS.get(key), *args, **kwargs) #I
        # Return the wrapped function.
        Return call #J
    # Returns the wrapper for wrapping the Redis function.
    Return wrapper





I know this set of nested functions is easy to confuse, but this is really not terribly bad, the function redis_connection (), specifies an application component and returns a wrapper function, which next calls the function that we passed the connection (the wrapped function), and then returns the function caller, This caller handles all including the read configuration, connects the server, and then calls our wrapped function, which is very awkward to say, but actually very handy, and you can apply this adorner to our 5.1.1 section of the Log_recent () function by using the following code


@redis_connection(‘logs‘) # redis_connection() The decorator is very easy to use.
Def log_recent(conn, app, message): # The definition of this function is the same as previously shown, without any changes.
     ‘the old log_recent() code’
Log_recent(‘main‘, ‘User 235 logged in‘) # We no longer have to manually pass the log server connection to it when calling the log_recent() function. 


Now you already know that using redis_connection () to Log_recent () doesn't seem too bad, does it? Using this better approach to handling connections and configurations, we only remove a few lines from each function we call, as an exercise, trying to add this adorner to 5.2.3 section of the Access_time () Content Manager so that we do not need to pass a connection, You can reuse this decorator to all the examples in this book.



<redis actual combat >5.4.3 Redis automatic connection management


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.