@@ -18,8 +18,7 @@ public class ContentFeedFunctions
1818 private readonly HackerNewsService _hnService ;
1919 private readonly YouTubeService _ytService ;
2020 private readonly RedditService _redditService ;
21- private static readonly ConcurrentDictionary < string , ( List < HackerNewsItemBasicInfo > Items , DateTime ExpirationTime ) > _hnSearchCache = new ( ) ;
22- private static readonly TimeSpan _cacheDuration = TimeSpan . FromMinutes ( 60 ) ;
21+ private static readonly TimeSpan _cacheDuration = TimeSpan . FromMinutes ( 10 ) ;
2322
2423 public ContentFeedFunctions (
2524 ILogger < ContentFeedFunctions > logger ,
@@ -30,6 +29,7 @@ public ContentFeedFunctions(
3029
3130 _configuration = new ConfigurationBuilder ( )
3231 . AddUserSecrets < Program > ( )
32+ . AddJsonFile ( "local.settings.json" )
3333 . Build ( ) ;
3434#else
3535
@@ -139,42 +139,27 @@ public async Task<HttpResponseData> GetTrendingRedditThreads(
139139 }
140140 }
141141
142- private bool TryGetFromCache ( string cacheKey , out List < HackerNewsItemBasicInfo > ? items )
143- {
144- items = null ;
145- if ( _hnSearchCache . TryGetValue ( cacheKey , out var cacheEntry ) )
146- {
147- if ( DateTime . UtcNow <= cacheEntry . ExpirationTime )
148- {
149- items = cacheEntry . Items ;
150- return true ;
151- }
152- _hnSearchCache . TryRemove ( cacheKey , out _ ) ;
153- }
154- return false ;
155- }
156-
157142 [ Function ( "SearchHackerNewsArticles" ) ]
158143 public async Task < HttpResponseData > SearchHackerNewsArticles (
159- [ HttpTrigger ( AuthorizationLevel . Function , "get" ) ] HttpRequestData req )
144+ [ HttpTrigger ( AuthorizationLevel . Function , "get" ) ] HttpRequestData req ,
145+ [ BlobInput ( "hackernews-cache/all.json" ) ] string ? cachedBlob )
160146 {
161147 _logger . LogInformation ( "Processing Hacker News search request" ) ;
162148
163149 try
164150 {
165- var cacheKey = "all" ;
166-
167151 List < HackerNewsItemBasicInfo > matchingItems ;
168- if ( ! TryGetFromCache ( cacheKey , out var cachedItems ) )
152+
153+ if ( ! string . IsNullOrEmpty ( cachedBlob ) )
169154 {
170- matchingItems = await _hnService . SearchByTitleBasicInfo ( ) ;
171- _hnSearchCache . TryAdd ( cacheKey , ( matchingItems , DateTime . UtcNow . Add ( _cacheDuration ) ) ) ;
172- _logger . LogInformation ( "Cache miss for key: {CacheKey}" , cacheKey ) ;
155+ // Use blob cache if available
156+ matchingItems = System . Text . Json . JsonSerializer . Deserialize < List < HackerNewsItemBasicInfo > > ( cachedBlob ) ?? new List < HackerNewsItemBasicInfo > ( ) ;
157+ _logger . LogInformation ( "Blob cache hit" ) ;
173158 }
174159 else
175160 {
176- matchingItems = cachedItems ! ;
177- _logger . LogInformation ( "Cache hit for key: {CacheKey}" , cacheKey ) ;
161+ matchingItems = await _hnService . SearchByTitleBasicInfo ( ) ;
162+ _logger . LogInformation ( "Blob cache miss, fetched from service" ) ;
178163 }
179164
180165 var response = req . CreateResponse ( HttpStatusCode . OK ) ;
@@ -191,28 +176,23 @@ public async Task<HttpResponseData> SearchHackerNewsArticles(
191176 }
192177
193178 [ Function ( "CacheHackerNewsArticlesHourly" ) ]
194- public async Task CacheHackerNewsArticlesHourly (
179+ [ BlobOutput ( "hackernews-cache/all.json" ) ]
180+ public async Task < string > CacheHackerNewsArticlesHourly (
195181 [ TimerTrigger ( "0 0 * * * *" ) ] TimerInfo timerInfo )
196182 {
197183 _logger . LogInformation ( "Starting hourly Hacker News cache refresh at: {Time}" , DateTime . UtcNow ) ;
198184
199185 try
200186 {
201- var cacheKey = "all" ;
202- var items = await _hnService . SearchByTitleBasicInfo ( ) ;
203- _hnSearchCache . AddOrUpdate (
204- cacheKey ,
205- ( items , DateTime . UtcNow . Add ( _cacheDuration ) ) ,
206- ( key , old ) => ( items , DateTime . UtcNow . Add ( _cacheDuration ) )
207- ) ;
208-
209-
210- _logger . LogInformation ( "Pre-cached Hacker News articles for key: {CacheKey}" , cacheKey ) ;
211-
187+ var items = await _hnService . SearchByTitleBasicInfo ( ) ;
188+ _logger . LogInformation ( "Pre-cached Hacker News articles" ) ;
189+ // Serialize items to JSON for blob output
190+ return System . Text . Json . JsonSerializer . Serialize ( items ) ;
212191 }
213192 catch ( Exception ex )
214193 {
215194 _logger . LogError ( ex , "Error during hourly Hacker News cache refresh" ) ;
195+ return string . Empty ;
216196 }
217197 }
218198}
0 commit comments