Skip to content

Commit d290005

Browse files
committed
feat: improve cache performance
1 parent b65e279 commit d290005

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/zeroconf/_cache.pxd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from ._dns cimport (
1111

1212

1313
cdef object _TYPE_PTR
14+
cdef object _UNIQUE_RECORD_TYPES
1415

1516
cdef _remove_key(cython.dict cache, object key, DNSRecord record)
1617

@@ -24,5 +25,20 @@ cdef class DNSCache:
2425

2526
cdef _async_remove(self, DNSRecord record)
2627

28+
@cython.locals(
29+
store=cython.dict
30+
)
31+
cpdef async_get_unique(self, DNSRecord record)
32+
33+
cpdef async_all_by_details(self, str name, object type_, object class_)
34+
35+
cpdef async_entries_with_name(self, str name)
36+
37+
cpdef async_entries_with_server(self, str name)
38+
39+
cpdef get_by_details(self, str name, object type_, object class_)
40+
41+
cpdef get_all_by_details(self, str name, object type_, object class_)
42+
2743

2844
cdef _dns_record_matches(DNSRecord record, object key, object type_, object class_)

src/zeroconf/_cache.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,32 +134,34 @@ def async_get_unique(self, entry: _UniqueRecordsType) -> Optional[DNSRecord]:
134134
return None
135135
return store.get(entry)
136136

137-
def async_all_by_details(self, name: str, type_: int, class_: int) -> Iterator[DNSRecord]:
137+
def async_all_by_details(self, name: _str, type_: int, class_: int) -> Iterator[DNSRecord]:
138138
"""Gets all matching entries by details.
139139
140140
This function is not threadsafe and must be called from
141141
the event loop.
142142
"""
143143
key = name.lower()
144-
for entry in self.cache.get(key, []):
144+
for entry in self.cache.get(key) or []:
145145
if _dns_record_matches(entry, key, type_, class_):
146146
yield entry
147147

148-
def async_entries_with_name(self, name: str) -> Dict[DNSRecord, DNSRecord]:
148+
def async_entries_with_name(self, name: _str) -> Dict[DNSRecord, DNSRecord]:
149149
"""Returns a dict of entries whose key matches the name.
150150
151151
This function is not threadsafe and must be called from
152152
the event loop.
153153
"""
154-
return self.cache.get(name.lower(), {})
154+
key = name.lower()
155+
return self.cache.get(key) or {}
155156

156-
def async_entries_with_server(self, name: str) -> Dict[DNSRecord, DNSRecord]:
157+
def async_entries_with_server(self, name: _str) -> Dict[DNSRecord, DNSRecord]:
157158
"""Returns a dict of entries whose key matches the server.
158159
159160
This function is not threadsafe and must be called from
160161
the event loop.
161162
"""
162-
return self.service_cache.get(name.lower(), {})
163+
key = name.lower()
164+
return self.service_cache.get(key) or {}
163165

164166
# The below functions are threadsafe and do not need to be run in the
165167
# event loop, however they all make copies so they significantly
@@ -175,7 +177,7 @@ def get(self, entry: DNSEntry) -> Optional[DNSRecord]:
175177
return cached_entry
176178
return None
177179

178-
def get_by_details(self, name: str, type_: int, class_: int) -> Optional[DNSRecord]:
180+
def get_by_details(self, name: _str, type_: int, class_: int) -> Optional[DNSRecord]:
179181
"""Gets the first matching entry by details. Returns None if no entries match.
180182
181183
Calling this function is not recommended as it will only
@@ -193,7 +195,7 @@ def get_by_details(self, name: str, type_: int, class_: int) -> Optional[DNSReco
193195
return cached_entry
194196
return None
195197

196-
def get_all_by_details(self, name: str, type_: int, class_: int) -> List[DNSRecord]:
198+
def get_all_by_details(self, name: _str, type_: int, class_: int) -> List[DNSRecord]:
197199
"""Gets all matching entries by details."""
198200
key = name.lower()
199201
return [

0 commit comments

Comments
 (0)