Move memcached_servers opt into common.memorycache

Add a factory function to nova.common.memorycache which consolidates
the code to choose between real or fake memcache. This then means that
memcached_servers is used in just one place and we can move the config
option there.

blueprint: scope-config-opts
Change-Id: I67c191e0db58364eda4162b9e881606063509b9d
This commit is contained in:
Mark McLoughlin 2013-01-08 09:18:11 +00:00
parent d8c80c6b14
commit 562b5a452d
5 changed files with 26 additions and 25 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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."""

View File

@ -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'),

View File

@ -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):