Cache Assets — asset management within a cache

DataCaches.CacheAssets is a submodule (public, not exported) that provides a filesystem-style interface for managing individual assets (entries) within a DataCache. Import it as using DataCaches.CacheAssets or access functions as DataCaches.CacheAssets.<function>.

All functions accept an optional leading DataCache argument. When omitted, active_autocache() is used.

List assets

ls returns a Vector{CacheEntry} for programmatic use; ls! prints a formatted listing to an IO stream and returns nothing. Both accept the same filtering and sorting keyword arguments.

Note

entries (exported at the top level) provides the same data-retrieval functionality without importing CacheAssets. Use CacheAssets.ls! when you want formatted output.

using DataCaches.CacheAssets

# --- Data form: returns Vector{CacheEntry} ---

entries = CacheAssets.ls(dc)

# Filter by label/description pattern
entries = CacheAssets.ls(dc; pattern = r"canidae")
entries = CacheAssets.ls(dc; pattern = "dino")          # string is converted to Regex

# Filter by file path (full path or filename only)
entries = CacheAssets.ls(dc; filepath_pattern = r"/project/caches/")
entries = CacheAssets.ls(dc; filepath_pattern = "/scratch")    # string → Regex
entries = CacheAssets.ls(dc; filename_pattern = r"^abc1234")   # UUID-prefix match
entries = CacheAssets.ls(dc; filename_pattern = r"\.csv$")     # CSV-backed entries only

# Filter by write or access date
entries = CacheAssets.ls(dc; after  = DateTime("2026-01-01T00:00:00"))
entries = CacheAssets.ls(dc; before = DateTime("2026-06-01T00:00:00"))
entries = CacheAssets.ls(dc; accessed_after_date  = DateTime("2026-03-01T00:00:00"))
entries = CacheAssets.ls(dc; accessed_before_date = DateTime("2026-06-01T00:00:00"))

# Filter by whether the entry has a label
entries = CacheAssets.ls(dc; labeled = true)   # only labeled entries
entries = CacheAssets.ls(dc; labeled = false)  # only unlabeled entries

# Filter by format tag
entries = CacheAssets.ls(dc; format = "jls")        # exact match
entries = CacheAssets.ls(dc; format = r"csv|json")  # Regex match

# Sort
entries = CacheAssets.ls(dc; sortby = :date_desc)          # newest first
entries = CacheAssets.ls(dc; sortby = :dateaccessed_desc)  # least recently used last
entries = CacheAssets.ls(dc; sortby = :size_desc)          # largest first (requires stat)
entries = CacheAssets.ls(dc; sortby = :label)

# Use autocache store (active_autocache())
entries = CacheAssets.ls()
entries = CacheAssets.ls(; sortby = :date_desc)

# --- Display form: prints to io, returns nothing ---

# Default: normal detail level, sorted by sequence index
CacheAssets.ls!(dc)

# Terse — just labels
CacheAssets.ls!(dc; detail = :minimal)

# Full — includes last-access time, file size, and data format
CacheAssets.ls!(dc; detail = :full)

# All ls filter/sort kwargs are also accepted by ls!
CacheAssets.ls!(dc; pattern = r"canidae", sortby = :date_desc)
CacheAssets.ls!(dc; filename_pattern = r"\.csv$", detail = :full)

# Redirect output
CacheAssets.ls!(dc; io = my_io)

# Use default cache
CacheAssets.ls!()
CacheAssets.ls!(; detail = :full, sortby = :date_desc)

Remove assets

# Remove by label, sequence index, UUID prefix, or CacheEntry — in any combination
CacheAssets.rm(dc, "canidae_occs")
CacheAssets.rm(dc, 3)
CacheAssets.rm(dc, "canidae_occs", "dinosaur_taxa", 5)

# Pass a vector of specifiers (any mix of CacheEntry, label String, seq Integer)
stale = CacheAssets.ls(dc; before = DateTime("2026-01-01"))
CacheAssets.rm(dc, stale)                              # remove all at once (one index rewrite)
CacheAssets.rm(dc, [entry1, "old_label", 7])           # mixed-type vector

# Suppress errors for missing specifiers
CacheAssets.rm(dc, "maybe_exists"; force = true)
CacheAssets.rm(dc, ["maybe1", "maybe2"]; force = true)

# Default cache
CacheAssets.rm("old_entry")
CacheAssets.rm(stale)

Relabel and move assets

# Relabel within the same cache (dest is a String)
CacheAssets.mv(dc, "old_label", "new_label")
CacheAssets.mv(dc, 2, "new_label")             # by sequence index

# Relabel with conflict resolution
CacheAssets.mv(dc, "old", "taken_label"; force = true)  # replaces the conflicting entry

# Move to another cache (dest is a DataCache)
dc2 = DataCache(:archive)
CacheAssets.mv(dc, "canidae_occs", dc2)
CacheAssets.mv(dc, "canidae_occs", dc2; label = "renamed_in_dest")
CacheAssets.mv(dc, "canidae_occs", dc2; force = true)   # overwrite if label exists in dc2

# Default-cache forms
CacheAssets.mv("old_label", "new_label")
CacheAssets.mv("entry", dc2)

Copy assets to another cache

# Single asset
CacheAssets.cp(dc, "canidae_occs", dc2)
CacheAssets.cp(dc, "canidae_occs", dc2; label = "canidae_copy")

# Multiple assets (vector of specifiers)
CacheAssets.cp(dc, ["canidae_occs", "dinosaur_taxa", 5], dc2)
CacheAssets.cp(dc, ["canidae_occs", "dinosaur_taxa"], dc2; force = true)

# Default-cache form
CacheAssets.cp("canidae_occs", dc2)
CacheAssets.cp(["entry1", "entry2"], dc2)

Each copy receives a new UUID, sequence number, and datecached timestamp in the destination. Copying within the same cache is allowed and produces a distinct new entry.

Access-time tracking and LRU support

Every DataCache records when each asset was last read via the dateaccessed field on each CacheEntry. This is enabled by default; disable it for caches that are read at very high frequency or have many entries, since every read triggers a full index rewrite:

dc = DataCache(:large_cache; track_access = false)

CacheAssets.ls(dc; detail = :full) shows both datecached and dateaccessed for each entry. Sort by :dateaccessed or :dateaccessed_desc to find least-recently-used assets.

Quick-reference table

FunctionDescription
CacheAssets.ls([dc]; pattern, filepath_pattern, filename_pattern, format, before, after, accessed_before_date, accessed_after_date, labeled, sortby, rev)List assets in a cache
CacheAssets.ls!([dc]; detail, …same filters…, io)Print formatted listing to io
CacheAssets.rm([dc,] assets...; force)Remove assets by varargs (batched index rewrite)
CacheAssets.rm([dc,] specs::Vector; force)Remove assets by vector (batched index rewrite)
delete!(dc, specs::Vector)Remove assets by vector from DataCache directly
CacheAssets.mv([dc,] src, new_label; force)Relabel an asset within a cache
CacheAssets.mv([src_dc,] src, dest_dc; label, force)Move asset to another cache
CacheAssets.cp([src_dc,] src, dest_dc; label, force)Copy asset to another cache
CacheAssets.cp([src_dc,] srcs::Vector, dest_dc; force)Copy multiple assets to another cache
CacheAssets.purge!([dc]; max_age, max_idle, keep_count, max_size_bytes, stale, keep_labeled, dry_run, …ls filters…)Bulk-delete entries by age, idle time, count, or size (LRU)

API reference

DataCaches.CacheAssets.cp — Method
DataCaches.CacheAssets.cp([src_cache::DataCache,] src, dest_cache::DataCache; kwargs...)
DataCaches.CacheAssets.cp([src_cache::DataCache,] srcs::AbstractVector, dest_cache::DataCache; kwargs...)

Copy one or more assets to dest_cache. Each copy receives a new UUID, sequence number, and datecached timestamp in the destination. Copying within the same cache is allowed and produces a distinct new entry.

src / each element of srcs can be a CacheEntry, label String, UUID-prefix String, or sequence index Integer.

Single-source form:

  • label="" — override the destination label (default: preserve original label)
  • force=false — when true, silently replace an existing entry with the same label

Multi-source form (srcs::AbstractVector):

  • Each entry preserves its original label; returns a Vector{CacheEntry} of new entries
  • force=false — apply to all entries

If src_cache is omitted, active_autocache() is used as the source.

source
DataCaches.CacheAssets.ls! — Method
DataCaches.CacheAssets.ls!([cache::DataCache]; kwargs...) → nothing

Print a formatted listing of assets in cache to io (default stdout). If cache is omitted, the active default cache is used (see active_autocache()).

Accepts all the same filtering and sorting keyword arguments as ls, plus:

Additional keyword arguments

Display:

  • detail::Symbol = :normal — richness of the output:
    • :minimal — sequence index and label only
    • :normal (default) — index, write timestamp, UUID prefix, label, and file path
    • :full — all of :normal plus last-access time, file size, and data format
  • io::IO = stdout
source
DataCaches.CacheAssets.ls — Method
DataCaches.CacheAssets.ls([cache::DataCache]; kwargs...) → Vector{CacheEntry}

Return a filtered and sorted vector of cache entries from cache. If cache is omitted, the active default cache is used (see active_autocache()).

Note

DataCaches.entries exposes the same functionality at the top level without needing to import CacheAssets.

Keyword arguments

Filtering:

  • pattern — Regex or String (converted to Regex); matched against label, description, then UUID. Pass nothing (default) to disable.
  • filepath_pattern — Regex or String; matched against the full absolute file path of each entry's backing data file.
  • filename_pattern — Regex or String; matched against only the filename (i.e. basename(path)) of each entry's backing data file.
  • before::DateTime — include only entries written before this time
  • after::DateTime — include only entries written after this time
  • accessed_before_date::DateTime — include only entries last accessed before this time
  • accessed_after_date::DateTime — include only entries last accessed after this time
  • labeled::Union{Nothing,Bool} = nothing — true for labeled entries only, false for unlabeled only, nothing for all
  • missing_file::Bool = false — when true, include entries whose backing file is absent
  • format::Union{Nothing,String,Regex} = nothing — String for exact format tag match (e.g. "csv", "jls"), Regex for pattern match (e.g. r"csv|json")

Sorting:

  • sortby::Symbol = :seq — sort criterion: :seq, :label, :date, :date_desc, :dateaccessed, :dateaccessed_desc, :size, :size_desc
  • rev::Bool = false — reverse the sort order

See also ls! for a display-oriented variant that prints to an IO stream.

source
DataCaches.CacheAssets.mv — Method
DataCaches.CacheAssets.mv([src_cache::DataCache,] src, dest; kwargs...)

Move or relabel a cache asset. src can be a CacheEntry, label String, UUID-prefix String, or sequence index Integer.

Relabel within the same cache (when dest is a String):

mv(cache, src, new_label::String; force=false)

Rename src to new_label in place. If new_label is already taken by a different entry, an error is raised unless force=true, in which case the conflicting entry is removed first.

Move to another cache (when dest is a DataCache):

mv(src_cache, src, dest_cache::DataCache; label="", force=false)

Read the asset from src_cache, write it to dest_cache (with a new UUID, sequence number, and datecached timestamp), then remove it from src_cache. The label kwarg overrides the destination label (default: preserve the original). Pass force=true to overwrite an existing entry with the same label in dest_cache.

If src_cache is omitted, active_autocache() is used as the source.

source
DataCaches.CacheAssets.purge! — Method
DataCaches.CacheAssets.purge!([cache::DataCache]; kwargs...) → DataCache

Bulk-delete cache entries matching the given criteria. All standard ls filtering keyword arguments are accepted to scope which entries are candidates for purging. When cache is omitted, the active default cache is used (see active_autocache()).

All removals are batched into a single index rewrite.

Standard filter kwargs (same as ls)

  • pattern, filepath_pattern, filename_pattern — label/path/filename pattern
  • format::Union{Nothing,String,Regex} — format tag filter (e.g. "jls", r"csv|json")
  • before, after — filter by datecached
  • accessed_before_date, accessed_after_date — filter by dateaccessed
  • labeled::Union{Nothing,Bool} — true = labeled only; false = unlabeled only

Purge criteria (applied after filtering)

  • stale::Bool = false — delete entries past their TTL (requires TTL to be configured)
  • max_age::Union{Nothing,Period} — delete entries whose age (by datecached) exceeds this
  • max_idle::Union{Nothing,Period} — delete entries that have not been accessed within this period (entries never accessed are treated as least-recently-used and are eligible)
  • keep_count::Union{Nothing,Int} — keep only the N most-recently-accessed entries among the candidates; delete the rest
  • max_size_bytes::Union{Nothing,Int} — purge LRU entries until the total size of the remaining candidates is at or below this limit

Options

  • keep_labeled::Bool = false — when true, labeled entries are never deleted, even if they match all other criteria
  • dry_run::Bool = false — print what would be deleted to io without deleting
  • io::IO = stdout — output stream for dry_run messages

Examples

dc = DataCache(:myproject)

# Remove all stale entries (requires TTL on entries or cache)
CacheAssets.purge!(dc; stale = true)

# Remove entries older than 30 days
CacheAssets.purge!(dc; max_age = Dates.Day(30))

# Keep only the 10 most recently accessed entries
CacheAssets.purge!(dc; keep_count = 10)

# Purge LRU entries until cache is under 100 MiB, keep labeled entries
CacheAssets.purge!(dc; max_size_bytes = 100 * 1024 * 1024, keep_labeled = true)

# Preview without deleting
CacheAssets.purge!(dc; max_age = Dates.Day(7), dry_run = true)

# Purge using default cache
CacheAssets.purge!(; keep_count = 50)

See also ls, rm, DataCaches.invalidate!, DataCaches.set_autopurge!.

source
DataCaches.CacheAssets.rm — Method
DataCaches.CacheAssets.rm([cache::DataCache,] assets...; force=false)
DataCaches.CacheAssets.rm([cache::DataCache,] specs::AbstractVector; force=false)

Remove one or more assets from cache (default: active default cache). Each asset can be specified as a CacheEntry, label String, UUID-prefix String, or sequence index Integer. The backing data file is also deleted from disk.

All removals are batched into a single index rewrite for efficiency.

The AbstractVector form accepts a vector of any mix of the above specifier types, which is convenient for operating on the result of ls.

Pass force=true to suppress errors for unresolvable specifiers (they are silently skipped).

source