Don't clear counter objects from cache
Since the counters do not keep any state, there's no reason to
recreate them.
@@ -243,30 +243,19 @@ class ZKTransaction:
if self.needs_connection or not self.handle.connected:
self.reestablish_connection()
- def clear_counter_from_cache():
- """ Deletes a counter from the cache due to an exception being raised.
- """
- if path in self.__counter_cache:
- del self.__counter_cache[path]
+ if path not in self.__counter_cache:
+ self.__counter_cache[path] = InspectableCounter(self.handle, path)
- try:
- counter = None
- if path in self.__counter_cache:
counter = self.__counter_cache[path]
- else:
- counter = InspectableCounter(self.handle, path)
- self.__counter_cache[path] = counter
-
+ try:
new_value = counter + value
return new_value - value, new_value
except kazoo.exceptions.ZookeeperError as zoo_exception:
self.logger.exception(zoo_exception)
- clear_counter_from_cache()
raise ZKTransactionException("Couldn't increment path {0} by value {1}" \
.format(path, value))
except kazoo.exceptions.KazooException as kazoo_exception:
self.logger.exception(kazoo_exception)
- clear_counter_from_cache()
raise ZKTransactionException(
"Couldn't increment path {0} with value {1}" \
.format(path, value))
