36 lines · python
1"""2Objective-C runtime wrapper for use by LLDB Python formatters3 4Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5See https://llvm.org/LICENSE.txt for license information.6SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7"""8import lldb.formatters.metrics9 10 11class Cache:12 def __init__(self):13 self.data = {}14 self.statistics = lldb.formatters.metrics.Metrics()15 self.statistics.add_metric("hit")16 self.statistics.add_metric("miss")17 18 def look_for_key(self, key):19 if key in self.data:20 return True21 return False22 23 def add_item(self, key, value, ok_to_replace=True):24 if not (ok_to_replace) and self.look_for_key(key):25 return False26 self.data[key] = value27 return True28 29 def get_value(self, key, default=None):30 if self.look_for_key(key):31 self.statistics.metric_hit("hit", key)32 return self.data[key]33 else:34 self.statistics.metric_hit("miss", key)35 return default36