diff --git a/lib/redis.ts b/lib/redis.ts
index 80bd3825..a26bb96a 100644
--- a/lib/redis.ts
+++ b/lib/redis.ts
@@ -130,7 +130,7 @@ export const KEYS = {
`email:content:${userId}:${accountId}:${emailId}`,
// New widget cache keys
CALENDAR: (userId: string) => `widget:calendar:${userId}`,
- NEWS: () => `widget:news`, // Global news cache, not user-specific
+ NEWS: (limit = '100') => `widget:news:${limit}`, // Include limit in cache key
TASKS: (userId: string) => `widget:tasks:${userId}`,
MESSAGES: (userId: string) => `widget:messages:${userId}`
};
@@ -567,14 +567,15 @@ export async function invalidateCalendarCache(
* Cache news data (global, not user-specific)
*/
export async function cacheNewsData(
- data: any
+ data: any,
+ limit = '100'
): Promise
{
const redis = getRedisClient();
- const key = KEYS.NEWS();
+ const key = KEYS.NEWS(limit);
try {
await redis.set(key, JSON.stringify(data), 'EX', TTL.NEWS);
- console.log('News data cached successfully');
+ console.log(`News data cached successfully (${data.length} articles, limit=${limit})`);
} catch (error) {
console.error('Error caching news data:', error);
}
@@ -583,9 +584,9 @@ export async function cacheNewsData(
/**
* Get cached news data
*/
-export async function getCachedNewsData(): Promise {
+export async function getCachedNewsData(limit = '100'): Promise {
const redis = getRedisClient();
- const key = KEYS.NEWS();
+ const key = KEYS.NEWS(limit);
try {
const cachedData = await redis.get(key);
@@ -593,7 +594,9 @@ export async function getCachedNewsData(): Promise {
return null;
}
- return JSON.parse(cachedData);
+ const parsedData = JSON.parse(cachedData);
+ console.log(`Retrieved ${parsedData.length} articles from cache with limit=${limit}`);
+ return parsedData;
} catch (error) {
console.error('Error getting cached news data:', error);
return null;
@@ -603,13 +606,24 @@ export async function getCachedNewsData(): Promise {
/**
* Invalidate news cache
*/
-export async function invalidateNewsCache(): Promise {
+export async function invalidateNewsCache(limit?: string): Promise {
const redis = getRedisClient();
- const key = KEYS.NEWS();
try {
- await redis.del(key);
- console.log('News cache invalidated');
+ if (limit) {
+ // Invalidate specific limit cache
+ const key = KEYS.NEWS(limit);
+ await redis.del(key);
+ console.log(`News cache invalidated for limit=${limit}`);
+ } else {
+ // Try to invalidate for some common limits
+ const limits = ['5', '50', '100', '200'];
+ for (const lim of limits) {
+ const key = KEYS.NEWS(lim);
+ await redis.del(key);
+ }
+ console.log('All news caches invalidated');
+ }
} catch (error) {
console.error('Error invalidating news cache:', error);
}