correctly cache files
diff --git a/reclass/storage/memcache_proxy.py b/reclass/storage/memcache_proxy.py
index a709d40..485024b 100644
--- a/reclass/storage/memcache_proxy.py
+++ b/reclass/storage/memcache_proxy.py
@@ -14,7 +14,7 @@
class MemcacheProxy(NodeStorageBase):
def __init__(self, real_storage, cache_classes=True, cache_nodes=True,
- cache_nodelist=True, cache_exports=True):
+ cache_nodelist=True):
name = '{0}({1})'.format(STORAGE_NAME, real_storage.name)
super(MemcacheProxy, self).__init__(name)
self._real_storage = real_storage
@@ -27,50 +27,28 @@
self._cache_nodelist = cache_nodelist
if cache_nodelist:
self._nodelist_cache = None
- self._cache_exports = cache_exports
- if cache_exports:
- self._exports_cache = None
name = property(lambda self: self._real_storage.name)
- @staticmethod
- def _cache_proxy(name, cache, getter):
- try:
- ret = cache[name]
-
- except KeyError, e:
- ret = getter(name)
- cache[name] = ret
-
- return ret
-
- @staticmethod
- def _cache(cache, getter):
- if cache is None:
- cache = getter()
- return cache
-
def get_node(self, name):
if not self._cache_nodes:
return self._real_storage.get_node(name)
-
- return MemcacheProxy._cache_proxy(name, self._nodes_cache,
- self._real_storage.get_node)
+ try:
+ ret = self._nodes_cache[name]
+ except KeyError, e:
+ ret = self._real_storage.get_node(name)
+ self._nodes_cache[name] = ret
+ return ret
def get_class(self, name, environment):
if not self._cache_classes:
return self._real_storage.get_class(name, environment)
-
- return MemcacheProxy._cache_proxy(name, self._classes_cache,
- self._real_storage.get_class)
-
- def get_exports(self):
- if not self._cache_exports:
- return self._real_storage.get_exports()
- return MemcacheProxy._cache(self._exports_cache, self._real_storage.get_exports)
-
- def put_exports(self, new):
- self._real_storage.put_exports(new)
+ try:
+ ret = self._classes_cache[name]
+ except KeyError, e:
+ ret = self._real_storage.get_class(name, environment)
+ self._classes_cache[name] = ret
+ return ret
def enumerate_nodes(self):
if not self._cache_nodelist:
diff --git a/reclass/storage/mixed/__init__.py b/reclass/storage/mixed/__init__.py
index 238ce74..1fc4fb5 100644
--- a/reclass/storage/mixed/__init__.py
+++ b/reclass/storage/mixed/__init__.py
@@ -50,7 +50,7 @@
def get_node(self, name):
return self._nodes_storage.get_node(name)
- def get_class(self, name, environment=None):
+ def get_class(self, name, environment):
if environment is None:
storage = self._classes_default_storage
else:
diff --git a/reclass/storage/tests/test_memcache_proxy.py b/reclass/storage/tests/test_memcache_proxy.py
index 066c27e..6764251 100644
--- a/reclass/storage/tests/test_memcache_proxy.py
+++ b/reclass/storage/tests/test_memcache_proxy.py
@@ -47,23 +47,23 @@
p = MemcacheProxy(self._storage, cache_classes=False)
NAME = 'foo'; NAME2 = 'bar'; RET = 'baz'
self._storage.get_class.return_value = RET
- self.assertEqual(p.get_class(NAME), RET)
- self.assertEqual(p.get_class(NAME), RET)
- self.assertEqual(p.get_class(NAME2), RET)
- self.assertEqual(p.get_class(NAME2), RET)
- expected = [mock.call(NAME), mock.call(NAME),
- mock.call(NAME2), mock.call(NAME2)]
+ self.assertEqual(p.get_class(NAME, None), RET)
+ self.assertEqual(p.get_class(NAME, None), RET)
+ self.assertEqual(p.get_class(NAME2, None), RET)
+ self.assertEqual(p.get_class(NAME2, None), RET)
+ expected = [mock.call(NAME, None), mock.call(NAME, None),
+ mock.call(NAME2, None), mock.call(NAME2, None)]
self.assertListEqual(self._storage.get_class.call_args_list, expected)
def test_classes_caching(self):
p = MemcacheProxy(self._storage, cache_classes=True)
NAME = 'foo'; NAME2 = 'bar'; RET = 'baz'
self._storage.get_class.return_value = RET
- self.assertEqual(p.get_class(NAME), RET)
- self.assertEqual(p.get_class(NAME), RET)
- self.assertEqual(p.get_class(NAME2), RET)
- self.assertEqual(p.get_class(NAME2), RET)
- expected = [mock.call(NAME), mock.call(NAME2)] # called once each
+ self.assertEqual(p.get_class(NAME, None), RET)
+ self.assertEqual(p.get_class(NAME, None), RET)
+ self.assertEqual(p.get_class(NAME2, None), RET)
+ self.assertEqual(p.get_class(NAME2, None), RET)
+ expected = [mock.call(NAME, None), mock.call(NAME2, None)] # called once each
self.assertListEqual(self._storage.get_class.call_args_list, expected)
def test_nodelist_no_caching(self):
diff --git a/reclass/storage/yaml_git/__init__.py b/reclass/storage/yaml_git/__init__.py
index cd85ed1..b08d4f9 100644
--- a/reclass/storage/yaml_git/__init__.py
+++ b/reclass/storage/yaml_git/__init__.py
@@ -136,7 +136,7 @@
entity = YamlData.from_string(blob.data, 'git_fs://{0}#{1}/{2}'.format(self._nodes_uri.repo, self._nodes_uri.branch, file.path)).get_entity(name)
return entity
- def get_class(self, name, environment=None):
+ def get_class(self, name, environment):
uri = self._env_to_uri(environment)
file = self._repos[uri.repo].files[uri.branch][name]
blob = self._repos[uri.repo].get(file.id)
@@ -151,12 +151,16 @@
self._repos[url] = GitRepo(url)
def _env_to_uri(self, environment):
+ ret = None
if environment is None:
ret = self._classes_default_uri
- for env, uri in self._classes_uri:
- if env == environment:
- ret = uri
- break
+ else:
+ for env, uri in self._classes_uri:
+ if env == environment:
+ ret = uri
+ break
+ if ret is None:
+ ret = self._classes_default_uri
if ret.branch == '__env__':
ret.branch = environment
if ret.branch == None: