things3-core 2.1.0

Core library for Things 3 database access and data models
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
//! L3 Database query result cache with smart invalidation

use crate::models::{Area, Project, Task, ThingsId};
use anyhow::Result;
use chrono::{DateTime, Utc};
use moka::future::Cache;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, warn};

/// Query cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryCacheConfig {
    /// Maximum number of cached queries
    pub max_queries: u64,
    /// Time to live for cached queries
    pub ttl: Duration,
    /// Time to idle for cached queries
    pub tti: Duration,
    /// Enable query result compression
    pub enable_compression: bool,
    /// Maximum query result size to cache (in bytes)
    pub max_result_size: usize,
}

impl Default for QueryCacheConfig {
    fn default() -> Self {
        Self {
            max_queries: 1000,
            ttl: Duration::from_secs(1800), // 30 minutes
            tti: Duration::from_secs(300),  // 5 minutes
            enable_compression: true,
            max_result_size: 1024 * 1024, // 1MB
        }
    }
}

/// Cached query result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedQueryResult<T> {
    /// The actual query result
    pub data: T,
    /// When the query was executed
    pub executed_at: DateTime<Utc>,
    /// When the result expires
    pub expires_at: DateTime<Utc>,
    /// Query execution time
    pub execution_time_ms: u64,
    /// Query parameters hash for invalidation
    pub params_hash: String,
    /// Tables/entities this query depends on
    pub dependencies: Vec<QueryDependency>,
    /// Query result size in bytes
    pub result_size: usize,
    /// Whether the result is compressed
    pub compressed: bool,
}

/// Query dependency for smart invalidation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct QueryDependency {
    /// Table name
    pub table: String,
    /// Specific entity ID (if applicable)
    pub entity_id: Option<ThingsId>,
    /// Operations that would invalidate this query
    pub invalidating_operations: Vec<String>,
}

/// Query cache statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QueryCacheStats {
    pub total_queries: u64,
    pub hits: u64,
    pub misses: u64,
    pub hit_rate: f64,
    pub total_size_bytes: u64,
    pub average_execution_time_ms: f64,
    pub compressed_queries: u64,
    pub uncompressed_queries: u64,
}

impl QueryCacheStats {
    pub fn calculate_hit_rate(&mut self) {
        let total = self.hits + self.misses;
        self.hit_rate = if total > 0 {
            #[allow(clippy::cast_precision_loss)]
            {
                self.hits as f64 / total as f64
            }
        } else {
            0.0
        };
    }
}

/// L3 Database query result cache
pub struct QueryCache {
    /// Tasks query cache
    tasks_cache: Cache<String, CachedQueryResult<Vec<Task>>>,
    /// Projects query cache
    projects_cache: Cache<String, CachedQueryResult<Vec<Project>>>,
    /// Areas query cache
    areas_cache: Cache<String, CachedQueryResult<Vec<Area>>>,
    /// Search results query cache
    search_cache: Cache<String, CachedQueryResult<Vec<Task>>>,
    /// Statistics
    stats: Arc<RwLock<QueryCacheStats>>,
    /// Configuration
    config: QueryCacheConfig,
}

impl QueryCache {
    /// Create a new query cache
    #[must_use]
    pub fn new(config: QueryCacheConfig) -> Self {
        let tasks_cache = Cache::builder()
            .max_capacity(config.max_queries)
            .time_to_live(config.ttl)
            .time_to_idle(config.tti)
            .build();

        let projects_cache = Cache::builder()
            .max_capacity(config.max_queries)
            .time_to_live(config.ttl)
            .time_to_idle(config.tti)
            .build();

        let areas_cache = Cache::builder()
            .max_capacity(config.max_queries)
            .time_to_live(config.ttl)
            .time_to_idle(config.tti)
            .build();

        let search_cache = Cache::builder()
            .max_capacity(config.max_queries)
            .time_to_live(config.ttl)
            .time_to_idle(config.tti)
            .build();

        Self {
            tasks_cache,
            projects_cache,
            areas_cache,
            search_cache,
            stats: Arc::new(RwLock::new(QueryCacheStats::default())),
            config,
        }
    }

    /// Create a new query cache with default configuration
    #[must_use]
    pub fn new_default() -> Self {
        Self::new(QueryCacheConfig::default())
    }

    /// Cache a tasks query result
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The fetcher function fails
    /// - Cache operations fail
    /// - Serialization/deserialization fails
    pub async fn cache_tasks_query<F, Fut>(
        &self,
        query_key: &str,
        params_hash: &str,
        fetcher: F,
    ) -> Result<Vec<Task>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<Vec<Task>>>,
    {
        // Check if query is already cached
        if let Some(cached) = self.tasks_cache.get(query_key).await {
            if !cached.is_expired() && cached.params_hash == params_hash {
                self.record_hit();
                debug!("Query cache hit for tasks: {}", query_key);
                return Ok(cached.data);
            }
        }

        // Execute the query
        let start_time = std::time::Instant::now();
        let data = fetcher().await?;
        #[allow(clippy::cast_possible_truncation)]
        let execution_time = start_time.elapsed().as_millis() as u64;

        // Check if result is too large to cache
        let result_size = Self::calculate_result_size(&data);
        if result_size > self.config.max_result_size {
            warn!("Query result too large to cache: {} bytes", result_size);
            self.record_miss();
            return Ok(data);
        }

        // Create dependencies for smart invalidation
        let dependencies = Self::create_task_dependencies(&data);

        // Create cached result
        let cached_result = CachedQueryResult {
            data: data.clone(),
            executed_at: Utc::now(),
            expires_at: Utc::now()
                + chrono::Duration::from_std(self.config.ttl).unwrap_or_default(),
            execution_time_ms: execution_time,
            params_hash: params_hash.to_string(),
            dependencies,
            result_size,
            compressed: self.config.enable_compression,
        };

        // Store in cache
        self.tasks_cache
            .insert(query_key.to_string(), cached_result)
            .await;

        // Update statistics
        self.update_stats(result_size, execution_time, false);

        self.record_miss();
        debug!(
            "Cached tasks query: {} ({}ms, {} bytes)",
            query_key, execution_time, result_size
        );
        Ok(data)
    }

    /// Cache a projects query result
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The fetcher function fails
    /// - Cache operations fail
    /// - Serialization/deserialization fails
    pub async fn cache_projects_query<F, Fut>(
        &self,
        query_key: &str,
        params_hash: &str,
        fetcher: F,
    ) -> Result<Vec<Project>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<Vec<Project>>>,
    {
        // Check if query is already cached
        if let Some(cached) = self.projects_cache.get(query_key).await {
            if !cached.is_expired() && cached.params_hash == params_hash {
                self.record_hit();
                debug!("Query cache hit for projects: {}", query_key);
                return Ok(cached.data);
            }
        }

        // Execute the query
        let start_time = std::time::Instant::now();
        let data = fetcher().await?;
        #[allow(clippy::cast_possible_truncation)]
        let execution_time = start_time.elapsed().as_millis() as u64;

        // Check if result is too large to cache
        let result_size = Self::calculate_result_size(&data);
        if result_size > self.config.max_result_size {
            warn!("Query result too large to cache: {} bytes", result_size);
            self.record_miss();
            return Ok(data);
        }

        // Create dependencies for smart invalidation
        let dependencies = Self::create_project_dependencies(&data);

        // Create cached result
        let cached_result = CachedQueryResult {
            data: data.clone(),
            executed_at: Utc::now(),
            expires_at: Utc::now()
                + chrono::Duration::from_std(self.config.ttl).unwrap_or_default(),
            execution_time_ms: execution_time,
            params_hash: params_hash.to_string(),
            dependencies,
            result_size,
            compressed: self.config.enable_compression,
        };

        // Store in cache
        self.projects_cache
            .insert(query_key.to_string(), cached_result)
            .await;

        // Update statistics
        self.update_stats(result_size, execution_time, false);

        self.record_miss();
        debug!(
            "Cached projects query: {} ({}ms, {} bytes)",
            query_key, execution_time, result_size
        );
        Ok(data)
    }

    /// Cache an areas query result
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The fetcher function fails
    /// - Cache operations fail
    /// - Serialization/deserialization fails
    pub async fn cache_areas_query<F, Fut>(
        &self,
        query_key: &str,
        params_hash: &str,
        fetcher: F,
    ) -> Result<Vec<Area>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<Vec<Area>>>,
    {
        // Check if query is already cached
        if let Some(cached) = self.areas_cache.get(query_key).await {
            if !cached.is_expired() && cached.params_hash == params_hash {
                self.record_hit();
                debug!("Query cache hit for areas: {}", query_key);
                return Ok(cached.data);
            }
        }

        // Execute the query
        let start_time = std::time::Instant::now();
        let data = fetcher().await?;
        #[allow(clippy::cast_possible_truncation)]
        let execution_time = start_time.elapsed().as_millis() as u64;

        // Check if result is too large to cache
        let result_size = Self::calculate_result_size(&data);
        if result_size > self.config.max_result_size {
            warn!("Query result too large to cache: {} bytes", result_size);
            self.record_miss();
            return Ok(data);
        }

        // Create dependencies for smart invalidation
        let dependencies = Self::create_area_dependencies(&data);

        // Create cached result
        let cached_result = CachedQueryResult {
            data: data.clone(),
            executed_at: Utc::now(),
            expires_at: Utc::now()
                + chrono::Duration::from_std(self.config.ttl).unwrap_or_default(),
            execution_time_ms: execution_time,
            params_hash: params_hash.to_string(),
            dependencies,
            result_size,
            compressed: self.config.enable_compression,
        };

        // Store in cache
        self.areas_cache
            .insert(query_key.to_string(), cached_result)
            .await;

        // Update statistics
        self.update_stats(result_size, execution_time, false);

        self.record_miss();
        debug!(
            "Cached areas query: {} ({}ms, {} bytes)",
            query_key, execution_time, result_size
        );
        Ok(data)
    }

    /// Cache a search query result
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The fetcher function fails
    /// - Cache operations fail
    /// - Serialization/deserialization fails
    pub async fn cache_search_query<F, Fut>(
        &self,
        query_key: &str,
        params_hash: &str,
        fetcher: F,
    ) -> Result<Vec<Task>>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<Vec<Task>>>,
    {
        // Check if query is already cached
        if let Some(cached) = self.search_cache.get(query_key).await {
            if !cached.is_expired() && cached.params_hash == params_hash {
                self.record_hit();
                debug!("Query cache hit for search: {}", query_key);
                return Ok(cached.data);
            }
        }

        // Execute the query
        let start_time = std::time::Instant::now();
        let data = fetcher().await?;
        #[allow(clippy::cast_possible_truncation)]
        let execution_time = start_time.elapsed().as_millis() as u64;

        // Check if result is too large to cache
        let result_size = Self::calculate_result_size(&data);
        if result_size > self.config.max_result_size {
            warn!("Query result too large to cache: {} bytes", result_size);
            self.record_miss();
            return Ok(data);
        }

        // Create dependencies for smart invalidation
        let dependencies = Self::create_task_dependencies(&data);

        // Create cached result
        let cached_result = CachedQueryResult {
            data: data.clone(),
            executed_at: Utc::now(),
            expires_at: Utc::now()
                + chrono::Duration::from_std(self.config.ttl).unwrap_or_default(),
            execution_time_ms: execution_time,
            params_hash: params_hash.to_string(),
            dependencies,
            result_size,
            compressed: self.config.enable_compression,
        };

        // Store in cache
        self.search_cache
            .insert(query_key.to_string(), cached_result)
            .await;

        // Update statistics
        self.update_stats(result_size, execution_time, false);

        self.record_miss();
        debug!(
            "Cached search query: {} ({}ms, {} bytes)",
            query_key, execution_time, result_size
        );
        Ok(data)
    }

    /// Invalidate queries by entity changes
    pub fn invalidate_by_entity(&self, entity_type: &str, entity_id: Option<&ThingsId>) {
        // Invalidate all caches for now - in a more sophisticated implementation,
        // we would check dependencies and only invalidate relevant queries
        self.tasks_cache.invalidate_all();
        self.projects_cache.invalidate_all();
        self.areas_cache.invalidate_all();
        self.search_cache.invalidate_all();

        debug!(
            "Invalidated all query caches due to entity change: {} {:?}",
            entity_type, entity_id
        );
    }

    /// Invalidate queries by operation
    pub fn invalidate_by_operation(&self, operation: &str) {
        match operation {
            "task_created" | "task_updated" | "task_deleted" | "task_completed" => {
                self.tasks_cache.invalidate_all();
                self.search_cache.invalidate_all();
            }
            "project_created" | "project_updated" | "project_deleted" => {
                self.projects_cache.invalidate_all();
                self.tasks_cache.invalidate_all(); // Tasks depend on projects
            }
            "area_created" | "area_updated" | "area_deleted" => {
                self.areas_cache.invalidate_all();
                self.projects_cache.invalidate_all(); // Projects depend on areas
                self.tasks_cache.invalidate_all(); // Tasks depend on areas
            }
            _ => {
                // For unknown operations, invalidate all caches
                self.invalidate_all();
            }
        }

        debug!("Invalidated query caches due to operation: {}", operation);
    }

    /// Invalidate all query caches
    pub fn invalidate_all(&self) {
        self.tasks_cache.invalidate_all();
        self.projects_cache.invalidate_all();
        self.areas_cache.invalidate_all();
        self.search_cache.invalidate_all();
    }

    /// Get query cache statistics
    #[must_use]
    pub fn get_stats(&self) -> QueryCacheStats {
        let mut stats = self.stats.read().clone();
        stats.calculate_hit_rate();
        stats
    }

    /// Calculate the size of a query result
    fn calculate_result_size<T>(data: &T) -> usize
    where
        T: Serialize,
    {
        // Estimate size by serializing to JSON
        serde_json::to_vec(data).map_or(0, |bytes| bytes.len())
    }

    /// Create dependencies for task data
    fn create_task_dependencies(tasks: &[Task]) -> Vec<QueryDependency> {
        let mut dependencies = Vec::new();

        // Add table dependency
        dependencies.push(QueryDependency {
            table: "TMTask".to_string(),
            entity_id: None,
            invalidating_operations: vec![
                "task_created".to_string(),
                "task_updated".to_string(),
                "task_deleted".to_string(),
                "task_completed".to_string(),
            ],
        });

        // Add specific task dependencies
        for task in tasks {
            dependencies.push(QueryDependency {
                table: "TMTask".to_string(),
                entity_id: Some(task.uuid.clone()),
                invalidating_operations: vec![
                    "task_updated".to_string(),
                    "task_deleted".to_string(),
                    "task_completed".to_string(),
                ],
            });

            // Add project dependency if task belongs to a project
            if let Some(project_uuid) = &task.project_uuid {
                dependencies.push(QueryDependency {
                    table: "TMProject".to_string(),
                    entity_id: Some(project_uuid.clone()),
                    invalidating_operations: vec![
                        "project_updated".to_string(),
                        "project_deleted".to_string(),
                    ],
                });
            }

            // Add area dependency if task belongs to an area
            if let Some(area_uuid) = &task.area_uuid {
                dependencies.push(QueryDependency {
                    table: "TMArea".to_string(),
                    entity_id: Some(area_uuid.clone()),
                    invalidating_operations: vec![
                        "area_updated".to_string(),
                        "area_deleted".to_string(),
                    ],
                });
            }
        }

        dependencies
    }

    /// Create dependencies for project data
    fn create_project_dependencies(projects: &[Project]) -> Vec<QueryDependency> {
        let mut dependencies = Vec::new();

        // Add table dependency
        dependencies.push(QueryDependency {
            table: "TMProject".to_string(),
            entity_id: None,
            invalidating_operations: vec![
                "project_created".to_string(),
                "project_updated".to_string(),
                "project_deleted".to_string(),
            ],
        });

        // Add specific project dependencies
        for project in projects {
            dependencies.push(QueryDependency {
                table: "TMProject".to_string(),
                entity_id: Some(project.uuid.clone()),
                invalidating_operations: vec![
                    "project_updated".to_string(),
                    "project_deleted".to_string(),
                ],
            });

            // Add area dependency if project belongs to an area
            if let Some(area_uuid) = &project.area_uuid {
                dependencies.push(QueryDependency {
                    table: "TMArea".to_string(),
                    entity_id: Some(area_uuid.clone()),
                    invalidating_operations: vec![
                        "area_updated".to_string(),
                        "area_deleted".to_string(),
                    ],
                });
            }
        }

        dependencies
    }

    /// Create dependencies for area data
    fn create_area_dependencies(areas: &[Area]) -> Vec<QueryDependency> {
        let mut dependencies = Vec::new();

        // Add table dependency
        dependencies.push(QueryDependency {
            table: "TMArea".to_string(),
            entity_id: None,
            invalidating_operations: vec![
                "area_created".to_string(),
                "area_updated".to_string(),
                "area_deleted".to_string(),
            ],
        });

        // Add specific area dependencies
        for area in areas {
            dependencies.push(QueryDependency {
                table: "TMArea".to_string(),
                entity_id: Some(area.uuid.clone()),
                invalidating_operations: vec![
                    "area_updated".to_string(),
                    "area_deleted".to_string(),
                ],
            });
        }

        dependencies
    }

    /// Record a cache hit
    fn record_hit(&self) {
        let mut stats = self.stats.write();
        stats.hits += 1;
    }

    /// Record a cache miss
    fn record_miss(&self) {
        let mut stats = self.stats.write();
        stats.misses += 1;
    }

    /// Update cache statistics
    #[allow(clippy::cast_precision_loss)]
    fn update_stats(&self, result_size: usize, execution_time_ms: u64, compressed: bool) {
        let mut stats = self.stats.write();
        stats.total_queries += 1;
        stats.total_size_bytes += result_size as u64;

        // Update average execution time
        let total_queries = stats.total_queries as f64;
        let current_avg = stats.average_execution_time_ms;
        stats.average_execution_time_ms =
            (current_avg * (total_queries - 1.0) + execution_time_ms as f64) / total_queries;

        if compressed {
            stats.compressed_queries += 1;
        } else {
            stats.uncompressed_queries += 1;
        }
    }
}

impl<T> CachedQueryResult<T> {
    /// Check if the cached result is expired
    pub fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::TaskStatus;
    use crate::test_utils::create_mock_tasks;

    #[tokio::test]
    async fn test_query_cache_basic_operations() {
        let cache = QueryCache::new_default();

        // Test caching a tasks query
        let tasks = create_mock_tasks();
        let query_key = "test_tasks_query";
        let params_hash = "test_params_hash";

        let result = cache
            .cache_tasks_query(query_key, params_hash, || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        assert_eq!(result.len(), tasks.len());

        // Test cache hit
        let cached_result = cache
            .cache_tasks_query(query_key, params_hash, || async {
                panic!("Should not execute fetcher on cache hit");
            })
            .await
            .unwrap();

        assert_eq!(cached_result.len(), tasks.len());

        // Test cache miss with different params
        let different_params = "different_params_hash";
        let _ = cache
            .cache_tasks_query(query_key, different_params, || async {
                Ok(create_mock_tasks())
            })
            .await
            .unwrap();

        let stats = cache.get_stats();
        assert!(stats.hits >= 1);
        assert!(stats.misses >= 1);
    }

    #[tokio::test]
    async fn test_query_cache_invalidation() {
        let cache = QueryCache::new_default();

        // Cache some data
        let tasks = create_mock_tasks();
        cache
            .cache_tasks_query("test_query", "params", || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        // Invalidate by operation
        cache.invalidate_by_operation("task_updated");

        // Should be a cache miss now
        let _ = cache
            .cache_tasks_query("test_query", "params", || async { Ok(create_mock_tasks()) })
            .await
            .unwrap();

        let stats = cache.get_stats();
        assert!(stats.misses >= 2);
    }

    #[tokio::test]
    async fn test_query_cache_dependencies() {
        let _cache = QueryCache::new_default();

        let tasks = create_mock_tasks();
        let dependencies = QueryCache::create_task_dependencies(&tasks);

        assert!(!dependencies.is_empty());
        assert!(dependencies.iter().any(|dep| dep.table == "TMTask"));
    }

    #[tokio::test]
    async fn test_query_cache_projects_query() {
        let cache = QueryCache::new_default();

        let projects = vec![Project {
            uuid: ThingsId::new_v4(),
            title: "Project 1".to_string(),
            area_uuid: Some(ThingsId::new_v4()),
            created: Utc::now(),
            modified: Utc::now(),
            status: TaskStatus::Incomplete,
            notes: Some("Notes".to_string()),
            deadline: None,
            start_date: None,
            tags: vec![],
            tasks: vec![],
        }];

        let query_key = "test_projects_query";
        let params_hash = "test_params";

        // Test cache miss
        let result = cache
            .cache_projects_query(query_key, params_hash, || async { Ok(projects.clone()) })
            .await
            .unwrap();

        assert_eq!(result.len(), projects.len());

        // Test cache hit
        let cached_result = cache
            .cache_projects_query(query_key, params_hash, || async {
                panic!("Should not execute fetcher on cache hit");
            })
            .await
            .unwrap();

        assert_eq!(cached_result.len(), projects.len());
    }

    #[tokio::test]
    async fn test_query_cache_config_default() {
        let config = QueryCacheConfig::default();
        assert_eq!(config.max_queries, 1000);
        assert_eq!(config.ttl, Duration::from_secs(1800));
        assert_eq!(config.tti, Duration::from_secs(300));
        assert!(config.enable_compression);
        assert_eq!(config.max_result_size, 1024 * 1024);
    }

    #[tokio::test]
    async fn test_cached_query_result_creation() {
        let tasks = create_mock_tasks();
        let now = Utc::now();
        let expires_at = now + chrono::Duration::seconds(1800);

        let dependency = QueryDependency {
            table: "TMTask".to_string(),
            entity_id: None,
            invalidating_operations: vec![
                "INSERT".to_string(),
                "UPDATE".to_string(),
                "DELETE".to_string(),
            ],
        };

        let result = CachedQueryResult {
            data: tasks.clone(),
            executed_at: now,
            expires_at,
            execution_time_ms: 100,
            params_hash: "test_hash".to_string(),
            result_size: 1024,
            dependencies: vec![dependency.clone()],
            compressed: false,
        };

        assert_eq!(result.data.len(), tasks.len());
        assert_eq!(result.execution_time_ms, 100);
        assert_eq!(result.result_size, 1024);
        assert_eq!(result.params_hash, "test_hash");
        assert_eq!(result.dependencies, vec![dependency]);
        assert!(!result.compressed);
    }

    #[tokio::test]
    async fn test_query_cache_areas_query() {
        let cache = QueryCache::new_default();

        let areas = vec![Area {
            uuid: ThingsId::new_v4(),
            title: "Area 1".to_string(),
            created: Utc::now(),
            modified: Utc::now(),
            notes: Some("Notes".to_string()),
            tags: vec![],
            projects: vec![],
        }];

        let query_key = "test_areas_query";
        let params_hash = "test_params";

        // Test cache miss
        let result = cache
            .cache_areas_query(query_key, params_hash, || async { Ok(areas.clone()) })
            .await
            .unwrap();

        assert_eq!(result.len(), areas.len());

        // Test cache hit
        let cached_result = cache
            .cache_areas_query(query_key, params_hash, || async {
                panic!("Should not execute fetcher on cache hit");
            })
            .await
            .unwrap();

        assert_eq!(cached_result.len(), areas.len());
    }

    #[tokio::test]
    async fn test_query_cache_expiration() {
        let config = QueryCacheConfig {
            max_queries: 100,
            ttl: Duration::from_millis(10), // Very short TTL for testing
            tti: Duration::from_millis(5),
            enable_compression: false,
            max_result_size: 1024,
        };
        let cache = QueryCache::new(config);

        let tasks = create_mock_tasks();
        let query_key = "test_expiration";
        let params_hash = "test_params";

        // Cache a query
        let _result = cache
            .cache_tasks_query(query_key, params_hash, || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        // Wait for expiration
        tokio::time::sleep(Duration::from_millis(20)).await;

        // Should execute fetcher again due to expiration
        let mut fetcher_called = false;
        let _expired_result = cache
            .cache_tasks_query(query_key, params_hash, || async {
                fetcher_called = true;
                Ok(tasks.clone())
            })
            .await
            .unwrap();

        assert!(fetcher_called);
    }

    #[tokio::test]
    async fn test_query_cache_size_limit() {
        let config = QueryCacheConfig {
            max_queries: 2, // Very small limit for testing
            ttl: Duration::from_secs(300),
            tti: Duration::from_secs(60),
            enable_compression: false,
            max_result_size: 1024,
        };
        let cache = QueryCache::new(config);

        let tasks = create_mock_tasks();

        // Cache multiple queries
        let _result1 = cache
            .cache_tasks_query("key1", "params1", || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        let _result2 = cache
            .cache_tasks_query("key2", "params2", || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        // This should evict one of the previous entries
        let _result3 = cache
            .cache_tasks_query("key3", "params3", || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        // Verify cache size is respected - the cache may have evicted entries
        // so we just check that it doesn't exceed the max capacity significantly
        let stats = cache.get_stats();
        // The cache should not have significantly more than the configured max
        assert!(stats.total_queries <= 10); // Allow some flexibility for the cache implementation
    }

    #[tokio::test]
    async fn test_query_cache_concurrent_access() {
        let cache = Arc::new(QueryCache::new_default());
        let tasks = create_mock_tasks();

        // Spawn multiple tasks to access cache concurrently
        let mut handles = vec![];

        for i in 0..10 {
            let cache_clone = cache.clone();
            let tasks_clone = tasks.clone();
            let handle = tokio::spawn(async move {
                let key = format!("concurrent_key_{i}");
                let params = format!("params_{i}");
                let result = cache_clone
                    .cache_tasks_query(&key, &params, || async { Ok(tasks_clone.clone()) })
                    .await
                    .unwrap();
                assert!(!result.is_empty());
            });
            handles.push(handle);
        }

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }
    }

    #[tokio::test]
    async fn test_query_cache_error_handling() {
        let cache = QueryCache::new_default();

        let query_key = "error_test";
        let params_hash = "test_params";

        // Test error handling
        let result = cache
            .cache_tasks_query(query_key, params_hash, || async {
                Err(anyhow::anyhow!("Test error"))
            })
            .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_query_cache_compression() {
        let config = QueryCacheConfig {
            max_queries: 100,
            ttl: Duration::from_secs(300),
            tti: Duration::from_secs(60),
            enable_compression: true,
            max_result_size: 1024 * 1024,
        };
        let cache = QueryCache::new(config);

        let tasks = create_mock_tasks();
        let query_key = "compression_test";
        let params_hash = "test_params";

        // Cache with compression enabled
        let result = cache
            .cache_tasks_query(query_key, params_hash, || async { Ok(tasks.clone()) })
            .await
            .unwrap();

        assert_eq!(result.len(), tasks.len());

        // Verify cache hit works with compression
        let cached_result = cache
            .cache_tasks_query(query_key, params_hash, || async {
                panic!("Should not execute fetcher on cache hit");
            })
            .await
            .unwrap();

        assert_eq!(cached_result.len(), tasks.len());
    }
}