Browse Source

fix(cache): Implement per-record caching to fix partial failure issue (#500)

* Initial plan

* Implement per-record caching to fix partial failure issue

Co-authored-by: NewFuture <[email protected]>

* Address review feedback: remove staging, fix Python 2 compatibility, remove cache cleanup and tests

Co-authored-by: NewFuture <[email protected]>

* Address review feedback: add address to cache log and eliminate cache_key duplication

Co-authored-by: NewFuture <[email protected]>

* fix(lint): Remove whitespace from blank line 103 to fix W293 lint error

Co-authored-by: NewFuture <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: NewFuture <[email protected]>
Copilot 5 months ago
parent
commit
33a0c295c1
1 changed files with 14 additions and 10 deletions
  1. 14 10
      ddns/__main__.py

+ 14 - 10
ddns/__main__.py

@@ -98,20 +98,24 @@ def update_ip(ip_type, cache, dns, ttl, proxy_list):
         error("Fail to get %s address!", ipname)
         return False
 
-    if cache and (address == cache.get(ipname)):
-        info("%s address not changed, using cache.", ipname)
-        return True
-
     record_type = "A" if ip_type == "4" else "AAAA"
     update_success = False
+
+    # Check cache and update each domain individually
     for domain in domains:
         domain = domain.lower()
-        if change_dns_record(dns, proxy_list, domain=domain, ip=address, record_type=record_type, ttl=ttl):
-            warning("set %s[IPv%s]: %s successfully.", domain, ip_type, address)
-            update_success = True
-
-    if isinstance(cache, dict):
-        cache[ipname] = update_success and address
+        cache_key = "{}:{}".format(domain, record_type)
+        if cache and cache.get(cache_key) == address:
+            info("%s[%s] address not changed, using cache: %s", domain, record_type, address)
+            update_success = True  # At least one domain is successfully cached
+        else:
+            # Update domain that is not cached or has different IP
+            if change_dns_record(dns, proxy_list, domain=domain, ip=address, record_type=record_type, ttl=ttl):
+                warning("set %s[IPv%s]: %s successfully.", domain, ip_type, address)
+                update_success = True
+                # Cache successful update immediately
+                if isinstance(cache, dict):
+                    cache[cache_key] = address
 
     return update_success