blob: 443ebf9c733f85667206669ccdbc0b33514f45c7 [file] [log] [blame]
Yi Kong83283012023-12-13 12:57:00 +09001"""
2Objective-C runtime wrapper for use by LLDB Python formatters
3
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-exception
7"""
8import lldb.formatters.metrics
9
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 True
21 return False
22
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 False
26 self.data[key] = value
27 return True
28
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 default