(** Example usage of the CacheIO library *) open Eio let setup_logs () = Logs.set_level (Some Logs.Info); Logs.set_reporter (Logs_fmt.reporter ()) (** Example: Using CacheIO for caching data *) let cache_example env sw = Printf.printf "CacheIO Example\n"; Printf.printf "===============\n\n"; (* Create cache in temp directory *) let base_dir = Path.(env#fs / "/tmp/cacheio-example") in let cache = Cacheio.create ~base_dir in (* Example 1: Cache some text data *) Printf.printf "1. Caching text data...\n"; let key1 = "document.txt" in let data1 = "This is an example document that we're caching." in let source1 = Flow.string_source data1 in Cacheio.put cache ~key:key1 ~source:source1 ~ttl:(Some 3600.) (); Printf.printf " Cached '%s'\n" key1; (* Example 2: Cache with no expiry *) let key2 = "permanent-data.txt" in let data2 = "This data never expires" in let source2 = Flow.string_source data2 in Cacheio.put cache ~key:key2 ~source:source2 (); Printf.printf " Cached '%s' (no expiry)\n" key2; (* Example 3: Retrieve cached data *) Printf.printf "\n2. Retrieving cached data...\n"; (match Cacheio.get cache ~key:key1 ~sw with | Some source -> let buf = Buffer.create 100 in let cstruct = Cstruct.create 100 in let rec read () = try match Flow.single_read source cstruct with | 0 -> () | n -> Buffer.add_string buf (Cstruct.to_string (Cstruct.sub cstruct 0 n)); read () with End_of_file -> () in read (); Printf.printf " Retrieved: '%s'\n" (Buffer.contents buf) | None -> Printf.printf " Key not found or expired\n"); (* Example 4: Working with flags *) Printf.printf "\n3. Working with flags...\n"; (* Pin an important file *) Cacheio.add_flag cache ~key:key2 `Pinned; Printf.printf " Pinned '%s'\n" key2; (* Mark a file as stale *) Cacheio.add_flag cache ~key:key1 `Stale; Printf.printf " Marked '%s' as stale\n" key1; (* Example 5: Cache statistics *) Printf.printf "\n4. Cache statistics:\n"; let stats = Cacheio.stats cache in let open Cacheio.Stats in Printf.printf " Total entries: %d\n" (entry_count stats); Printf.printf " Total size: %Ld bytes\n" (total_size stats); Printf.printf " Expired entries: %d\n" (expired_count stats); Printf.printf " Pinned entries: %d\n" (pinned_count stats); Printf.printf " Stale entries: %d\n" (stale_count stats); (* Example 6: Scan cache *) Printf.printf "\n5. Cache contents:\n"; let entries = Cacheio.scan cache in List.iter (fun entry -> let flags_str = Format.asprintf "%a" Cacheio.Flags.pp (Cacheio.Entry.flags entry) in let ttl_str = match Cacheio.Entry.ttl entry with | None -> "never" | Some t -> let remaining = t -. Unix.time () in if remaining > 0. then Printf.sprintf "%.0fs" remaining else "expired" in Printf.printf " %s: %Ld bytes, expires: %s, flags: %s\n" (String.sub (Cacheio.Entry.key entry) 0 16) (Cacheio.Entry.size entry) ttl_str flags_str ) entries; (* Example 7: Expire old entries *) Printf.printf "\n6. Expiring old entries...\n"; let expired_count = Cacheio.expire cache in Printf.printf " Removed %d expired entries\n" expired_count; (* Example 8: Clear temporary entries *) Printf.printf "\n7. Clear temporary entries...\n"; Cacheio.clear_temporary cache; Printf.printf " Cleared all temporary entries\n" let () = setup_logs (); Eio_main.run @@ fun env -> Switch.run @@ fun sw -> cache_example env sw; Printf.printf "\nExample completed successfully!\n"