changed settings access
							parent
							
								
									cf8f4c3ef3
								
							
						
					
					
						commit
						3d8eb77807
					
				@ -1,3 +1,43 @@
 | 
			
		||||
import redis
 | 
			
		||||
 | 
			
		||||
from apistar import Command, Component, Settings
 | 
			
		||||
from apistar import Component, Settings
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class RedisBackend:
 | 
			
		||||
    def __init__(self, settings: Settings) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        Configure a new redis backend
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            settings: The application settings dictionary
 | 
			
		||||
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
        redis_config = settings['REDIS']
 | 
			
		||||
        self.host = redis_config.get('HOST')
 | 
			
		||||
        self.port = redis_config.get('PORT')
 | 
			
		||||
        self.password = redis_config.get('PASSWORD')
 | 
			
		||||
 | 
			
		||||
        # TODO add connection pooling
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Client:
 | 
			
		||||
    def __init__(self, backend: RedisBackend):
 | 
			
		||||
        self.backend = backend
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_redis_client(backend: RedisBackend):
 | 
			
		||||
    """
 | 
			
		||||
    Create a new redis session
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    client = redis.Redis(host=backend.host,
 | 
			
		||||
                         port=backend.port,
 | 
			
		||||
                         password=backend.password)
 | 
			
		||||
    return client
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
components = [
 | 
			
		||||
    Component(RedisBackend),
 | 
			
		||||
    Component(Client, init=get_redis_client)
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue