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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
import time
import json
import pickle
import os
from typing import Any, Optional, Dict
from datetime import datetime, timedelta
from pathlib import Path
from config import Config

try:
    from upstash_redis import Redis
except ImportError:
    Redis = None

class CacheManager:
    def __init__(self):
        self.storage_type = None
        self.cache_dir = None
        self.redis = None
        
        # 尝试初始化存储(优先级:Redis > Disk > Memory)
        self._init_storage()
        
        # 初始化内存缓存
        self.epg: Dict[str, Dict[str, Any]] = {}
        self.cid: Optional[str] = None
        self.cid_time: float = 0
        self.auth: Optional[Dict[str, Any]] = None
        self.auth_time: float = 0
        self.channels: Optional[list] = None
        self.channels_time: float = 0
        self.stream_info: Dict[str, Dict[str, Any]] = {}
        
        # 加载已有缓存
        self._load_cache()
    
    def _init_storage(self):
        """初始化存储(Redis > Disk > Memory)"""
        
        # 1. 尝试 Redis
        redis_url = os.getenv('REDIS_URL', '')
        redis_token = os.getenv('REDIS_TOKEN', '')
        
        if Redis and redis_url and redis_token:
            try:
                self.redis = Redis(url=redis_url, token=redis_token)
                self.redis.ping()
                self.storage_type = 'redis'
                print("✅ 使用 Redis 持久化存储")
                return
            except Exception as e:
                print(f"⚠️ Redis 连接失败: {e}")
                self.redis = None
        
        # 2. 尝试本地磁盘
        cache_dir = Path(os.getenv('CACHE_DIR', '/tmp/cache'))
        
        try:
            cache_dir.mkdir(parents=True, exist_ok=True)
            (cache_dir / 'epg').mkdir(exist_ok=True)
            
            # 测试写入权限
            test_file = cache_dir / '.test'
            test_file.write_text('test')
            test_file.unlink()
            
            self.cache_dir = cache_dir
            self.storage_type = 'disk'
            print(f"✅ 使用本地磁盘缓存: {cache_dir}")
            print(f"   缓存将在容器运行期间保留")
            return
            
        except Exception as e:
            print(f"⚠️ 磁盘缓存不可用: {e}")
        
        # 3. 降级到内存
        self.storage_type = 'memory'
        print("⚠️ 使用内存缓存(容器重启后丢失)")
    
    def _load_cache(self):
        """启动时加载缓存"""
        if self.storage_type == 'redis':
            self._load_from_redis()
        elif self.storage_type == 'disk':
            self._load_from_disk()
    
    def _load_from_redis(self):
        """从 Redis 加载缓存"""
        try:
            # 加载 CID
            cid_data = self.redis.get('cache:cid')
            if cid_data:
                data = json.loads(cid_data)
                self.cid = data['value']
                self.cid_time = data['time']
            
            # 加载 Auth
            auth_data = self.redis.get('cache:auth')
            if auth_data:
                data = json.loads(auth_data)
                self.auth = data['value']
                self.auth_time = data['time']
            
            # 加载 Channels
            channels_data = self.redis.get('cache:channels')
            if channels_data:
                data = json.loads(channels_data)
                self.channels = data['value']
                self.channels_time = data['time']
            
            print("✅ Redis 缓存加载完成")
        except Exception as e:
            print(f"⚠️ Redis 缓存加载失败: {e}")
    
    def _load_from_disk(self):
        """从磁盘加载缓存"""
        try:
            # 加载 EPG 缓存
            epg_file = self.cache_dir / 'epg_cache.pkl'
            if epg_file.exists():
                with open(epg_file, 'rb') as f:
                    self.epg = pickle.load(f)
                print(f"✅ 从磁盘加载 {len(self.epg)} 条 EPG 缓存")
            
            # 加载元数据缓存
            meta_file = self.cache_dir / 'meta_cache.json'
            if meta_file.exists():
                with open(meta_file, 'r') as f:
                    meta = json.load(f)
                    if 'cid' in meta:
                        self.cid = meta['cid'].get('value')
                        self.cid_time = meta['cid'].get('time', 0)
                    if 'auth' in meta:
                        self.auth = meta['auth'].get('value')
                        self.auth_time = meta['auth'].get('time', 0)
                    if 'channels' in meta:
                        self.channels = meta['channels'].get('value')
                        self.channels_time = meta['channels'].get('time', 0)
                print("✅ 从磁盘加载元数据缓存")
                
        except Exception as e:
            print(f"⚠️ 磁盘缓存加载失败: {e}")
    
    def _save_to_disk(self, force=False):
        """保存缓存到磁盘"""
        if self.storage_type != 'disk' or not self.cache_dir:
            return
        
        try:
            # 保存 EPG(使用 pickle,快速)
            epg_file = self.cache_dir / 'epg_cache.pkl'
            with open(epg_file, 'wb') as f:
                pickle.dump(self.epg, f, protocol=pickle.HIGHEST_PROTOCOL)
            
            # 保存元数据(使用 JSON,可读)
            meta_file = self.cache_dir / 'meta_cache.json'
            meta = {
                'cid': {'value': self.cid, 'time': self.cid_time},
                'auth': {'value': self.auth, 'time': self.auth_time},
                'channels': {'value': self.channels, 'time': self.channels_time}
            }
            with open(meta_file, 'w') as f:
                json.dump(meta, f)
            
            if force:
                print(f"💾 磁盘缓存已保存 ({len(self.epg)} 条 EPG)")
        except Exception as e:
            print(f"⚠️ 磁盘缓存保存失败: {e}")
    
    # ==================== CID 缓存 ====================
    
    def get_cid(self) -> Optional[str]:
        if self.cid and (time.time() - self.cid_time < Config.CACHE_TTL['CID']):
            return self.cid
        
        if self.storage_type == 'redis':
            try:
                cid_data = self.redis.get('cache:cid')
                if cid_data:
                    data = json.loads(cid_data)
                    if time.time() - data['time'] < Config.CACHE_TTL['CID']:
                        self.cid = data['value']
                        self.cid_time = data['time']
                        return self.cid
            except:
                pass
        
        return None
    
    def set_cid(self, cid: str):
        self.cid = cid
        self.cid_time = time.time()
        
        if self.storage_type == 'redis':
            try:
                data = {'value': cid, 'time': self.cid_time}
                self.redis.set('cache:cid', json.dumps(data), ex=Config.CACHE_TTL['CID'])
            except:
                pass
        elif self.storage_type == 'disk':
            self._save_to_disk()
    
    # ==================== Auth 缓存 ====================
    
    def get_auth(self) -> Optional[Dict[str, Any]]:
        if self.auth and (time.time() - self.auth_time < Config.CACHE_TTL['AUTH']):
            return self.auth
        
        if self.storage_type == 'redis':
            try:
                auth_data = self.redis.get('cache:auth')
                if auth_data:
                    data = json.loads(auth_data)
                    if time.time() - data['time'] < Config.CACHE_TTL['AUTH']:
                        self.auth = data['value']
                        self.auth_time = data['time']
                        return self.auth
            except:
                pass
        
        return None
    
    def set_auth(self, auth: Dict[str, Any]):
        self.auth = auth
        self.auth_time = time.time()
        
        if self.storage_type == 'redis':
            try:
                data = {'value': auth, 'time': self.auth_time}
                self.redis.set('cache:auth', json.dumps(data), ex=Config.CACHE_TTL['AUTH'])
            except:
                pass
        elif self.storage_type == 'disk':
            self._save_to_disk()
    
    # ==================== Channels 缓存 ====================
    
    def get_channels(self) -> Optional[list]:
        if self.channels and (time.time() - self.channels_time < Config.CACHE_TTL['CHANNELS']):
            return self.channels
        
        if self.storage_type == 'redis':
            try:
                channels_data = self.redis.get('cache:channels')
                if channels_data:
                    data = json.loads(channels_data)
                    if time.time() - data['time'] < Config.CACHE_TTL['CHANNELS']:
                        self.channels = data['value']
                        self.channels_time = data['time']
                        return self.channels
            except:
                pass
        
        return None
    
    def set_channels(self, channels: list):
        self.channels = channels
        self.channels_time = time.time()
        
        if self.storage_type == 'redis':
            try:
                data = {'value': channels, 'time': self.channels_time}
                self.redis.set('cache:channels', json.dumps(data), ex=Config.CACHE_TTL['CHANNELS'])
            except:
                pass
        elif self.storage_type == 'disk':
            self._save_to_disk()
    
    # ==================== Stream 缓存 ====================
    
    def get_stream(self, key: str) -> Optional[str]:
        if key in self.stream_info:
            cached = self.stream_info[key]
            if time.time() - cached['time'] < Config.CACHE_TTL['STREAM']:
                return cached['url']
        return None
    
    def set_stream(self, key: str, url: str):
        self.stream_info[key] = {'url': url, 'time': time.time()}
        
        if len(self.stream_info) > Config.MAX_STREAM_CACHE:
            oldest_key = min(self.stream_info.keys(), 
                           key=lambda k: self.stream_info[k]['time'])
            del self.stream_info[oldest_key]
    
    # ==================== EPG 缓存 ====================
    
    def get_epg(self, vid: str, date: str) -> Optional[any]:
        key = f"{vid}_{date}"
        
        # 检查内存缓存
        if key in self.epg:
            cached = self.epg[key]
            
            if vid == '_all_' and date == 'full':
                if time.time() - cached['time'] < Config.CACHE_TTL['EPG_FULL']:
                    return cached['data']
            else:
                today = self._get_jst_date()
                ttl = Config.CACHE_TTL['EPG_TODAY'] if date == today else Config.CACHE_TTL['EPG_OTHER']
                
                if time.time() - cached['time'] < ttl:
                    return cached['data']
        
        # 如果使用磁盘缓存,尝试从单独文件加载
        if self.storage_type == 'disk' and vid != '_all_':
            try:
                epg_file = self.cache_dir / 'epg' / f"{key}.pkl"
                if epg_file.exists():
                    with open(epg_file, 'rb') as f:
                        cached = pickle.load(f)
                    
                    today = self._get_jst_date()
                    ttl = Config.CACHE_TTL['EPG_TODAY'] if date == today else Config.CACHE_TTL['EPG_OTHER']
                    
                    if time.time() - cached['time'] < ttl:
                        # 加载到内存
                        self.epg[key] = cached
                        return cached['data']
            except:
                pass
        
        return None
    
    def set_epg(self, vid: str, date: str, data: any):
        key = f"{vid}_{date}"
        self.epg[key] = {
            'data': data,
            'time': time.time(),
            'date': date,
            'vid': vid
        }
        
        # Redis 存储
        if self.storage_type == 'redis':
            # Redis 代码保持不变...
            pass
        
        # 磁盘存储
        elif self.storage_type == 'disk':
            # 单独保存每个 EPG 文件(避免频繁写入大文件)
            try:
                epg_file = self.cache_dir / 'epg' / f"{key}.pkl"
                with open(epg_file, 'wb') as f:
                    pickle.dump(self.epg[key], f, protocol=pickle.HIGHEST_PROTOCOL)
            except:
                pass
            
            # 每 20 条更新一次主缓存文件
            if len(self.epg) % 20 == 0:
                self._save_to_disk()
        
        # 清理过期缓存
        if len(self.epg) > Config.MAX_EPG_CACHE:
            self._clean_old_epg()
    
    def _clean_old_epg(self):
        """清理过期 EPG"""
        cutoff_date = (datetime.now() - timedelta(days=Config.CACHE_TTL['EPG_MAX_DAYS'])).strftime('%Y-%m-%d')
        to_delete = [k for k, v in self.epg.items() 
                    if v.get('date', '') < cutoff_date and not k.startswith('_all_')]
        
        for k in to_delete:
            del self.epg[k]
            
            # 删除磁盘文件
            if self.storage_type == 'disk':
                try:
                    epg_file = self.cache_dir / 'epg' / f"{k}.pkl"
                    if epg_file.exists():
                        epg_file.unlink()
                except:
                    pass
    
    def _get_jst_date(self) -> str:
        from datetime import timezone
        jst = timezone(timedelta(hours=9))
        now = datetime.now(jst)
        return now.strftime('%Y-%m-%d')
    
    # ==================== 缓存管理 ====================
    
    def clear_cache(self, cache_type: str = 'all'):
        if cache_type in ['cid', 'all']:
            self.cid = None
            self.cid_time = 0
            if self.storage_type == 'redis':
                try:
                    self.redis.delete('cache:cid')
                except:
                    pass
        
        if cache_type in ['auth', 'all']:
            self.auth = None
            self.auth_time = 0
            if self.storage_type == 'redis':
                try:
                    self.redis.delete('cache:auth')
                except:
                    pass
        
        if cache_type in ['channels', 'all']:
            self.channels = None
            self.channels_time = 0
            if self.storage_type == 'redis':
                try:
                    self.redis.delete('cache:channels')
                except:
                    pass
        
        if cache_type in ['streams', 'all']:
            self.stream_info.clear()
        
        if cache_type in ['epg', 'all']:
            self.epg.clear()
            
            # 清理磁盘文件
            if self.storage_type == 'disk' and self.cache_dir:
                try:
                    for epg_file in (self.cache_dir / 'epg').glob('*.pkl'):
                        epg_file.unlink()
                    (self.cache_dir / 'epg_cache.pkl').unlink(missing_ok=True)
                except:
                    pass
        
        # 保存更改
        if self.storage_type == 'disk':
            self._save_to_disk()
    
    def get_stats(self) -> dict:
        return {
            'storage_type': self.storage_type,
            'cid': {
                'cached': self.cid is not None,
                'value': self.cid if self.cid else None,
                'age': f"{int(time.time() - self.cid_time)}s" if self.cid else None,
                'ttl': f"{Config.CACHE_TTL['CID'] - int(time.time() - self.cid_time)}s" if self.cid else None,
                'storage': self.storage_type
            },
            'auth': {
                'cached': self.auth is not None,
                'token': self.auth['access_token'] if self.auth and 'access_token' in self.auth else None,
                'age': f"{int(time.time() - self.auth_time)}s" if self.auth else None,
                'ttl': f"{Config.CACHE_TTL['AUTH'] - int(time.time() - self.auth_time)}s" if self.auth else None,
                'storage': self.storage_type
            },
            'channels': self.channels is not None,
            'streams': len(self.stream_info),
            'epg': len(self.epg),
            'epg_detail': self.get_epg_cache_info() if self.epg else None
        }
    
    def get_epg_cache_info(self) -> dict:
        """获取 EPG 缓存详细信息"""
        info = {
            'total_entries': len(self.epg),
            'by_channel': {},
            'by_date': {},
            'full_cache_available': False
        }
        
        # 检查全量缓存
        if '_all__full' in self.epg:
            info['full_cache_available'] = True
            full_cache = self.epg['_all__full']
            info['full_cache_time'] = datetime.fromtimestamp(full_cache['time']).strftime('%Y-%m-%d %H:%M:%S')
            info['full_cache_age'] = f"{int(time.time() - full_cache['time'])}s"
        
        # 统计各频道和日期
        for key, value in self.epg.items():
            if key == '_all__full':
                continue
            
            vid = value.get('vid', 'unknown')
            date = value.get('date', 'unknown')
            
            if vid not in info['by_channel']:
                info['by_channel'][vid] = {'dates': [], 'program_count': 0}
            info['by_channel'][vid]['dates'].append(date)
            info['by_channel'][vid]['program_count'] += len(value.get('data', []))
            
            if date not in info['by_date']:
                info['by_date'][date] = {'channels': [], 'program_count': 0}
            info['by_date'][date]['channels'].append(vid)
            info['by_date'][date]['program_count'] += len(value.get('data', []))
        
        info['summary'] = {
            'total_channels': len(info['by_channel']),
            'total_dates': len(info['by_date']),
            'total_programs': sum(ch['program_count'] for ch in info['by_channel'].values())
        }
        
        return info
    
    def __del__(self):
        """程序退出时保存缓存"""
        if self.storage_type == 'disk':
            self._save_to_disk(force=True)
            print("💾 缓存已保存到磁盘")


# 全局单例
cache = CacheManager()