Added NO_VALUE to core file
Using oslo_cache lib the only way to compare some cached_value with NO_VALUE is to add bad import like 'from oslo_cache.backends.dictionary import NO_VALUE'. NO_VALUE is now added to core.py file and the improved import 'oslo_cache.core import NO_VALUE' will look much better in other projects. NO_VALUE is now public, it is added to __all__ and is used everywhere from core instead of dogpile.cache.api.NO_VALUE. Co-Authored-By: Sergey Nikitin <snikitin@mirantis.com> Change-Id: I8fea4a9eb088089289ba1dd35377fb10465c313a
This commit is contained in:
parent
a267390505
commit
5881ad7cb1
@ -15,15 +15,14 @@
|
||||
"""dogpile.cache backend that uses dictionary for storage"""
|
||||
|
||||
from dogpile.cache import api
|
||||
from oslo_cache import core
|
||||
from oslo_utils import timeutils
|
||||
|
||||
__all__ = [
|
||||
'DictCacheBackend'
|
||||
]
|
||||
|
||||
|
||||
# Value for nonexistent and expired keys
|
||||
NO_VALUE = api.NO_VALUE
|
||||
_NO_VALUE = core.NO_VALUE
|
||||
|
||||
|
||||
class DictCacheBackend(api.CacheBackend):
|
||||
@ -43,15 +42,16 @@ class DictCacheBackend(api.CacheBackend):
|
||||
self.cache = {}
|
||||
|
||||
def get(self, key):
|
||||
"""Retrieves the value for a key or NO_VALUE for nonexistent and
|
||||
expired keys.
|
||||
"""Retrieves the value for a key.
|
||||
|
||||
:param key: dictionary key
|
||||
:returns: value for a key or :data:`oslo_cache.core.NO_VALUE`
|
||||
for nonexistent or expired keys.
|
||||
"""
|
||||
(value, timeout) = self.cache.get(key, (NO_VALUE, 0))
|
||||
(value, timeout) = self.cache.get(key, (_NO_VALUE, 0))
|
||||
if self.expiration_time > 0 and timeutils.utcnow_ts() >= timeout:
|
||||
self.cache.pop(key, None)
|
||||
return NO_VALUE
|
||||
return _NO_VALUE
|
||||
|
||||
return value
|
||||
|
||||
|
@ -17,6 +17,7 @@ import datetime
|
||||
|
||||
from dogpile.cache import api
|
||||
from dogpile.cache import util as dp_util
|
||||
from oslo_cache import core
|
||||
from oslo_log import log
|
||||
from oslo_utils import importutils
|
||||
from oslo_utils import timeutils
|
||||
@ -30,7 +31,7 @@ __all__ = [
|
||||
'MongoCacheBackend'
|
||||
]
|
||||
|
||||
NO_VALUE = api.NO_VALUE
|
||||
_NO_VALUE = core.NO_VALUE
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
@ -173,16 +174,28 @@ class MongoCacheBackend(api.CacheBackend):
|
||||
return self.api
|
||||
|
||||
def get(self, key):
|
||||
"""Retrieves the value for a key.
|
||||
|
||||
:param key: key to be retrieved.
|
||||
:returns: value for a key or :data:`oslo_cache.core.NO_VALUE`
|
||||
for nonexistent or expired keys.
|
||||
"""
|
||||
value = self.client.get(key)
|
||||
if value is None:
|
||||
return NO_VALUE
|
||||
return _NO_VALUE
|
||||
else:
|
||||
return value
|
||||
|
||||
def get_multi(self, keys):
|
||||
"""Return multiple values from the cache, based on the given keys.
|
||||
|
||||
:param keys: sequence of keys to be retrieved.
|
||||
:returns: returns values (or :data:`oslo_cache.core.NO_VALUE`)
|
||||
as a list matching the keys given.
|
||||
"""
|
||||
values = self.client.get_multi(keys)
|
||||
return [
|
||||
NO_VALUE if key not in values
|
||||
_NO_VALUE if key not in values
|
||||
else values[key] for key in keys
|
||||
]
|
||||
|
||||
|
@ -13,13 +13,14 @@
|
||||
# under the License.
|
||||
|
||||
from dogpile.cache import api
|
||||
from oslo_cache import core
|
||||
|
||||
|
||||
__all__ = [
|
||||
'NoopCacheBackend'
|
||||
]
|
||||
|
||||
NO_VALUE = api.NO_VALUE
|
||||
_NO_VALUE = core.NO_VALUE
|
||||
|
||||
|
||||
class NoopCacheBackend(api.CacheBackend):
|
||||
@ -35,10 +36,10 @@ class NoopCacheBackend(api.CacheBackend):
|
||||
return
|
||||
|
||||
def get(self, key):
|
||||
return NO_VALUE
|
||||
return _NO_VALUE
|
||||
|
||||
def get_multi(self, keys):
|
||||
return [NO_VALUE for x in keys]
|
||||
return [_NO_VALUE for x in keys]
|
||||
|
||||
def set(self, key, value):
|
||||
return
|
||||
|
@ -28,9 +28,15 @@ This library's configuration options must be registered in your application's
|
||||
:class:`oslo_config.cfg.ConfigOpts` instance. Do this by passing the ConfigOpts
|
||||
instance to :func:`configure`.
|
||||
|
||||
The library has special public value for nonexistent or expired keys called
|
||||
:data:`NO_VALUE`. To use this value you should import it from oslo_cache.core::
|
||||
|
||||
from oslo_cache import core
|
||||
NO_VALUE = core.NO_VALUE
|
||||
"""
|
||||
|
||||
import dogpile.cache
|
||||
from dogpile.cache import api
|
||||
from dogpile.cache import proxy
|
||||
from dogpile.cache import util
|
||||
from oslo_log import log
|
||||
@ -46,8 +52,12 @@ __all__ = [
|
||||
'configure_cache_region',
|
||||
'create_region',
|
||||
'get_memoization_decorator',
|
||||
'NO_VALUE',
|
||||
]
|
||||
|
||||
NO_VALUE = api.NO_VALUE
|
||||
"""Value returned for nonexistent or expired keys."""
|
||||
|
||||
_LOG = log.getLogger(__name__)
|
||||
|
||||
_BACKENDS = [
|
||||
|
@ -17,7 +17,6 @@ import copy
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from dogpile.cache import api
|
||||
from dogpile.cache import proxy
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
@ -28,7 +27,7 @@ from oslo_cache import exception
|
||||
from oslo_config import fixture as config_fixture
|
||||
|
||||
|
||||
NO_VALUE = api.NO_VALUE
|
||||
NO_VALUE = cache.NO_VALUE
|
||||
TEST_GROUP = uuid.uuid4().hex
|
||||
TEST_GROUP2 = uuid.uuid4().hex
|
||||
|
||||
|
@ -17,12 +17,12 @@ import copy
|
||||
import functools
|
||||
import uuid
|
||||
|
||||
from dogpile.cache import api
|
||||
from dogpile.cache import region as dp_region
|
||||
import six
|
||||
from six.moves import range
|
||||
|
||||
from oslo_cache.backends import mongo
|
||||
from oslo_cache import core
|
||||
from oslo_cache import exception
|
||||
from oslo_cache.tests import test_cache
|
||||
|
||||
@ -61,6 +61,7 @@ ks_cache = {
|
||||
|
||||
COLLECTIONS = {}
|
||||
SON_MANIPULATOR = None
|
||||
NO_VALUE = core.NO_VALUE
|
||||
|
||||
|
||||
class MockCursor(object):
|
||||
@ -463,7 +464,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
|
||||
random_key = uuid.uuid4().hex
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
|
||||
def test_backend_set_data(self):
|
||||
|
||||
@ -551,7 +552,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
random_key3: 'dummyValue3'}
|
||||
region.set_multi(mapping)
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
self.assertFalse(region.get(random_key))
|
||||
self.assertEqual("dummyValue1", region.get(random_key1))
|
||||
self.assertEqual("dummyValue2", region.get(random_key2))
|
||||
@ -575,7 +576,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
keys = [random_key, random_key1, random_key2, random_key3]
|
||||
results = region.get_multi(keys)
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, results[0])
|
||||
self.assertEqual(NO_VALUE, results[0])
|
||||
self.assertEqual("dummyValue1", results[1])
|
||||
self.assertEqual("", results[2])
|
||||
self.assertEqual("dummyValue3", results[3])
|
||||
@ -595,7 +596,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
random_key3: 'dummyValue3'}
|
||||
region.set_multi(mapping)
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
self.assertEqual("dummyValue1", region.get(random_key1))
|
||||
self.assertEqual("dummyValue2", region.get(random_key2))
|
||||
self.assertEqual("dummyValue3", region.get(random_key3))
|
||||
@ -603,7 +604,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
mapping = {random_key1: 'dummyValue4',
|
||||
random_key2: 'dummyValue5'}
|
||||
region.set_multi(mapping)
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
self.assertEqual("dummyValue4", region.get(random_key1))
|
||||
self.assertEqual("dummyValue5", region.get(random_key2))
|
||||
self.assertEqual("dummyValue3", region.get(random_key3))
|
||||
@ -625,7 +626,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
random_key4: 'dummyValue4'}
|
||||
region.set_multi(mapping)
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
self.assertEqual("dummyValue1", region.get(random_key1))
|
||||
self.assertIsNone(region.get(random_key2))
|
||||
self.assertEqual("", region.get(random_key3))
|
||||
@ -635,7 +636,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
results = region.get_multi(keys)
|
||||
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, results[0])
|
||||
self.assertEqual(NO_VALUE, results[0])
|
||||
self.assertEqual("dummyValue1", results[1])
|
||||
self.assertIsNone(results[2])
|
||||
self.assertEqual("", results[3])
|
||||
@ -644,7 +645,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
mapping = {random_key1: 'dummyValue5',
|
||||
random_key2: 'dummyValue6'}
|
||||
region.set_multi(mapping)
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
self.assertEqual("dummyValue5", region.get(random_key1))
|
||||
self.assertEqual("dummyValue6", region.get(random_key2))
|
||||
self.assertEqual("", region.get(random_key3))
|
||||
@ -662,7 +663,7 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
|
||||
region.delete(random_key)
|
||||
# should return NO_VALUE as key no longer exists in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
|
||||
def test_backend_multi_delete_data(self):
|
||||
|
||||
@ -679,21 +680,21 @@ class MongoCache(test_cache.BaseTestCase):
|
||||
random_key3: 'dummyValue3'}
|
||||
region.set_multi(mapping)
|
||||
# should return NO_VALUE as key does not exist in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key))
|
||||
self.assertEqual("dummyValue1", region.get(random_key1))
|
||||
self.assertEqual("dummyValue2", region.get(random_key2))
|
||||
self.assertEqual("dummyValue3", region.get(random_key3))
|
||||
self.assertEqual(api.NO_VALUE, region.get("InvalidKey"))
|
||||
self.assertEqual(NO_VALUE, region.get("InvalidKey"))
|
||||
|
||||
keys = mapping.keys()
|
||||
|
||||
region.delete_multi(keys)
|
||||
|
||||
self.assertEqual(api.NO_VALUE, region.get("InvalidKey"))
|
||||
self.assertEqual(NO_VALUE, region.get("InvalidKey"))
|
||||
# should return NO_VALUE as keys no longer exist in cache
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key1))
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key2))
|
||||
self.assertEqual(api.NO_VALUE, region.get(random_key3))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key1))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key2))
|
||||
self.assertEqual(NO_VALUE, region.get(random_key3))
|
||||
|
||||
def test_additional_crud_method_arguments_support(self):
|
||||
"""Additional arguments should works across find/insert/update."""
|
||||
|
@ -12,15 +12,15 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from dogpile.cache import api
|
||||
from dogpile.cache import region as dp_region
|
||||
|
||||
from oslo_cache import core
|
||||
from oslo_cache.tests import test_cache
|
||||
from oslo_config import fixture as config_fixture
|
||||
from oslo_utils import fixture as time_fixture
|
||||
|
||||
|
||||
NO_VALUE = api.NO_VALUE
|
||||
NO_VALUE = core.NO_VALUE
|
||||
KEY = 'test_key'
|
||||
VALUE = 'test_value'
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user