Use null backend when cache is disabled

Rather then hacking up our cache object while using a file backend, we
can simply ask dogpile.cache to use a null backend. This way we keep
things simple.

Change-Id: Ibf34c4e26652629c3ee589fdcdd65188792aafef
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2015-10-22 18:12:41 -04:00
parent 1bb25348e9
commit af5f511cf5
3 changed files with 51 additions and 27 deletions

View File

@ -25,11 +25,12 @@ LOG = logging.getLogger(__name__)
class Builder(object):
def __init__(self, config):
self.cache = Cache(
config.get('cache', 'cachedir'),
config.getboolean('cache', 'enabled'))
self.grafana = Grafana(
config.get('grafana', 'apikey'), config.get('grafana', 'url'))
self.parser = YamlParser()
self.cache_enabled = config.getboolean('cache', 'enabled')
self.cache = Cache(config.get('cache', 'cachedir'))
def load_files(self, path):
files_to_process = []
@ -50,7 +51,7 @@ class Builder(object):
LOG.info('Number of dashboards generated: %d', len(dashboards))
for name in dashboards:
data, md5 = self.parser.get_dashboard(name)
if self.cache.has_changed(name, md5) or not self.cache_enabled:
if self.cache.has_changed(name, md5):
self.grafana.dashboard.create(name, data, overwrite=True)
self.cache.set(name, md5)
else:

View File

@ -22,16 +22,19 @@ LOG = logging.getLogger(__name__)
class Cache(object):
def __init__(self, cachedir):
cache_dir = self._get_cache_dir(cachedir)
filename = os.path.join(cache_dir, 'cache.dbm')
LOG.debug('Using cache: %s' % filename)
self.region = make_region().configure(
'dogpile.cache.dbm',
arguments={
def __init__(self, cachedir, enabled=True):
if enabled:
backend = 'dogpile.cache.dbm'
cache_dir = self._get_cache_dir(cachedir)
filename = os.path.join(cache_dir, 'cache.dbm')
LOG.debug('Using cache: %s' % filename)
arguments = {
'filename': filename,
}
)
else:
backend = 'dogpile.cache.null'
arguments = {}
self.region = make_region().configure(backend, arguments=arguments)
def get(self, title):
res = self.region.get(title)

View File

@ -22,39 +22,59 @@ class TestCaseCache(TestCase):
'hello-world': '2095312189753de6ad47dfe20cbe97ec',
}
def setUp(self):
super(TestCaseCache, self).setUp()
cachedir = self.config.get('cache', 'cachedir')
self.storage = cache.Cache(cachedir)
def test_cache_has_changed(self):
res = self.storage.has_changed(
storage = cache.Cache(self.config.get('cache', 'cachedir'))
res = storage.has_changed(
'hello-world', self.dashboard['hello-world'])
self.assertTrue(res)
self.storage.set('hello-world', self.dashboard['hello-world'])
res = self.storage.has_changed(
storage.set('hello-world', self.dashboard['hello-world'])
res = storage.has_changed(
'hello-world', self.dashboard['hello-world'])
self.assertFalse(res)
def test_cache_disabled_has_changed(self):
storage = cache.Cache(self.config.get('cache', 'cachedir'), False)
res = storage.has_changed(
'hello-world', self.dashboard['hello-world'])
self.assertTrue(res)
# Set a second time and confirm cache has_changed is True.
storage.set('hello-world', self.dashboard['hello-world'])
res = storage.has_changed(
'hello-world', self.dashboard['hello-world'])
self.assertTrue(res)
def test_cache_get_empty(self):
self.assertEqual(self.storage.get('empty'), None)
storage = cache.Cache(self.config.get('cache', 'cachedir'))
self.assertEqual(storage.get('empty'), None)
def test_cache_disabled_get_empty(self):
storage = cache.Cache(self.config.get('cache', 'cachedir'), False)
self.assertEqual(storage.get('empty'), None)
def test_cache_set_multiple(self):
self.storage.set('hello-world', self.dashboard['hello-world'])
storage = cache.Cache(self.config.get('cache', 'cachedir'))
storage.set('hello-world', self.dashboard['hello-world'])
self.assertEqual(
self.storage.get('hello-world'), self.dashboard['hello-world'])
storage.get('hello-world'), self.dashboard['hello-world'])
dashboard = {
'foobar': '14758f1afd44c09b7992073ccf00b43d'
}
dashboard['hello-world'] = self.dashboard['hello-world']
self.storage.set('foobar', dashboard['foobar'])
self.assertEqual(self.storage.get('foobar'), dashboard['foobar'])
storage.set('foobar', dashboard['foobar'])
self.assertEqual(storage.get('foobar'), dashboard['foobar'])
# Make sure hello-world is still valid.
self.assertEqual(
self.storage.get('hello-world'), self.dashboard['hello-world'])
storage.get('hello-world'), self.dashboard['hello-world'])
def test_cache_set_single(self):
self.storage.set('hello-world', self.dashboard['hello-world'])
storage = cache.Cache(self.config.get('cache', 'cachedir'))
storage.set('hello-world', self.dashboard['hello-world'])
self.assertEqual(
self.storage.get('hello-world'), self.dashboard['hello-world'])
storage.get('hello-world'), self.dashboard['hello-world'])
def test_cache_disabled_set_single(self):
storage = cache.Cache(self.config.get('cache', 'cachedir'), False)
storage.set('hello-world', self.dashboard['hello-world'])
# Make sure cache is empty
self.assertEqual(storage.get('hello-world'), None)