|
| 1 | +# Copyright 2010 United States Government as represented by the |
| 2 | +# Administrator of the National Aeronautics and Space Administration. |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +"""Simple wrapper for oslo_cache.""" |
| 18 | + |
| 19 | +from oslo_cache import core as cache |
| 20 | +from oslo_log import log as logging |
| 21 | + |
| 22 | +from oslo_config import cfg |
| 23 | +from bigbang.common.i18n import _, _LW |
| 24 | + |
| 25 | +CONF = cfg.CONF |
| 26 | + |
| 27 | +LOG = logging.getLogger(__name__) |
| 28 | + |
| 29 | +WEEK = 604800 |
| 30 | + |
| 31 | + |
| 32 | +def _warn_if_null_backend(): |
| 33 | + if CONF.cache.backend == 'dogpile.cache.null': |
| 34 | + LOG.warning(_LW("Cache enabled with backend dogpile.cache.null.")) |
| 35 | + |
| 36 | + |
| 37 | +def get_memcached_client(expiration_time=0): |
| 38 | + """Used ONLY when memcached is explicitly needed.""" |
| 39 | + # If the operator has [cache]/enabled flag on then we let oslo_cache |
| 40 | + # configure the region from the configuration settings |
| 41 | + if CONF.cache.enabled and CONF.cache.memcache_servers: |
| 42 | + _warn_if_null_backend() |
| 43 | + return CacheClient( |
| 44 | + _get_default_cache_region(expiration_time=expiration_time)) |
| 45 | + |
| 46 | + |
| 47 | +def get_client(expiration_time=0): |
| 48 | + """Used to get a caching client.""" |
| 49 | + # If the operator has [cache]/enabled flag on then we let oslo_cache |
| 50 | + # configure the region from configuration settings. |
| 51 | + if CONF.cache.enabled: |
| 52 | + _warn_if_null_backend() |
| 53 | + return CacheClient( |
| 54 | + _get_default_cache_region(expiration_time=expiration_time)) |
| 55 | + # If [cache]/enabled flag is off, we use the dictionary backend |
| 56 | + return CacheClient( |
| 57 | + _get_custom_cache_region(expiration_time=expiration_time, |
| 58 | + backend='oslo_cache.dict')) |
| 59 | + |
| 60 | + |
| 61 | +def _get_default_cache_region(expiration_time): |
| 62 | + region = cache.create_region() |
| 63 | + if expiration_time != 0: |
| 64 | + CONF.cache.expiration_time = expiration_time |
| 65 | + cache.configure_cache_region(CONF, region) |
| 66 | + return region |
| 67 | + |
| 68 | + |
| 69 | +def _get_custom_cache_region(expiration_time=WEEK, |
| 70 | + backend=None, |
| 71 | + url=None): |
| 72 | + """Create instance of oslo_cache client. |
| 73 | +
|
| 74 | + For backends you can pass specific parameters by kwargs. |
| 75 | + For 'dogpile.cache.memcached' backend 'url' parameter must be specified. |
| 76 | +
|
| 77 | + :param backend: backend name |
| 78 | + :param expiration_time: interval in seconds to indicate maximum |
| 79 | + time-to-live value for each key |
| 80 | + :param url: memcached url(s) |
| 81 | + """ |
| 82 | + |
| 83 | + region = cache.create_region() |
| 84 | + region_params = {} |
| 85 | + if expiration_time != 0: |
| 86 | + region_params['expiration_time'] = expiration_time |
| 87 | + |
| 88 | + if backend == 'oslo_cache.dict': |
| 89 | + region_params['arguments'] = {'expiration_time': expiration_time} |
| 90 | + elif backend == 'dogpile.cache.memcached': |
| 91 | + region_params['arguments'] = {'url': url} |
| 92 | + else: |
| 93 | + raise RuntimeError(_('old style configuration can use ' |
| 94 | + 'only dictionary or memcached backends')) |
| 95 | + |
| 96 | + region.configure(backend, **region_params) |
| 97 | + return region |
| 98 | + |
| 99 | + |
| 100 | +class CacheClient(object): |
| 101 | + """Replicates a tiny subset of memcached client interface.""" |
| 102 | + |
| 103 | + def __init__(self, region): |
| 104 | + self.region = region |
| 105 | + |
| 106 | + def get(self, key): |
| 107 | + value = self.region.get(key) |
| 108 | + if value == cache.NO_VALUE: |
| 109 | + return None |
| 110 | + return value |
| 111 | + |
| 112 | + def get_or_create(self, key, creator): |
| 113 | + return self.region.get_or_create(key, creator) |
| 114 | + |
| 115 | + def set(self, key, value): |
| 116 | + return self.region.set(key, value) |
| 117 | + |
| 118 | + def add(self, key, value): |
| 119 | + return self.region.get_or_create(key, lambda: value) |
| 120 | + |
| 121 | + def delete(self, key): |
| 122 | + return self.region.delete(key) |
| 123 | + |
| 124 | + def get_multi(self, keys): |
| 125 | + values = self.region.get_multi(keys) |
| 126 | + return [None if value is cache.NO_VALUE else value for value in |
| 127 | + values] |
| 128 | + |
| 129 | + def delete_multi(self, keys): |
| 130 | + return self.region.delete_multi(keys) |
0 commit comments