File size: 1,212 Bytes
1d31670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from pathlib import Path
from datetime import timedelta

class CacheConfig:
    def __init__(self):
        self.base_path = Path(os.getenv("HF_HOME", "."))
        self.cache_ttl = timedelta(minutes=5)  # 5 minute cache TTL
        
    def get_cache_path(self, cache_type: str = "datasets") -> Path:
        """Get cache path for different cache types"""
        cache_path = self.base_path / "cache" / cache_type
        cache_path.mkdir(parents=True, exist_ok=True)
        return cache_path
    
    def flush_cache(self, cache_type: str = None):
        """Flush specific cache or all caches"""
        if cache_type:
            cache_path = self.get_cache_path(cache_type)
            for file in cache_path.glob("*"):
                if file.is_file():
                    file.unlink()
        else:
            cache_base = self.base_path / "cache"
            if cache_base.exists():
                for cache_dir in cache_base.iterdir():
                    if cache_dir.is_dir():
                        for file in cache_dir.glob("*"):
                            if file.is_file():
                                file.unlink()

# Global cache configuration
cache_config = CacheConfig()