File size: 1,780 Bytes
03bdc4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

function isVercel() {
    // Check common Vercel environment indicators
    if (isset($_ENV['VERCEL']) && $_ENV['VERCEL']) return true;
    if (getenv('VERCEL') === '1') return true;
    if (isset($_SERVER['VERCEL'])) return true;
    
    // Check for Vercel runtime (serverless functions)
    if (isset($_SERVER['HTTP_X_VERCEL_ID'])) return true;
    if (isset($_SERVER['HTTP_X_Vercel'])) return true;
    
    return false;
}

function getCacheDir() {
    // Vercel: use /tmp (writable) for serverless functions
    if (isVercel()) {
        $dir = sys_get_temp_dir() . '/buddhaword_cache';
        if (!is_dir($dir)) {
            @mkdir($dir, 0777, true);
        }
        return $dir;
    }
    
    // HuggingFace and local: use storage/cache
    $cacheDir = __DIR__ . '/../../storage/cache';
    if (!is_dir($cacheDir)) {
        @mkdir($cacheDir, 0777, true);
    }
    return $cacheDir;
}

function getCachePath($filename) {
    return getCacheDir() . '/' . $filename;
}

function readCache($filename, $maxAge = 86400) {
    $path = getCachePath($filename);
    if (file_exists($path) && (time() - filemtime($path) < $maxAge)) {
        return @file_get_contents($path);
    }
    return null;
}

function writeCache($filename, $data) {
    $path = getCachePath($filename);
    $dir = dirname($path);
    
    if (!is_dir($dir)) {
        @mkdir($dir, 0777, true);
    }
    
    $result = @file_put_contents($path, $data);
    return $result !== false;
}

function getCacheVersion($filename) {
    $path = getCachePath($filename);
    return file_exists($path) ? @filemtime($path) : 0;
}

function shouldRefreshCache($filename, $debounce = 30) {
    $path = getCachePath($filename);
    return !file_exists($path) || (time() - filemtime($path) >= $debounce);
}