diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py index b7742a53c442..7cd7e1c7d1d0 100644 --- a/nova/api/ec2/__init__.py +++ b/nova/api/ec2/__init__.py @@ -31,6 +31,7 @@ from nova.api.ec2 import apirequest from nova.api.ec2 import ec2utils from nova.api.ec2 import faults from nova.api import validator +from nova.common import memorycache from nova import context from nova import exception from nova.openstack.common import cfg @@ -72,7 +73,6 @@ ec2_opts = [ CONF = cfg.CONF CONF.register_opts(ec2_opts) -CONF.import_opt('memcached_servers', 'nova.config') CONF.import_opt('use_forwarded_for', 'nova.api.auth') @@ -162,12 +162,7 @@ class Lockout(wsgi.Middleware): def __init__(self, application): """middleware can use fake for testing.""" - if CONF.memcached_servers: - import memcache - else: - from nova.common import memorycache as memcache - self.mc = memcache.Client(CONF.memcached_servers, - debug=0) + self.mc = memorycache.get_client() super(Lockout, self).__init__(application) @webob.dec.wsgify(RequestClass=wsgi.Request) diff --git a/nova/api/metadata/handler.py b/nova/api/metadata/handler.py index b164c5fea968..fbb46930b5b5 100644 --- a/nova/api/metadata/handler.py +++ b/nova/api/metadata/handler.py @@ -25,6 +25,7 @@ import webob.dec import webob.exc from nova.api.metadata import base +from nova.common import memorycache from nova import exception from nova.openstack.common import cfg from nova.openstack.common import log as logging @@ -33,7 +34,6 @@ from nova import wsgi CACHE_EXPIRATION = 15 # in seconds CONF = cfg.CONF -CONF.import_opt('memcached_servers', 'nova.config') CONF.import_opt('use_forwarded_for', 'nova.api.auth') metadata_proxy_opts = [ @@ -52,17 +52,12 @@ CONF.register_opts(metadata_proxy_opts) LOG = logging.getLogger(__name__) -if CONF.memcached_servers: - import memcache -else: - from nova.common import memorycache as memcache - class MetadataRequestHandler(wsgi.Application): """Serve metadata.""" def __init__(self): - self._cache = memcache.Client(CONF.memcached_servers, debug=0) + self._cache = memorycache.get_client() def get_metadata_by_remote_address(self, address): if not address: diff --git a/nova/common/memorycache.py b/nova/common/memorycache.py index 502f8338114e..f77b3f51a3ca 100644 --- a/nova/common/memorycache.py +++ b/nova/common/memorycache.py @@ -18,8 +18,28 @@ """Super simple fake memcache client.""" +from nova.openstack.common import cfg from nova.openstack.common import timeutils +memcache_opts = [ + cfg.ListOpt('memcached_servers', + default=None, + help='Memcached servers or None for in process cache.'), +] + +CONF = cfg.CONF +CONF.register_opts(memcache_opts) + + +def get_client(): + client_cls = Client + + if CONF.memcached_servers: + import memcache + client_cls = memcache.Client + + return client_cls(CONF.memcached_servers, debug=0) + class Client(object): """Replicates a tiny subset of memcached client interface.""" diff --git a/nova/config.py b/nova/config.py index 81d63670831a..f3fa8d380b85 100644 --- a/nova/config.py +++ b/nova/config.py @@ -54,9 +54,6 @@ global_opts = [ 'However, the node name must be valid within ' 'an AMQP key, and if using ZeroMQ, a valid ' 'hostname, FQDN, or IP address'), - cfg.ListOpt('memcached_servers', - default=None, - help='Memcached servers or None for in process cache.'), cfg.BoolOpt('use_ipv6', default=False, help='use ipv6'), diff --git a/nova/consoleauth/manager.py b/nova/consoleauth/manager.py index 8d2171de720e..2dfc72435cf1 100644 --- a/nova/consoleauth/manager.py +++ b/nova/consoleauth/manager.py @@ -20,6 +20,7 @@ import time +from nova.common import memorycache from nova import manager from nova.openstack.common import cfg from nova.openstack.common import jsonutils @@ -39,7 +40,6 @@ consoleauth_opts = [ CONF = cfg.CONF CONF.register_opts(consoleauth_opts) -CONF.import_opt('memcached_servers', 'nova.config') class ConsoleAuthManager(manager.Manager): @@ -49,13 +49,7 @@ class ConsoleAuthManager(manager.Manager): def __init__(self, scheduler_driver=None, *args, **kwargs): super(ConsoleAuthManager, self).__init__(*args, **kwargs) - - if CONF.memcached_servers: - import memcache - else: - from nova.common import memorycache as memcache - self.mc = memcache.Client(CONF.memcached_servers, - debug=0) + self.mc = memorycache.get_client() def authorize_console(self, context, token, console_type, host, port, internal_access_path):