-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache_manager.py
More file actions
648 lines (541 loc) · 21 KB
/
cache_manager.py
File metadata and controls
648 lines (541 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""
Smart Caching System (Feature 15)
=================================
Intelligent cache management for efficient incremental scraping.
Reduces bandwidth usage by 60-80% through staleness detection.
Synergizes with:
- Feature 5 (Change Detection): Cache enables efficient diff comparison
- Feature 13 (Proxy Pool): Cache hits reduce proxy bandwidth usage
"""
import hashlib
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any, Set
from dataclasses import dataclass, field
logger = logging.getLogger(__name__)
@dataclass
class CacheConfig:
"""Configuration for cache behavior."""
default_ttl_days: int = 7
stale_threshold_days: int = 14
category_ttl: Dict[str, int] = field(default_factory=lambda: {
'restaurant': 3,
'food': 3,
'cafe': 3,
'bar': 3,
'pet cremation': 30,
'funeral': 30,
'cemetery': 30,
'lawyer': 14,
'attorney': 14,
'dentist': 14,
'doctor': 14,
'plumber': 7,
'electrician': 7,
'hvac': 7,
})
refresh_strategy: str = 'stale_first' # stale_first, random_sample, priority_based
@dataclass
class CacheEntry:
"""Represents a cached business entry."""
dedup_hash: str
table_name: str
cached_at: datetime
ttl_days: int
last_verified: Optional[datetime] = None
refresh_priority: int = 0 # Higher = more urgent to refresh
data_snapshot: Optional[Dict] = None
@property
def expires_at(self) -> datetime:
return self.cached_at + timedelta(days=self.ttl_days)
@property
def is_expired(self) -> bool:
return datetime.now() > self.expires_at
@property
def is_stale(self) -> bool:
"""Stale = past TTL but within stale threshold (soft expiry)."""
stale_at = self.cached_at + timedelta(days=self.ttl_days + 7)
return datetime.now() > self.expires_at and datetime.now() <= stale_at
@property
def freshness_score(self) -> float:
"""0.0 = expired, 1.0 = fresh, value between indicates staleness."""
age = (datetime.now() - self.cached_at).days
if age <= 0:
return 1.0
if age >= self.ttl_days * 2:
return 0.0
return max(0.0, 1.0 - (age / (self.ttl_days * 2)))
class CacheManager:
"""
Smart caching system for Google Maps scraper.
Features:
- TTL-based staleness detection
- Category-specific cache durations
- Refresh priority scoring
- Bandwidth savings tracking
"""
def __init__(self, db, config: Optional[CacheConfig] = None):
"""
Initialize cache manager.
Args:
db: DatabaseManager instance
config: Optional CacheConfig, uses defaults if not provided
"""
self.db = db
self.config = config or CacheConfig()
self._init_cache_table()
# Statistics
self.stats = {
'cache_hits': 0,
'cache_misses': 0,
'bandwidth_saved_kb': 0,
'entries_refreshed': 0,
}
def _init_cache_table(self):
"""Create cache metadata table if not exists."""
self.db.conn.execute("""
CREATE TABLE IF NOT EXISTS cache_metadata (
dedup_hash TEXT PRIMARY KEY,
table_name TEXT NOT NULL,
cached_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ttl_days INTEGER DEFAULT 7,
last_verified TIMESTAMP,
refresh_priority INTEGER DEFAULT 0,
category TEXT,
rating REAL,
review_count INTEGER,
has_website INTEGER DEFAULT 0,
data_checksum TEXT,
UNIQUE(dedup_hash)
)
""")
# Index for efficient queries
self.db.conn.execute("""
CREATE INDEX IF NOT EXISTS idx_cache_table
ON cache_metadata(table_name)
""")
self.db.conn.execute("""
CREATE INDEX IF NOT EXISTS idx_cache_priority
ON cache_metadata(refresh_priority DESC)
""")
self.db.conn.execute("""
CREATE INDEX IF NOT EXISTS idx_cache_freshness
ON cache_metadata(cached_at)
""")
self.db.conn.commit()
logger.info("Cache metadata table initialized")
def get_ttl_for_category(self, category: Optional[str]) -> int:
"""Get appropriate TTL based on business category."""
if not category:
return self.config.default_ttl_days
category_lower = category.lower()
for key, ttl in self.config.category_ttl.items():
if key in category_lower:
return ttl
return self.config.default_ttl_days
def _compute_checksum(self, data: Dict) -> str:
"""Compute checksum of business data for change detection."""
# Include key fields that indicate meaningful changes
check_fields = ['name', 'phone_number', 'rating', 'review_count',
'website_url', 'address_text', 'open_status']
check_data = {k: data.get(k) for k in check_fields}
return hashlib.md5(str(check_data).encode()).hexdigest()
def add_to_cache(self, dedup_hash: str, table_name: str,
business_data: Dict) -> bool:
"""
Add or update a business in the cache.
Args:
dedup_hash: Unique business identifier
table_name: Source table name
business_data: Full business data dict
Returns:
True if added/updated successfully
"""
try:
category = business_data.get('category')
ttl = self.get_ttl_for_category(category)
checksum = self._compute_checksum(business_data)
self.db.conn.execute("""
INSERT OR REPLACE INTO cache_metadata
(dedup_hash, table_name, cached_at, ttl_days, category,
rating, review_count, has_website, data_checksum, refresh_priority)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
""", (
dedup_hash,
table_name,
datetime.now().isoformat(),
ttl,
category,
business_data.get('rating'),
business_data.get('review_count'),
1 if business_data.get('website_url') else 0,
checksum
))
self.db.conn.commit()
return True
except Exception as e:
logger.error(f"Failed to add to cache: {e}")
return False
def bulk_add_to_cache(self, table_name: str,
businesses: List[Dict]) -> int:
"""
Bulk add businesses to cache.
Args:
table_name: Source table name
businesses: List of business data dicts
Returns:
Number of entries added
"""
added = 0
for biz in businesses:
dedup_hash = biz.get('dedup_hash')
if dedup_hash and self.add_to_cache(dedup_hash, table_name, biz):
added += 1
return added
def is_cached(self, dedup_hash: str) -> bool:
"""Check if a business is in cache (regardless of freshness)."""
cursor = self.db.conn.execute(
"SELECT 1 FROM cache_metadata WHERE dedup_hash = ?",
(dedup_hash,)
)
return cursor.fetchone() is not None
def is_fresh(self, dedup_hash: str) -> bool:
"""
Check if a cached business is still fresh (within TTL).
Args:
dedup_hash: Business identifier
Returns:
True if cached and fresh, False otherwise
"""
cursor = self.db.conn.execute("""
SELECT cached_at, ttl_days
FROM cache_metadata
WHERE dedup_hash = ?
""", (dedup_hash,))
row = cursor.fetchone()
if not row:
self.stats['cache_misses'] += 1
return False
cached_at = datetime.fromisoformat(row[0])
ttl_days = row[1]
expires_at = cached_at + timedelta(days=ttl_days)
if datetime.now() <= expires_at:
self.stats['cache_hits'] += 1
# Estimate ~5KB saved per cache hit
self.stats['bandwidth_saved_kb'] += 5
return True
self.stats['cache_misses'] += 1
return False
def get_fresh_hashes(self, table_name: str) -> Set[str]:
"""
Get all fresh (non-expired) hashes for a table.
Args:
table_name: Table to check
Returns:
Set of dedup_hashes that are still fresh
"""
cursor = self.db.conn.execute("""
SELECT dedup_hash, cached_at, ttl_days
FROM cache_metadata
WHERE table_name = ?
""", (table_name,))
fresh_hashes = set()
now = datetime.now()
for row in cursor.fetchall():
cached_at = datetime.fromisoformat(row[1])
ttl_days = row[2]
if now <= cached_at + timedelta(days=ttl_days):
fresh_hashes.add(row[0])
return fresh_hashes
def get_stale_entries(self, table_name: Optional[str] = None,
limit: int = 100) -> List[Dict]:
"""
Get entries that need refreshing (expired but within stale threshold).
Args:
table_name: Optional filter by table
limit: Max entries to return
Returns:
List of stale entries sorted by priority
"""
query = """
SELECT dedup_hash, table_name, cached_at, ttl_days,
refresh_priority, category, rating, review_count
FROM cache_metadata
WHERE datetime(cached_at, '+' || ttl_days || ' days') < datetime('now')
"""
params = []
if table_name:
query += " AND table_name = ?"
params.append(table_name)
query += " ORDER BY refresh_priority DESC, cached_at ASC LIMIT ?"
params.append(limit)
cursor = self.db.conn.execute(query, params)
return [
{
'dedup_hash': row[0],
'table_name': row[1],
'cached_at': row[2],
'ttl_days': row[3],
'refresh_priority': row[4],
'category': row[5],
'rating': row[6],
'review_count': row[7],
}
for row in cursor.fetchall()
]
def get_entries_to_refresh(self, strategy: Optional[str] = None,
limit: int = 100) -> List[str]:
"""
Get list of dedup_hashes that should be refreshed.
Args:
strategy: Override default refresh strategy
limit: Max entries to return
Returns:
List of dedup_hashes to refresh
"""
strategy = strategy or self.config.refresh_strategy
if strategy == 'stale_first':
# Prioritize oldest expired entries
entries = self.get_stale_entries(limit=limit)
return [e['dedup_hash'] for e in entries]
elif strategy == 'priority_based':
# Prioritize high-priority entries
cursor = self.db.conn.execute("""
SELECT dedup_hash FROM cache_metadata
WHERE datetime(cached_at, '+' || ttl_days || ' days') < datetime('now')
ORDER BY refresh_priority DESC
LIMIT ?
""", (limit,))
return [row[0] for row in cursor.fetchall()]
elif strategy == 'random_sample':
# Random selection from expired
cursor = self.db.conn.execute("""
SELECT dedup_hash FROM cache_metadata
WHERE datetime(cached_at, '+' || ttl_days || ' days') < datetime('now')
ORDER BY RANDOM()
LIMIT ?
""", (limit,))
return [row[0] for row in cursor.fetchall()]
else:
logger.warning(f"Unknown strategy: {strategy}, using stale_first")
return self.get_entries_to_refresh('stale_first', limit)
def update_refresh_priority(self, dedup_hash: str, priority: int):
"""Update refresh priority for an entry."""
self.db.conn.execute("""
UPDATE cache_metadata
SET refresh_priority = ?
WHERE dedup_hash = ?
""", (priority, dedup_hash))
self.db.conn.commit()
def mark_refreshed(self, dedup_hash: str, new_data: Optional[Dict] = None):
"""
Mark an entry as refreshed (reset TTL).
Args:
dedup_hash: Business identifier
new_data: Optional new data to update checksum
"""
updates = ["cached_at = ?", "refresh_priority = 0"]
params = [datetime.now().isoformat()]
if new_data:
checksum = self._compute_checksum(new_data)
updates.append("data_checksum = ?")
params.append(checksum)
if new_data.get('rating'):
updates.append("rating = ?")
params.append(new_data['rating'])
if new_data.get('review_count'):
updates.append("review_count = ?")
params.append(new_data['review_count'])
params.append(dedup_hash)
self.db.conn.execute(f"""
UPDATE cache_metadata
SET {', '.join(updates)}
WHERE dedup_hash = ?
""", params)
self.db.conn.commit()
self.stats['entries_refreshed'] += 1
def detect_changes(self, dedup_hash: str, new_data: Dict) -> Dict[str, Any]:
"""
Detect what changed between cached and new data.
Args:
dedup_hash: Business identifier
new_data: New scraped data
Returns:
Dict of changes: {field: (old_value, new_value)}
"""
cursor = self.db.conn.execute("""
SELECT rating, review_count, has_website, data_checksum
FROM cache_metadata
WHERE dedup_hash = ?
""", (dedup_hash,))
row = cursor.fetchone()
if not row:
return {'status': 'new'}
old_rating, old_reviews, old_has_website, old_checksum = row
new_checksum = self._compute_checksum(new_data)
if old_checksum == new_checksum:
return {'status': 'unchanged'}
changes = {'status': 'changed'}
new_rating = new_data.get('rating')
if old_rating != new_rating and new_rating is not None:
changes['rating'] = (old_rating, new_rating)
new_reviews = new_data.get('review_count')
if old_reviews != new_reviews and new_reviews is not None:
changes['review_count'] = (old_reviews, new_reviews)
new_has_website = 1 if new_data.get('website_url') else 0
if old_has_website != new_has_website:
changes['has_website'] = (bool(old_has_website), bool(new_has_website))
return changes
def get_cache_stats(self) -> Dict[str, Any]:
"""Get cache statistics."""
stale_threshold = self.config.stale_threshold_days
cursor = self.db.conn.execute("""
SELECT
COUNT(*) as total_entries,
SUM(CASE WHEN datetime(cached_at, '+' || ttl_days || ' days') > datetime('now')
THEN 1 ELSE 0 END) as fresh_entries,
SUM(CASE WHEN datetime(cached_at, '+' || ttl_days || ' days') <= datetime('now')
AND datetime(cached_at, '+' || (ttl_days + ?) || ' days') > datetime('now')
THEN 1 ELSE 0 END) as stale_entries,
SUM(CASE WHEN datetime(cached_at, '+' || (ttl_days + ?) || ' days') <= datetime('now')
THEN 1 ELSE 0 END) as expired_entries,
COUNT(DISTINCT table_name) as tables_cached
FROM cache_metadata
""", (stale_threshold, stale_threshold))
row = cursor.fetchone()
hit_rate = 0
total_lookups = self.stats['cache_hits'] + self.stats['cache_misses']
if total_lookups > 0:
hit_rate = self.stats['cache_hits'] / total_lookups * 100
# Get per-table stats
tables_stats = {}
cursor2 = self.db.conn.execute("""
SELECT
table_name,
COUNT(*) as total,
SUM(CASE WHEN datetime(cached_at, '+' || ttl_days || ' days') > datetime('now')
THEN 1 ELSE 0 END) as fresh,
SUM(CASE WHEN datetime(cached_at, '+' || ttl_days || ' days') <= datetime('now')
THEN 1 ELSE 0 END) as stale
FROM cache_metadata
GROUP BY table_name
""")
for tbl_row in cursor2.fetchall():
tables_stats[tbl_row[0]] = {
'total': tbl_row[1],
'fresh': tbl_row[2] or 0,
'stale': tbl_row[3] or 0,
}
return {
'total_entries': row[0] or 0,
'fresh': row[1] or 0,
'stale': row[2] or 0,
'expired': row[3] or 0,
'tables_cached': row[4] or 0,
'tables': tables_stats,
'cache_hits': self.stats['cache_hits'],
'cache_misses': self.stats['cache_misses'],
'hit_rate_percent': round(hit_rate, 1),
'bandwidth_saved_kb': self.stats['bandwidth_saved_kb'],
'entries_refreshed': self.stats['entries_refreshed'],
}
def sync_table_to_cache(self, table_name: str) -> Dict[str, int]:
"""
Sync all entries from a table into the cache.
Args:
table_name: Table to sync
Returns:
Dict with 'added', 'updated', 'skipped' counts
"""
stats = {'added': 0, 'updated': 0, 'skipped': 0}
# Get all entries from table
try:
cursor = self.db.conn.execute(f"""
SELECT dedup_hash, name, category, rating, review_count,
website_url, phone_number, address_text
FROM "{table_name}"
WHERE dedup_hash IS NOT NULL
""")
except Exception as e:
logger.error(f"Error reading table {table_name}: {e}")
return stats
for row in cursor.fetchall():
dedup_hash = row[0]
if not dedup_hash:
continue
business_data = {
'name': row[1],
'category': row[2],
'rating': row[3],
'review_count': row[4],
'website_url': row[5],
'phone_number': row[6],
'address_text': row[7],
}
# Check if already cached
existing = self.db.conn.execute(
"SELECT 1 FROM cache_metadata WHERE dedup_hash = ?",
(dedup_hash,)
).fetchone()
if existing:
# Update existing entry
self.mark_refreshed(dedup_hash, business_data)
stats['updated'] += 1
else:
# Add new entry
if self.add_to_cache(dedup_hash, table_name, business_data):
stats['added'] += 1
else:
stats['skipped'] += 1
return stats
def clear_cache(self, table_name: Optional[str] = None):
"""
Clear cache entries.
Args:
table_name: If provided, only clear entries for this table
"""
if table_name:
self.db.conn.execute(
"DELETE FROM cache_metadata WHERE table_name = ?",
(table_name,)
)
else:
self.db.conn.execute("DELETE FROM cache_metadata")
self.db.conn.commit()
logger.info(f"Cache cleared: {table_name or 'all'}")
def prune_expired(self, days_past_expiry: int = 30) -> int:
"""
Remove entries that have been expired for too long.
Args:
days_past_expiry: Remove entries expired longer than this
Returns:
Number of entries removed
"""
cursor = self.db.conn.execute("""
DELETE FROM cache_metadata
WHERE datetime(cached_at, '+' || (ttl_days + ?) || ' days') < datetime('now')
""", (days_past_expiry,))
deleted = cursor.rowcount
self.db.conn.commit()
if deleted > 0:
logger.info(f"Pruned {deleted} expired cache entries")
return deleted
def create_cache_manager(db, config_dict: Optional[Dict] = None) -> CacheManager:
"""
Factory function to create CacheManager from config dict.
Args:
db: DatabaseManager instance
config_dict: Optional config from YAML
Returns:
Configured CacheManager
"""
if config_dict:
config = CacheConfig(
default_ttl_days=config_dict.get('default_ttl_days', 7),
stale_threshold_days=config_dict.get('stale_threshold_days', 14),
category_ttl=config_dict.get('category_ttl', {}),
refresh_strategy=config_dict.get('refresh_strategy', 'stale_first')
)
else:
config = CacheConfig()
return CacheManager(db, config)