Fortran · 2822 bytes Raw Blame History
1 module cache_module
2 use types_module
3 implicit none
4
5 ! File cache type for storing git command results
6 type :: file_cache
7 real(8) :: timestamp
8 type(file_entry), allocatable :: files(:)
9 integer :: n_files
10 logical :: valid
11 end type
12
13 ! Global caches for dirty files and all files
14 type(file_cache) :: dirty_cache, all_cache
15
16 ! Cache time-to-live (500ms = 0.5 seconds)
17 ! After this time, cache is considered stale
18 real(8), parameter :: CACHE_TTL = 0.5d0
19
20 contains
21
22 ! Check if a cache entry is still valid
23 function cache_valid(cache) result(valid)
24 type(file_cache), intent(in) :: cache
25 logical :: valid
26 real(8) :: current_time
27
28 if (.not. cache%valid) then
29 valid = .false.
30 return
31 end if
32
33 call cpu_time(current_time)
34 valid = (current_time - cache%timestamp) < CACHE_TTL
35 end function cache_valid
36
37 ! Invalidate a cache (mark as stale)
38 subroutine invalidate_cache(cache)
39 type(file_cache), intent(inout) :: cache
40 cache%valid = .false.
41 end subroutine invalidate_cache
42
43 ! Invalidate all caches (called after state-changing operations)
44 subroutine invalidate_all_caches()
45 call invalidate_cache(dirty_cache)
46 call invalidate_cache(all_cache)
47 end subroutine invalidate_all_caches
48
49 ! Update cache with new data
50 subroutine update_cache(cache, files, n_files)
51 type(file_cache), intent(inout) :: cache
52 type(file_entry), intent(in) :: files(:)
53 integer, intent(in) :: n_files
54 integer :: i
55
56 ! Free old data
57 if (allocated(cache%files)) deallocate(cache%files)
58
59 ! Allocate and copy new data
60 allocate(cache%files(n_files))
61 do i = 1, n_files
62 cache%files(i) = files(i)
63 end do
64
65 cache%n_files = n_files
66 call cpu_time(cache%timestamp)
67 cache%valid = .true.
68 end subroutine update_cache
69
70 ! Retrieve cached data
71 subroutine get_cached_files(cache, files, n_files)
72 type(file_cache), intent(in) :: cache
73 type(file_entry), allocatable, intent(out) :: files(:)
74 integer, intent(out) :: n_files
75 integer :: i
76
77 n_files = cache%n_files
78
79 if (allocated(files)) deallocate(files)
80 allocate(files(n_files))
81
82 do i = 1, n_files
83 files(i) = cache%files(i)
84 end do
85 end subroutine get_cached_files
86
87 ! Initialize caches (call at program start)
88 subroutine init_caches()
89 dirty_cache%valid = .false.
90 dirty_cache%timestamp = 0.0d0
91 dirty_cache%n_files = 0
92
93 all_cache%valid = .false.
94 all_cache%timestamp = 0.0d0
95 all_cache%n_files = 0
96 end subroutine init_caches
97
98 end module cache_module
99