2017-04-06 19:05:51 +03:00
|
|
|
"""`AbstractFactory` providers example."""
|
|
|
|
|
|
|
|
import cache
|
|
|
|
|
|
|
|
import dependency_injector.providers as providers
|
|
|
|
|
|
|
|
|
|
|
|
# Define abstract cache client factory:
|
|
|
|
cache_client_factory = providers.AbstractFactory(cache.AbstractCacheClient)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# Override abstract factory with redis client factory:
|
2020-07-18 07:40:14 +03:00
|
|
|
cache_client_factory.override(
|
|
|
|
providers.Factory(
|
|
|
|
cache.RedisCacheClient,
|
|
|
|
host='localhost',
|
|
|
|
port=6379,
|
|
|
|
db=0,
|
|
|
|
),
|
|
|
|
)
|
2017-04-06 19:05:51 +03:00
|
|
|
redis_cache = cache_client_factory()
|
2020-07-18 07:40:14 +03:00
|
|
|
print(redis_cache)
|
|
|
|
# <cache.RedisCacheClient object at 0x10975bc50>
|
2017-04-06 19:05:51 +03:00
|
|
|
|
|
|
|
# Override abstract factory with memcache client factory:
|
2020-07-18 07:40:14 +03:00
|
|
|
cache_client_factory.override(
|
|
|
|
providers.Factory(
|
|
|
|
cache.MemcacheCacheClient,
|
|
|
|
hosts=['10.0.1.1', '10.0.1.2', '10.0.1.3'],
|
|
|
|
port=11211,
|
|
|
|
prefix='my_app',
|
|
|
|
),
|
|
|
|
)
|
2017-04-06 19:05:51 +03:00
|
|
|
memcache_cache = cache_client_factory()
|
2020-07-18 07:40:14 +03:00
|
|
|
print(memcache_cache)
|
|
|
|
# <cache.MemcacheCacheClient object at 0x10975bc90>
|