Skip to content

If a cache and a type are passed the type is ignored (and error is incorrect if the type does not match the cache) #4

@orgadish

Description

@orgadish

If a cache and a type are passed the type is ignored (and error is incorrect if the type does not match the cache)

library(filecacher)

tmp <- tempfile()
dir.create(tmp)

RDS_CACHE <- file_cache(tmp, type="rds")
PARQUET_CACHE <- file_cache(tmp, type="parquet")

my_data <- data.frame(x=1, y=2)
label <- "my_data"

# Expect neither exist
!file.exists(file.path(tmp, paste0(label, ".cache_rds")))
#> [1] TRUE
!file.exists(file.path(tmp, paste0(label, ".cache_parquet")))
#> [1] TRUE

my_data |> with_cache(label, RDS_CACHE) |> invisible()

# Expect RDS exists but parquet does not
file.exists(file.path(tmp, paste0(label, ".cache_rds")))
#> [1] TRUE
!file.exists(file.path(tmp, paste0(label, ".cache_parquet")))
#> [1] TRUE

my_data |> with_cache(label, RDS_CACHE, type="parquet") |> invisible()

# Expect both exist
file.exists(file.path(tmp, paste0(label, ".cache_rds")))
#> [1] TRUE
file.exists(file.path(tmp, paste0(label, ".cache_parquet")))
#> [1] FALSE

my_data |> with_cache(label, PARQUET_CACHE) |> invisible()

# Now both exist, with explicitly different caches
file.exists(file.path(tmp, paste0(label, ".cache_rds")))
#> [1] TRUE
file.exists(file.path(tmp, paste0(label, ".cache_parquet")))
#> [1] TRUE

### INCORRECT ERROR MESSAGES

# Something that cannot be parsed into parquet
my_list <- list(a = 1, b = list(list(list(3))))

# Not supposed to work, but should give error. Likely error with glue.
my_list |> with_cache("my_list", PARQUET_CACHE)
#> Error in value[[3L]](cond):

# Error says "cache type ('rds')", but is actually checking against parquet.
my_list |> with_cache("my_list", PARQUET_CACHE, type="rds")
#> Error in value[[3L]](cond): Error in .cache$set(label, out): Error setting value for key "my_list".
#>  Check if the cache type ('rds') is compatible with the data being stored.

# Works when type is rds correctly
my_list |> with_cache("my_list", RDS_CACHE) |> invisible()

Created on 2025-01-29 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions