Coverage for fastblocks / adapters / sitemap / cached.py: 38%

85 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:00 -0800

1"""FastBlocks Cached Sitemap Adapter. 

2 

3Heavy caching wrapper for sitemap generation with background refresh 

4and persistent storage for high-performance sitemap serving. 

5""" 

6 

7import asyncio 

8import typing as t 

9from contextlib import suppress 

10from uuid import UUID 

11 

12 

13# Custom implementations for ACB compatibility 

14class AdapterStatus: 

15 """Custom AdapterStatus for Oneiric compatibility.""" 

16 

17 STABLE = "STABLE" 

18 BETA = "BETA" 

19 ALPHA = "ALPHA" 

20 EXPERIMENTAL = "EXPERIMENTAL" 

21 

22 

23def debug(msg: str) -> None: 

24 """Custom debug function for Oneiric compatibility.""" 

25 print(f"[DEBUG] {msg}") 

26 

27 

28from oneiric.core.resolution import Resolver 

29 

30# Oneiric resolver for dependency injection 

31depends = Resolver() 

32 

33 

34def import_adapter(adapter_name: str) -> None: 

35 """Custom implementation for Oneiric compatibility.""" 

36 return None 

37 

38 

39from ._base import SitemapBase, SitemapBaseSettings 

40from .core import BaseSitemap, SitemapApp 

41 

42 

43class CachedSitemapSettings(SitemapBaseSettings): 

44 pass 

45 

46 

47class CachedSitemap(BaseSitemap[str], SitemapBase): 

48 sitemap: SitemapApp | None = None 

49 _background_task: asyncio.Task[t.Any] | None = None 

50 

51 def __init__(self) -> None: 

52 super().__init__() 

53 self._underlying_adapter = None 

54 

55 async def items(self) -> t.Any: 

56 try: 

57 routes_adapter = await depends.resolve("fastblocks", "routes") 

58 if routes_adapter and hasattr(routes_adapter, "routes"): 

59 route_paths = [r.path for r in routes_adapter.routes] 

60 debug(f"CachedSitemap: Cached {len(route_paths)} routes") 

61 return route_paths 

62 return [] 

63 except Exception as e: 

64 debug(f"CachedSitemap: Error getting routes: {e}") 

65 return [] 

66 

67 def location(self, item: str) -> str: 

68 return item 

69 

70 def changefreq(self, item: str) -> str: 

71 return t.cast(str, self.config.change_freq) 

72 

73 def priority(self, item: str) -> float: 

74 if item == "/": 

75 return 1.0 

76 segments = len([s for s in item.split("/") if s]) 

77 if segments == 1: 

78 return 0.8 

79 if segments == 2: 

80 return 0.6 

81 return 0.4 

82 

83 async def _background_refresh(self) -> None: 

84 strategy_options = self.config.strategy_options 

85 background_refresh = strategy_options.get("background_refresh", True) 

86 if not background_refresh: 

87 return 

88 debug("CachedSitemap: Starting background refresh task") 

89 while True: 

90 try: 

91 await asyncio.sleep(self.config.cache_ttl) 

92 debug("CachedSitemap: Refreshing sitemap cache") 

93 except asyncio.CancelledError: 

94 debug("CachedSitemap: Background refresh cancelled") 

95 break 

96 except Exception as e: 

97 debug(f"CachedSitemap: Background refresh error: {e}") 

98 await asyncio.sleep(300) 

99 

100 async def init(self) -> None: 

101 if not self.config.domain: 

102 msg = "domain must be set in sitemap settings" 

103 raise ValueError(msg) 

104 extended_ttl = self.config.cache_ttl * 2 

105 self.sitemap = SitemapApp( 

106 self, 

107 domain=self.config.domain, 

108 cache_ttl=extended_ttl, 

109 ) 

110 strategy_options = self.config.strategy_options 

111 if strategy_options.get("background_refresh", True): 

112 self._background_task = asyncio.create_task(self._background_refresh()) 

113 debug(f"CachedSitemap: Initialized with domain={self.config.domain}") 

114 

115 async def cleanup(self) -> None: 

116 if self._background_task: 

117 self._background_task.cancel() 

118 try: 

119 await self._background_task 

120 except asyncio.CancelledError: 

121 pass 

122 

123 

124Sitemap = CachedSitemap 

125 

126MODULE_ID = UUID("01937d86-cfa2-73b4-0675-901234567823") 

127MODULE_STATUS = AdapterStatus.STABLE 

128 

129with suppress(Exception): 

130 depends.set(Sitemap)