My agentic slop goes here. Not intended for anyone else!
1(** Example usage of the CacheIO library *)
2
3open Eio
4
5let setup_logs () =
6 Logs.set_level (Some Logs.Info);
7 Logs.set_reporter (Logs_fmt.reporter ())
8
9(** Example: Using CacheIO for caching data *)
10let cache_example env sw =
11 Printf.printf "CacheIO Example\n";
12 Printf.printf "===============\n\n";
13
14 (* Create cache in temp directory *)
15 let base_dir = Path.(env#fs / "/tmp/cacheio-example") in
16 let cache = Cacheio.create ~base_dir in
17
18 (* Example 1: Cache some text data *)
19 Printf.printf "1. Caching text data...\n";
20 let key1 = "document.txt" in
21 let data1 = "This is an example document that we're caching." in
22 let source1 = Flow.string_source data1 in
23 Cacheio.put cache ~key:key1 ~source:source1 ~ttl:(Some 3600.) ();
24 Printf.printf " Cached '%s'\n" key1;
25
26 (* Example 2: Cache with no expiry *)
27 let key2 = "permanent-data.txt" in
28 let data2 = "This data never expires" in
29 let source2 = Flow.string_source data2 in
30 Cacheio.put cache ~key:key2 ~source:source2 ();
31 Printf.printf " Cached '%s' (no expiry)\n" key2;
32
33 (* Example 3: Retrieve cached data *)
34 Printf.printf "\n2. Retrieving cached data...\n";
35 (match Cacheio.get cache ~key:key1 ~sw with
36 | Some source ->
37 let buf = Buffer.create 100 in
38 let cstruct = Cstruct.create 100 in
39 let rec read () =
40 try
41 match Flow.single_read source cstruct with
42 | 0 -> ()
43 | n ->
44 Buffer.add_string buf (Cstruct.to_string (Cstruct.sub cstruct 0 n));
45 read ()
46 with End_of_file -> ()
47 in
48 read ();
49 Printf.printf " Retrieved: '%s'\n" (Buffer.contents buf)
50 | None ->
51 Printf.printf " Key not found or expired\n");
52
53 (* Example 4: Working with flags *)
54 Printf.printf "\n3. Working with flags...\n";
55
56 (* Pin an important file *)
57 Cacheio.add_flag cache ~key:key2 `Pinned;
58 Printf.printf " Pinned '%s'\n" key2;
59
60 (* Mark a file as stale *)
61 Cacheio.add_flag cache ~key:key1 `Stale;
62 Printf.printf " Marked '%s' as stale\n" key1;
63
64 (* Example 5: Cache statistics *)
65 Printf.printf "\n4. Cache statistics:\n";
66 let stats = Cacheio.stats cache in
67 let open Cacheio.Stats in
68 Printf.printf " Total entries: %d\n" (entry_count stats);
69 Printf.printf " Total size: %Ld bytes\n" (total_size stats);
70 Printf.printf " Expired entries: %d\n" (expired_count stats);
71 Printf.printf " Pinned entries: %d\n" (pinned_count stats);
72 Printf.printf " Stale entries: %d\n" (stale_count stats);
73
74 (* Example 6: Scan cache *)
75 Printf.printf "\n5. Cache contents:\n";
76 let entries = Cacheio.scan cache in
77 List.iter (fun entry ->
78 let flags_str = Format.asprintf "%a" Cacheio.Flags.pp (Cacheio.Entry.flags entry) in
79 let ttl_str = match Cacheio.Entry.ttl entry with
80 | None -> "never"
81 | Some t ->
82 let remaining = t -. Unix.time () in
83 if remaining > 0. then Printf.sprintf "%.0fs" remaining
84 else "expired"
85 in
86 Printf.printf " %s: %Ld bytes, expires: %s, flags: %s\n"
87 (String.sub (Cacheio.Entry.key entry) 0 16) (Cacheio.Entry.size entry) ttl_str flags_str
88 ) entries;
89
90 (* Example 7: Expire old entries *)
91 Printf.printf "\n6. Expiring old entries...\n";
92 let expired_count = Cacheio.expire cache in
93 Printf.printf " Removed %d expired entries\n" expired_count;
94
95 (* Example 8: Clear temporary entries *)
96 Printf.printf "\n7. Clear temporary entries...\n";
97 Cacheio.clear_temporary cache;
98 Printf.printf " Cleared all temporary entries\n"
99
100let () =
101 setup_logs ();
102 Eio_main.run @@ fun env ->
103 Switch.run @@ fun sw ->
104 cache_example env sw;
105 Printf.printf "\nExample completed successfully!\n"