solana-recover 1.1.3

A comprehensive Solana wallet recovery and account management tool
Documentation
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
//! # NFT Caching and Performance Optimization System
//!
//! Ultra-fast multi-tier caching with intelligent eviction, compression,
//! performance monitoring, and adaptive optimization strategies.

use crate::nft::errors::{NftError, NftResult};
use crate::nft::types::*;
use crate::nft::valuation::ValuationResult;
use crate::nft::security::SecurityValidationResult;
use dashmap::DashMap;
use moka::future::Cache as MokaCache;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

/// Comprehensive cache manager for NFT operations
#[derive(Clone)]
pub struct CacheManager {
    /// L1 cache - hot data with Moka
    l1_cache: Arc<MokaCache<CacheKey, CacheEntry>>,
    
    /// L2 cache - warm data with DashMap
    l2_cache: Arc<DashMap<CacheKey, CacheEntry>>,
    
    /// Cache configuration
    config: CacheConfig,
    
    /// Performance metrics
    metrics: Arc<CacheMetrics>,
    
    /// Compression engine
    compression: Arc<CompressionEngine>,
    
    /// Eviction policy manager
    eviction_manager: Arc<EvictionManager>,
    
    /// Cache warmer
    cache_warmer: Arc<CacheWarmer>,
}

/// Cache configuration
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// L1 cache size (number of entries)
    pub l1_max_entries: u64,
    
    /// L2 cache size (number of entries)
    pub l2_max_entries: u64,
    
    /// L1 TTL in seconds
    pub l1_ttl_seconds: u64,
    
    /// L2 TTL in seconds
    pub l2_ttl_seconds: u64,
    
    /// Enable compression for large entries
    pub enable_compression: bool,
    
    /// Compression threshold in bytes
    pub compression_threshold_bytes: usize,
    
    /// Enable intelligent eviction
    pub enable_intelligent_eviction: bool,
    
    /// Enable cache warming
    pub enable_cache_warming: bool,
    
    /// Enable metrics collection
    pub enable_metrics: bool,
    
    /// Background cleanup interval in seconds
    pub cleanup_interval_seconds: u64,
    
    /// Maximum memory usage in MB
    pub max_memory_mb: u64,
    
    /// Cache hit ratio target (0-1)
    pub hit_ratio_target: f64,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            l1_max_entries: 10000,
            l2_max_entries: 50000,
            l1_ttl_seconds: 300,  // 5 minutes
            l2_ttl_seconds: 1800, // 30 minutes
            enable_compression: true,
            compression_threshold_bytes: 1024, // 1KB
            enable_intelligent_eviction: true,
            enable_cache_warming: true,
            enable_metrics: true,
            cleanup_interval_seconds: 60, // 1 minute
            max_memory_mb: 512, // 512MB
            hit_ratio_target: 0.8,
        }
    }
}

/// Cache performance metrics
#[derive(Debug, Default)]
pub struct CacheMetrics {
    /// Total cache requests
    pub total_requests: Arc<std::sync::atomic::AtomicU64>,
    
    /// L1 cache hits
    pub l1_hits: Arc<std::sync::atomic::AtomicU64>,
    
    /// L2 cache hits
    pub l2_hits: Arc<std::sync::atomic::AtomicU64>,
    
    /// Cache misses
    pub misses: Arc<std::sync::atomic::AtomicU64>,
    
    /// Evictions
    pub evictions: Arc<std::sync::atomic::AtomicU64>,
    
    /// Compressions performed
    pub compressions: Arc<std::sync::atomic::AtomicU64>,
    
    /// Decompressions performed
    pub decompressions: Arc<std::sync::atomic::AtomicU64>,
    
    /// Memory usage in bytes
    pub memory_usage_bytes: Arc<std::sync::atomic::AtomicU64>,
    
    /// Average access time in microseconds
    pub avg_access_time_us: Arc<std::sync::atomic::AtomicU64>,
    
    /// Cache size (number of entries)
    pub cache_size: Arc<std::sync::atomic::AtomicU64>,
    
    /// Hit ratio (0-1)
    pub hit_ratio: Arc<std::sync::atomic::AtomicF64>,
    
    /// Metrics by cache type
    pub metrics_by_type: Arc<DashMap<CacheEntryType, TypeMetrics>>,
}

/// Metrics by cache entry type
#[derive(Debug, Default)]
pub struct TypeMetrics {
    /// Requests for this type
    pub requests: u64,
    
    /// Hits for this type
    pub hits: u64,
    
    /// Misses for this type
    pub misses: u64,
    
    /// Average size for this type
    pub avg_size_bytes: f64,
}

/// Cache key
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CacheKey {
    /// Key type
    pub key_type: CacheKeyType,
    
    /// Key value
    pub value: String,
    
    /// Additional namespace
    pub namespace: Option<String>,
    
    /// Version for cache invalidation
    pub version: Option<u32>,
}

/// Cache key types
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum CacheKeyType {
    /// NFT metadata
    NftMetadata,
    /// NFT valuation
    NftValuation,
    /// Security validation
    SecurityValidation,
    /// Collection data
    CollectionData,
    /// Market data
    MarketData,
    /// Image data
    ImageData,
    /// Custom key type
    Custom { type_name: String },
}

/// Cache entry with metadata
#[derive(Debug, Clone)]
pub struct CacheEntry {
    /// Entry type
    pub entry_type: CacheEntryType,
    
    /// Entry data
    pub data: CacheEntryData,
    
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
    
    /// Last access timestamp
    pub last_accessed: Arc<RwLock<chrono::DateTime<chrono::Utc>>>,
    
    /// Access count
    pub access_count: Arc<std::sync::atomic::AtomicU64>,
    
    /// Size in bytes
    pub size_bytes: usize,
    
    /// Is compressed
    pub compressed: bool,
    
    /// Priority score for eviction
    pub priority_score: Arc<RwLock<f64>>,
    
    /// TTL in seconds
    pub ttl_seconds: u64,
}

/// Cache entry types
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CacheEntryType {
    NftInfo,
    ValuationResult,
    SecurityValidationResult,
    CollectionInfo,
    MarketData,
    ImageData,
    Custom { type_name: String },
}

/// Cache entry data
#[derive(Debug, Clone)]
pub enum CacheEntryData {
    /// NFT information
    NftInfo(NftInfo),
    /// Valuation result
    ValuationResult(ValuationResult),
    /// Security validation result
    SecurityValidationResult(SecurityValidationResult),
    /// Collection information
    CollectionInfo(CollectionInfo),
    /// Market data
    MarketData(crate::nft::valuation::MarketData),
    /// Image data (bytes)
    ImageData(Vec<u8>),
    /// JSON data
    JsonData(serde_json::Value),
    /// Raw bytes
    RawBytes(Vec<u8>),
    /// Custom data
    Custom { type_name: String, data: Vec<u8> },
}

/// Compression engine
#[derive(Clone)]
pub struct CompressionEngine {
    /// Enable compression
    enabled: bool,
    
    /// Compression threshold
    threshold_bytes: usize,
    
    /// Compression algorithm
    algorithm: CompressionAlgorithm,
}

/// Compression algorithms
#[derive(Debug, Clone)]
pub enum CompressionAlgorithm {
    Gzip,
    Lz4,
    Zstd,
}

/// Eviction policy manager
#[derive(Clone)]
pub struct EvictionManager {
    /// Policy configuration
    policy: EvictionPolicy,
    
    /// Eviction statistics
    stats: Arc<EvictionStats>,
}

/// Eviction policies
#[derive(Debug, Clone)]
pub enum EvictionPolicy {
    /// Least Recently Used
    LRU,
    /// Least Frequently Used
    LFU,
    /// Time-based expiration
    TimeBased,
    /// Size-based eviction
    SizeBased,
    /// Adaptive eviction
    Adaptive { weights: HashMap<EvictionFactor, f64> },
}

/// Eviction factors for adaptive policy
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum EvictionFactor {
    AccessFrequency,
    Recency,
    Size,
    Age,
    Priority,
}

/// Eviction statistics
#[derive(Debug, Default)]
pub struct EvictionStats {
    /// Total evictions
    pub total_evictions: Arc<std::sync::atomic::AtomicU64>,
    
    /// Evictions by reason
    pub evictions_by_reason: Arc<DashMap<EvictionReason, u64>>,
    
    /// Evictions by entry type
    pub evictions_by_type: Arc<DashMap<CacheEntryType, u64>>,
    
    /// Memory recovered in bytes
    pub memory_recovered_bytes: Arc<std::sync::atomic::AtomicU64>,
}

/// Eviction reasons
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EvictionReason {
    Expired,
    SizeLimit,
    MemoryLimit,
    LowPriority,
    Manual,
}

/// Cache warmer for proactive loading
#[derive(Clone)]
pub struct CacheWarmer {
    /// Warming configuration
    config: CacheWarmingConfig,
    
    /// Warming statistics
    stats: Arc<WarmingStats>,
}

/// Cache warming configuration
#[derive(Debug, Clone)]
pub struct CacheWarmingConfig {
    /// Enable warming
    pub enabled: bool,
    
    /// Warming interval in seconds
    pub interval_seconds: u64,
    
    /// Maximum items to warm per interval
    pub max_items_per_interval: usize,
    
    /// Warming strategies
    pub strategies: Vec<WarmingStrategy>,
}

/// Warming strategies
#[derive(Debug, Clone)]
pub enum WarmingStrategy {
    /// Warm popular items
    PopularItems { threshold: u64 },
    /// Warm recently accessed items
    RecentlyAccessed { time_window_minutes: u64 },
    /// Warm high-value items
    HighValueItems { min_value_lamports: u64 },
    /// Warm verified collections
    VerifiedCollections,
    /// Custom warming strategy
    Custom { strategy_name: String, config: serde_json::Value },
}

/// Warming statistics
#[derive(Debug, Default)]
pub struct WarmingStats {
    /// Items warmed
    pub items_warmed: Arc<std::sync::atomic::AtomicU64>,
    
    /// Warming successes
    pub warming_successes: Arc<std::sync::atomic::AtomicU64>,
    
    /// Warming failures
    pub warming_failures: Arc<std::sync::atomic::AtomicU64>,
    
    /// Time spent warming in milliseconds
    pub warming_time_ms: Arc<std::sync::atomic::AtomicU64>,
}

impl CacheManager {
    /// Create new cache manager
    pub fn new(config: CacheConfig) -> Self {
        let metrics = Arc::new(CacheMetrics::default());
        let compression = Arc::new(CompressionEngine::new(
            config.enable_compression,
            config.compression_threshold_bytes,
        ));
        let eviction_manager = Arc::new(EvictionManager::new(EvictionPolicy::Adaptive {
            weights: [
                (EvictionFactor::AccessFrequency, 0.3),
                (EvictionFactor::Recency, 0.3),
                (EvictionFactor::Size, 0.2),
                (EvictionFactor::Age, 0.1),
                (EvictionFactor::Priority, 0.1),
            ].into_iter().collect(),
        }));
        let cache_warmer = Arc::new(CacheWarmer::new(CacheWarmingConfig::default()));

        // Initialize L1 cache with Moka
        let l1_cache = Arc::new(
            MokaCache::builder()
                .max_capacity(config.l1_max_entries)
                .time_to_live(Duration::from_secs(config.l1_ttl_seconds))
                .build()
        );

        // Initialize L2 cache with DashMap
        let l2_cache = Arc::new(DashMap::new());

        let cache_manager = Self {
            l1_cache,
            l2_cache,
            metrics,
            compression,
            eviction_manager,
            cache_warmer,
            config,
        };

        // Start background tasks if enabled
        if cache_manager.config.enable_intelligent_eviction {
            cache_manager.start_eviction_task();
        }

        if cache_manager.config.enable_cache_warming {
            cache_manager.start_warming_task();
        }

        cache_manager
    }

    /// Get NFT from cache
    pub async fn get_nft(&self, key: &CacheKey) -> Option<NftInfo> {
        let start_time = Instant::now();
        self.metrics.total_requests.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        // Try L1 cache first
        if let Some(entry) = self.l1_cache.get(key).await {
            self.metrics.l1_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.update_access_stats(&entry).await;
            
            let access_time_us = start_time.elapsed().as_micros() as u64;
            self.metrics.avg_access_time_us.fetch_add(access_time_us, std::sync::atomic::Ordering::Relaxed);
            
            match &entry.data {
                CacheEntryData::NftInfo(nft_info) => return Some(nft_info.clone()),
                _ => warn!("Cache entry type mismatch for key {:?}", key),
            }
        }

        // Try L2 cache
        if let Some(entry) = self.l2_cache.get(key) {
            self.metrics.l2_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.update_access_stats(&entry).await;
            
            // Promote to L1 cache
            self.l1_cache.insert(key.clone(), entry.clone()).await;
            
            let access_time_us = start_time.elapsed().as_micros() as u64;
            self.metrics.avg_access_time_us.fetch_add(access_time_us, std::sync::atomic::Ordering::Relaxed);
            
            match &entry.data {
                CacheEntryData::NftInfo(nft_info) => return Some(nft_info.clone()),
                _ => warn!("Cache entry type mismatch for key {:?}", key),
            }
        }

        // Cache miss
        self.metrics.misses.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.update_type_metrics(CacheEntryType::NftInfo, false).await;
        
        None
    }

    /// Set NFT in cache
    pub async fn set_nft(&self, key: &CacheKey, nft_info: &NftInfo) {
        let serialized_size = serde_json::to_vec(nft_info).unwrap_or_default().len();
        
        let entry = CacheEntry {
            entry_type: CacheEntryType::NftInfo,
            data: CacheEntryData::NftInfo(nft_info.clone()),
            created_at: chrono::Utc::now(),
            last_accessed: Arc::new(RwLock::new(chrono::Utc::now())),
            access_count: Arc::new(std::sync::atomic::AtomicU64::new(1)),
            size_bytes: serialized_size,
            compressed: false,
            priority_score: Arc::new(RwLock::new(1.0)),
            ttl_seconds: self.config.l1_ttl_seconds,
        };

        // Store in L1 cache
        self.l1_cache.insert(key.clone(), entry.clone()).await;
        
        // Also store in L2 for backup
        self.l2_cache.insert(key.clone(), entry);
        
        self.metrics.cache_size.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.metrics.memory_usage_bytes.fetch_add(
            serialized_size as u64,
            std::sync::atomic::Ordering::Relaxed
        );
        self.update_type_metrics(CacheEntryType::NftInfo, true).await;
    }

    /// Get valuation from cache
    pub async fn get_valuation(&self, key: &CacheKey) -> Option<ValuationResult> {
        let start_time = Instant::now();
        self.metrics.total_requests.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        // Try L1 cache first
        if let Some(entry) = self.l1_cache.get(key).await {
            self.metrics.l1_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.update_access_stats(&entry).await;
            
            let access_time_us = start_time.elapsed().as_micros() as u64;
            self.metrics.avg_access_time_us.fetch_add(access_time_us, std::sync::atomic::Ordering::Relaxed);
            
            match &entry.data {
                CacheEntryData::ValuationResult(result) => return Some(result.clone()),
                _ => warn!("Cache entry type mismatch for key {:?}", key),
            }
        }

        // Try L2 cache
        if let Some(entry) = self.l2_cache.get(key) {
            self.metrics.l2_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.update_access_stats(&entry).await;
            
            // Promote to L1 cache
            self.l1_cache.insert(key.clone(), entry.clone()).await;
            
            let access_time_us = start_time.elapsed().as_micros() as u64;
            self.metrics.avg_access_time_us.fetch_add(access_time_us, std::sync::atomic::Ordering::Relaxed);
            
            match &entry.data {
                CacheEntryData::ValuationResult(result) => return Some(result.clone()),
                _ => warn!("Cache entry type mismatch for key {:?}", key),
            }
        }

        self.metrics.misses.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.update_type_metrics(CacheEntryType::ValuationResult, false).await;
        
        None
    }

    /// Set valuation in cache
    pub async fn set_valuation(&self, key: &CacheKey, valuation: &ValuationResult) {
        let serialized_size = serde_json::to_vec(valuation).unwrap_or_default().len();
        
        let entry = CacheEntry {
            entry_type: CacheEntryType::ValuationResult,
            data: CacheEntryData::ValuationResult(valuation.clone()),
            created_at: chrono::Utc::now(),
            last_accessed: Arc::new(RwLock::new(chrono::Utc::now())),
            access_count: Arc::new(std::sync::atomic::AtomicU64::new(1)),
            size_bytes: serialized_size,
            compressed: false,
            priority_score: Arc::new(RwLock::new(1.0)),
            ttl_seconds: self.config.l1_ttl_seconds,
        };

        self.l1_cache.insert(key.clone(), entry.clone()).await;
        self.l2_cache.insert(key.clone(), entry);
        
        self.metrics.cache_size.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.metrics.memory_usage_bytes.fetch_add(
            serialized_size as u64,
            std::sync::atomic::Ordering::Relaxed
        );
        self.update_type_metrics(CacheEntryType::ValuationResult, true).await;
    }

    /// Get security validation from cache
    pub async fn get_security_validation(&self, key: &CacheKey) -> Option<SecurityValidationResult> {
        let start_time = Instant::now();
        self.metrics.total_requests.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        // Try L1 cache first
        if let Some(entry) = self.l1_cache.get(key).await {
            self.metrics.l1_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.update_access_stats(&entry).await;
            
            let access_time_us = start_time.elapsed().as_micros() as u64;
            self.metrics.avg_access_time_us.fetch_add(access_time_us, std::sync::atomic::Ordering::Relaxed);
            
            match &entry.data {
                CacheEntryData::SecurityValidationResult(result) => return Some(result.clone()),
                _ => warn!("Cache entry type mismatch for key {:?}", key),
            }
        }

        // Try L2 cache
        if let Some(entry) = self.l2_cache.get(key) {
            self.metrics.l2_hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.update_access_stats(&entry).await;
            
            // Promote to L1 cache
            self.l1_cache.insert(key.clone(), entry.clone()).await;
            
            let access_time_us = start_time.elapsed().as_micros() as u64;
            self.metrics.avg_access_time_us.fetch_add(access_time_us, std::sync::atomic::Ordering::Relaxed);
            
            match &entry.data {
                CacheEntryData::SecurityValidationResult(result) => return Some(result.clone()),
                _ => warn!("Cache entry type mismatch for key {:?}", key),
            }
        }

        self.metrics.misses.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.update_type_metrics(CacheEntryType::SecurityValidationResult, false).await;
        
        None
    }

    /// Set security validation in cache
    pub async fn set_security_validation(&self, key: &CacheKey, validation: &SecurityValidationResult) {
        let serialized_size = serde_json::to_vec(validation).unwrap_or_default().len();
        
        let entry = CacheEntry {
            entry_type: CacheEntryType::SecurityValidationResult,
            data: CacheEntryData::SecurityValidationResult(validation.clone()),
            created_at: chrono::Utc::now(),
            last_accessed: Arc::new(RwLock::new(chrono::Utc::now())),
            access_count: Arc::new(std::sync::atomic::AtomicU64::new(1)),
            size_bytes: serialized_size,
            compressed: false,
            priority_score: Arc::new(RwLock::new(1.0)),
            ttl_seconds: self.config.l1_ttl_seconds,
        };

        self.l1_cache.insert(key.clone(), entry.clone()).await;
        self.l2_cache.insert(key.clone(), entry);
        
        self.metrics.cache_size.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.metrics.memory_usage_bytes.fetch_add(
            serialized_size as u64,
            std::sync::atomic::Ordering::Relaxed
        );
        self.update_type_metrics(CacheEntryType::SecurityValidationResult, true).await;
    }

    /// Invalidate cache entry
    pub async fn invalidate(&self, key: &CacheKey) {
        self.l1_cache.remove(key).await;
        self.l2_cache.remove(key);
        self.metrics.cache_size.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
    }

    /// Clear all cache entries
    pub async fn clear(&self) {
        self.l1_cache.invalidate_all();
        self.l2_cache.clear();
        self.metrics.cache_size.store(0, std::sync::atomic::Ordering::Relaxed);
        self.metrics.memory_usage_bytes.store(0, std::sync::atomic::Ordering::Relaxed);
    }

    /// Get cache statistics
    pub async fn get_stats(&self) -> CacheStats {
        let total_requests = self.metrics.total_requests.load(std::sync::atomic::Ordering::Relaxed);
        let total_hits = self.metrics.l1_hits.load(std::sync::atomic::Ordering::Relaxed) + 
                         self.metrics.l2_hits.load(std::sync::atomic::Ordering::Relaxed);
        let hit_ratio = if total_requests > 0 {
            total_hits as f64 / total_requests as f64
        } else {
            0.0
        };

        CacheStats {
            total_requests,
            l1_hits: self.metrics.l1_hits.load(std::sync::atomic::Ordering::Relaxed),
            l2_hits: self.metrics.l2_hits.load(std::sync::atomic::Ordering::Relaxed),
            misses: self.metrics.misses.load(std::sync::atomic::Ordering::Relaxed),
            hit_ratio,
            cache_size: self.metrics.cache_size.load(std::sync::atomic::Ordering::Relaxed),
            memory_usage_mb: self.metrics.memory_usage_bytes.load(std::sync::atomic::Ordering::Relaxed) / 1024 / 1024,
            avg_access_time_us: self.metrics.avg_access_time_us.load(std::sync::atomic::Ordering::Relaxed),
            evictions: self.metrics.evictions.load(std::sync::atomic::Ordering::Relaxed),
            compressions: self.metrics.compressions.load(std::sync::atomic::Ordering::Relaxed),
            decompressions: self.metrics.decompressions.load(std::sync::atomic::Ordering::Relaxed),
        }
    }

    /// Update access statistics for an entry
    async fn update_access_stats(&self, entry: &CacheEntry) {
        let now = chrono::Utc::now();
        *entry.last_accessed.write().await = now;
        entry.access_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        
        // Update priority score based on access patterns
        let _current_score = *entry.priority_score.read().await;
        let access_count = entry.access_count.load(std::sync::atomic::Ordering::Relaxed) as f64;
        let age_hours = (now - entry.created_at).num_hours() as f64;
        
        let new_score = (access_count / (age_hours + 1.0)).min(100.0);
        *entry.priority_score.write().await = new_score;
    }

    /// Update type-specific metrics
    async fn update_type_metrics(&self, entry_type: CacheEntryType, is_hit: bool) {
        if !self.config.enable_metrics {
            return;
        }

        let mut type_metrics = self.metrics.metrics_by_type
            .entry(entry_type)
            .or_insert_with(TypeMetrics::default);
        
        type_metrics.requests += 1;
        if is_hit {
            type_metrics.hits += 1;
        } else {
            type_metrics.misses += 1;
        }
    }

    /// Start eviction task
    fn start_eviction_task(&self) {
        // In a real implementation, this would spawn a background task
        // For now, we'll just note that this would be implemented
        info!("Eviction task started");
    }

    /// Start warming task
    fn start_warming_task(&self) {
        // In a real implementation, this would spawn a background task
        // For now, we'll just note that this would be implemented
        info!("Cache warming task started");
    }

    /// Get performance metrics
    pub fn get_metrics(&self) -> &CacheMetrics {
        &self.metrics
    }
}

/// Cache statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
    pub total_requests: u64,
    pub l1_hits: u64,
    pub l2_hits: u64,
    pub misses: u64,
    pub hit_ratio: f64,
    pub cache_size: u64,
    pub memory_usage_mb: u64,
    pub avg_access_time_us: u64,
    pub evictions: u64,
    pub compressions: u64,
    pub decompressions: u64,
}

impl CompressionEngine {
    pub fn new(enabled: bool, threshold_bytes: usize) -> Self {
        Self {
            enabled,
            threshold_bytes,
            algorithm: CompressionAlgorithm::Gzip,
        }
    }

    pub fn compress(&self, data: &[u8]) -> NftResult<Vec<u8>> {
        if !self.enabled || data.len() < self.threshold_bytes {
            return Ok(data.to_vec());
        }

        match self.algorithm {
            CompressionAlgorithm::Gzip => {
                use flate2::write::GzEncoder;
                use flate2::Compression;
                use std::io::Write;
                
                let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
                encoder.write_all(data)
                    .map_err(|e| NftError::Serialization {
                        message: format!("Compression failed: {}", e),
                        format: Some("gzip".to_string()),
                        data_type: Some("bytes".to_string()),
                    })?;
                
                encoder.finish()
                    .map_err(|e| NftError::Serialization {
                        message: format!("Compression finish failed: {}", e),
                        format: Some("gzip".to_string()),
                        data_type: Some("bytes".to_string()),
                    })
            }
            CompressionAlgorithm::Lz4 => {
                // Placeholder for LZ4 compression
                Ok(data.to_vec())
            }
            CompressionAlgorithm::Zstd => {
                // Placeholder for Zstd compression
                Ok(data.to_vec())
            }
        }
    }

    pub fn decompress(&self, compressed_data: &[u8]) -> NftResult<Vec<u8>> {
        if !self.enabled {
            return Ok(compressed_data.to_vec());
        }

        match self.algorithm {
            CompressionAlgorithm::Gzip => {
                use flate2::read::GzDecoder;
                use std::io::Read;
                
                let mut decoder = GzDecoder::new(compressed_data);
                let mut decompressed = Vec::new();
                
                decoder.read_to_end(&mut decompressed)
                    .map_err(|e| NftError::Serialization {
                        message: format!("Decompression failed: {}", e),
                        format: Some("gzip".to_string()),
                        data_type: Some("bytes".to_string()),
                    })?;
                
                Ok(decompressed)
            }
            CompressionAlgorithm::Lz4 => {
                // Placeholder for LZ4 decompression
                Ok(compressed_data.to_vec())
            }
            CompressionAlgorithm::Zstd => {
                // Placeholder for Zstd decompression
                Ok(compressed_data.to_vec())
            }
        }
    }
}

impl EvictionManager {
    pub fn new(policy: EvictionPolicy) -> Self {
        Self {
            policy,
            stats: Arc::new(EvictionStats::default()),
        }
    }
}

impl CacheWarmer {
    pub fn new(config: CacheWarmingConfig) -> Self {
        Self {
            config,
            stats: Arc::new(WarmingStats::default()),
        }
    }
}

impl Default for CacheWarmingConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            interval_seconds: 300, // 5 minutes
            max_items_per_interval: 100,
            strategies: vec![
                WarmingStrategy::PopularItems { threshold: 10 },
                WarmingStrategy::RecentlyAccessed { time_window_minutes: 60 },
            ],
        }
    }
}

impl CacheKey {
    /// Create cache key for NFT metadata
    pub fn metadata(mint_address: &str) -> Self {
        Self {
            key_type: CacheKeyType::NftMetadata,
            value: mint_address.to_string(),
            namespace: None,
            version: Some(1),
        }
    }

    /// Create cache key for NFT valuation
    pub fn valuation(mint_address: &str) -> Self {
        Self {
            key_type: CacheKeyType::NftValuation,
            value: mint_address.to_string(),
            namespace: None,
            version: Some(1),
        }
    }

    /// Create cache key for security validation
    pub fn security(mint_address: &str) -> Self {
        Self {
            key_type: CacheKeyType::SecurityValidation,
            value: mint_address.to_string(),
            namespace: None,
            version: Some(1),
        }
    }

    /// Create cache key for collection data
    pub fn collection(collection_id: &str) -> Self {
        Self {
            key_type: CacheKeyType::CollectionData,
            value: collection_id.to_string(),
            namespace: None,
            version: Some(1),
        }
    }

    /// Create cache key for market data
    pub fn market_data(collection_id: &str) -> Self {
        Self {
            key_type: CacheKeyType::MarketData,
            value: collection_id.to_string(),
            namespace: None,
            version: Some(1),
        }
    }

    /// Create cache key for image data
    pub fn image(image_uri: &str) -> Self {
        Self {
            key_type: CacheKeyType::ImageData,
            value: image_uri.to_string(),
            namespace: None,
            version: Some(1),
        }
    }

    /// Create custom cache key
    pub fn custom(type_name: &str, value: &str) -> Self {
        Self {
            key_type: CacheKeyType::Custom { type_name: type_name.to_string() },
            value: value.to_string(),
            namespace: None,
            version: Some(1),
        }
    }
}