@@ -19,69 +19,69 @@ def demonstrate_cache_timestamps():
19
19
"""Demonstrate accessing cache timestamp information."""
20
20
print ("Cache Timestamp Information Demo" )
21
21
print ("=" * 40 )
22
-
22
+
23
23
# Create a repository with a cache backend
24
24
cache = EphemeralCache (max_keys = 100 )
25
- repo = Repository (working_dir = GIT_PANDAS_DIR , cache_backend = cache , default_branch = ' master' )
26
-
25
+ repo = Repository (working_dir = GIT_PANDAS_DIR , cache_backend = cache , default_branch = " master" )
26
+
27
27
print (f"Repository: { repo .repo_name } " )
28
28
print (f"Cache backend: { type (cache ).__name__ } " )
29
29
print ()
30
-
30
+
31
31
# Call some methods to populate the cache
32
32
print ("Populating cache with repository data..." )
33
-
33
+
34
34
print (" - Getting commit history..." )
35
- commit_history = repo .commit_history (limit = 10 )
36
-
35
+ repo .commit_history (limit = 10 )
36
+
37
37
print (" - Getting file list..." )
38
- files = repo .list_files ()
39
-
38
+ repo .list_files ()
39
+
40
40
print (" - Getting blame information..." )
41
- blame = repo .blame ()
42
-
41
+ repo .blame ()
42
+
43
43
print (f"Cache now contains { len (cache ._cache )} entries" )
44
44
print ()
45
-
45
+
46
46
# Show cache information
47
47
print ("Cache Contents and Timestamps:" )
48
48
print ("-" * 40 )
49
-
49
+
50
50
cached_keys = cache .list_cached_keys ()
51
51
for entry in cached_keys :
52
52
print (f"Key: { entry ['key' ]} " )
53
53
print (f" Cached at: { entry ['cached_at' ].strftime ('%Y-%m-%d %H:%M:%S UTC' )} " )
54
54
print (f" Age: { entry ['age_seconds' ]:.1f} seconds" )
55
55
print ()
56
-
56
+
57
57
# Wait a moment and call one method again
58
58
print ("Waiting 2 seconds and refreshing commit history..." )
59
59
time .sleep (2 )
60
-
60
+
61
61
# This should hit the cache
62
- commit_history_cached = repo .commit_history (limit = 10 )
63
-
62
+ repo .commit_history (limit = 10 )
63
+
64
64
# This should create a new cache entry
65
- commit_history_fresh = repo .commit_history (limit = 20 )
66
-
65
+ repo .commit_history (limit = 20 )
66
+
67
67
print ("\n Updated cache contents:" )
68
68
print ("-" * 40 )
69
-
69
+
70
70
cached_keys = cache .list_cached_keys ()
71
71
for entry in cached_keys :
72
72
print (f"Key: { entry ['key' ]} " )
73
73
print (f" Cached at: { entry ['cached_at' ].strftime ('%Y-%m-%d %H:%M:%S UTC' )} " )
74
74
print (f" Age: { entry ['age_seconds' ]:.1f} seconds" )
75
75
print ()
76
-
76
+
77
77
# Demonstrate getting specific cache info
78
78
print ("Getting specific cache information:" )
79
79
print ("-" * 40 )
80
-
80
+
81
81
# Find a commit_history cache key
82
- commit_keys = [k for k in cached_keys if ' commit_history' in k [' key' ]]
82
+ commit_keys = [k for k in cached_keys if " commit_history" in k [" key" ]]
83
83
if commit_keys :
84
- key = commit_keys [0 ][' key' ]
84
+ key = commit_keys [0 ][" key" ]
85
85
info = cache .get_cache_info (key )
86
86
if info :
87
87
print (f"Cache info for key '{ key } ':" )
@@ -95,44 +95,43 @@ def demonstrate_disk_cache_persistence():
95
95
print ("\n " + "=" * 50 )
96
96
print ("Disk Cache Persistence Demo" )
97
97
print ("=" * 50 )
98
-
98
+
99
99
cache_file = "/tmp/gitpandas_demo_cache.gz"
100
-
100
+
101
101
# Clean up any existing cache file
102
102
if os .path .exists (cache_file ):
103
103
os .remove (cache_file )
104
-
104
+
105
105
print ("Creating repository with DiskCache..." )
106
106
cache = DiskCache (filepath = cache_file , max_keys = 50 )
107
- repo = Repository (working_dir = GIT_PANDAS_DIR , cache_backend = cache , default_branch = ' master' )
108
-
107
+ repo = Repository (working_dir = GIT_PANDAS_DIR , cache_backend = cache , default_branch = " master" )
108
+
109
109
# Populate cache
110
110
print ("Populating cache..." )
111
- commit_history = repo .commit_history (limit = 5 )
112
- files = repo .list_files ()
113
-
111
+ repo .commit_history (limit = 5 )
112
+ repo .list_files ()
113
+
114
114
print (f"Cache file created: { cache_file } " )
115
115
print (f"Cache contains { len (cache ._cache )} entries" )
116
-
116
+
117
117
# Show initial cache info
118
118
cached_keys = cache .list_cached_keys ()
119
119
print ("\n Initial cache entries:" )
120
120
for entry in cached_keys :
121
121
print (f" { entry ['key' ]} : { entry ['cached_at' ].strftime ('%H:%M:%S' )} " )
122
-
122
+
123
123
# Create a new cache instance from the same file
124
124
print ("\n Creating new cache instance from saved file..." )
125
125
cache2 = DiskCache (filepath = cache_file , max_keys = 50 )
126
-
126
+
127
127
print (f"Loaded cache contains { len (cache2 ._cache )} entries" )
128
-
128
+
129
129
# Show loaded cache info
130
130
cached_keys2 = cache2 .list_cached_keys ()
131
131
print ("\n Loaded cache entries:" )
132
132
for entry in cached_keys2 :
133
- print (f" { entry ['key' ]} : { entry ['cached_at' ].strftime ('%H:%M:%S' )} "
134
- f"(age: { entry ['age_seconds' ]:.1f} s)" )
135
-
133
+ print (f" { entry ['key' ]} : { entry ['cached_at' ].strftime ('%H:%M:%S' )} (age: { entry ['age_seconds' ]:.1f} s)" )
134
+
136
135
# Clean up
137
136
if os .path .exists (cache_file ):
138
137
os .remove (cache_file )
@@ -144,29 +143,29 @@ def demonstrate_cache_with_force_refresh():
144
143
print ("\n " + "=" * 50 )
145
144
print ("Force Refresh Demo" )
146
145
print ("=" * 50 )
147
-
146
+
148
147
cache = EphemeralCache (max_keys = 10 )
149
- repo = Repository (working_dir = GIT_PANDAS_DIR , cache_backend = cache , default_branch = ' master' )
150
-
148
+ repo = Repository (working_dir = GIT_PANDAS_DIR , cache_backend = cache , default_branch = " master" )
149
+
151
150
print ("Getting commit history (first time)..." )
152
151
start_time = datetime .now (timezone .utc )
153
- commit_history1 = repo .commit_history (limit = 5 )
154
-
152
+ repo .commit_history (limit = 5 )
153
+
155
154
time .sleep (1 )
156
-
155
+
157
156
print ("Getting commit history (should use cache)..." )
158
- commit_history2 = repo .commit_history (limit = 5 )
159
-
157
+ repo .commit_history (limit = 5 )
158
+
160
159
time .sleep (1 )
161
-
160
+
162
161
print ("Getting commit history with force_refresh=True..." )
163
- commit_history3 = repo .commit_history (limit = 5 , force_refresh = True )
164
-
162
+ repo .commit_history (limit = 5 , force_refresh = True )
163
+
165
164
print ("\n Cache timeline:" )
166
165
cached_keys = cache .list_cached_keys ()
167
166
for entry in cached_keys :
168
- if ' commit_history' in entry [' key' ]:
169
- age_from_start = (entry [' cached_at' ] - start_time ).total_seconds ()
167
+ if " commit_history" in entry [" key" ]:
168
+ age_from_start = (entry [" cached_at" ] - start_time ).total_seconds ()
170
169
print (f" Commit history cached at: +{ age_from_start :.1f} s from start" )
171
170
print (f" Current age: { entry ['age_seconds' ]:.1f} s" )
172
171
@@ -176,15 +175,15 @@ def demonstrate_cache_with_force_refresh():
176
175
demonstrate_cache_timestamps ()
177
176
demonstrate_disk_cache_persistence ()
178
177
demonstrate_cache_with_force_refresh ()
179
-
178
+
180
179
print ("\n " + "=" * 50 )
181
180
print ("Summary:" )
182
181
print ("- Cache backends now track when entries were created" )
183
182
print ("- No changes to Repository or ProjectDirectory API" )
184
183
print ("- Users can access cache info via cache_backend.get_cache_info()" )
185
184
print ("- Users can list all cached keys via cache_backend.list_cached_keys()" )
186
185
print ("- Backward compatibility maintained with existing caches" )
187
-
186
+
188
187
except Exception as e :
189
188
print (f"Error running demo: { e } " )
190
- print ("Make sure you're running this from the git-pandas directory" )
189
+ print ("Make sure you're running this from the git-pandas directory" )
0 commit comments