Skip to content

Commit

Permalink
version 0.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
orgadish authored and cran-robot committed Nov 10, 2023
1 parent 414d883 commit cae28fa
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 59 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: filecacher
Type: Package
Title: File Cacher
Version: 0.2.3
Version: 0.2.4
Authors@R:
person("Or", "Gadish", email = "orgadish@gmail.com", role = c("aut", "cre", "cph"))
Description:
Expand All @@ -21,8 +21,8 @@ Config/testthat/edition: 3
URL: https://github.com/orgadish/filecacher
BugReports: https://github.com/orgadish/filecacher/issues
NeedsCompilation: no
Packaged: 2023-11-09 08:17:04 UTC; orgadish
Packaged: 2023-11-09 19:44:22 UTC; orgadish
Author: Or Gadish [aut, cre, cph]
Maintainer: Or Gadish <orgadish@gmail.com>
Repository: CRAN
Date/Publication: 2023-11-09 10:30:02 UTC
Date/Publication: 2023-11-10 09:50:03 UTC
22 changes: 11 additions & 11 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
ab5e50bc7a3736ac7b611ea2b8663d75 *DESCRIPTION
8cecc3996f36164cd20081681f1b4abb *DESCRIPTION
a699489fbaa4c98f0be448cb32b5ce39 *LICENSE
030e9df7f37ee257cff118182f32630b *NAMESPACE
da6460df974ce8421fcc06a1bcca6a18 *NEWS.md
e379dcf0aba315f6d8e3c33dc239bcb1 *NEWS.md
3ed23ba66095a20862982211659b34b9 *R/cached_read.R
db56f626aaf2ceb024cc59d2fb749043 *R/file_cache.R
25ddb99327f915d34b0e566605bc21a7 *R/file_cache.R
7cd4505a59b22a915f14ec3bd64fff52 *R/interpret_type.R
57f8c3d43fca95fdb6eba3265ddde21f *R/util-dfs_equal.R
47ba852b5c44ae23c92ab08a6fb43ed7 *R/util-fns_equal.R
8e0b2dc83dd19e55a19f75d89c11e2dd *R/util-get_file_info.R
370946680ccdc94145b2051f41eacd6d *R/vectorize_reader.R
de2b0df4045c92ae0afc83c555991e67 *R/with_cache.R
1e871536b2d1ba54e17007186c3a29e5 *README.md
57800f2b7754c75111760f3ed038bafc *README.md
071c2fc2ab835760ab14c7ec44ab8d85 *inst/WORDLIST
6087ff9f569743b5b84d6e0114aea7e5 *inst/examples/cache.R
8b028e4421ceec1db98e204aba5adef2 *inst/examples/cached_read.R
64cddaad066ea0b9446d40bf27a3ca40 *inst/examples/cache.R
947ae3a30327a6ecb3034c3ac5aee724 *inst/examples/cached_read.R
0be50cdfc61e4711032a55aaef73fa01 *inst/examples/vectorize_reader.R
fa33100515046319623e73627fb82a50 *inst/extdata/create_extdata_from_iris.R
d100dbed6c5ce6f74e0ba16127fa7e16 *inst/extdata/iris_complete.csv
97a71b453f64dee5d29a4de436bbd739 *inst/extdata/iris_setosa_only.csv
892af90bb2a229c30e8140658e2fc1d4 *inst/extdata/iris_versicolor_only.csv
904ae0155a55b8618059a144f9e7e525 *inst/extdata/iris_virginica_only.csv
a910434f5b1f1ededf32aea20efa7195 *man/cached_read.Rd
1e6b776bf154a0592a78c79b37f77d63 *man/cached_read.Rd
f1f3a5d5d050fee4013f1a277d8baefd *man/dfs_equal.Rd
abcc22cc06204a12f6067331f6711798 *man/file_cache.Rd
ec923a0136b3cfde5658479f1f5e45b4 *man/file_cache.Rd
24db7f85a969168ff6c54e66fe88cb60 *man/fns_equal.Rd
ce98425ac564644c7590657217df6da2 *man/get_csv_fns.Rd
1cc345250e7fa7d4cb305749701754bb *man/get_csv_read_fn.Rd
d0f2ad5d3f676040e055272dfab3d664 *man/get_file_info.Rd
48108a8b173d37ed634da43bd2f2947b *man/interpret_cache_type.Rd
6bddb3b8d0d7c02d9c1db5da895d8d2c *man/vectorize_reader.Rd
17f9bab2ff57b42f977e0dc72c5ed760 *man/with_cache.Rd
aeab1d5787d0a6ae89e2aa1220a8fc44 *man/with_cache.Rd
7347c3a61edadfd418402153d613c76c *tests/testthat.R
a943b01dbddfcd0b5067767b3d47ed6a *tests/testthat/test-cached_read.R
e5321ba8ccb06d3fe9e9f0f720adfb5b *tests/testthat/test-file_cache.R
aaa295cbb60534a3c23d106548528ec6 *tests/testthat/test-file_cache.R
f1169a22866b9364f64c0f42d71f277b *tests/testthat/test-interpret_type.R
9b7423ed0fa99644850bdd05f0e3f6fc *tests/testthat/test-util-dfs_equal.R
3d1ff45ccb9389492678f3a49eba2c34 *tests/testthat/test-util-fns_equal.R
e11e606c90be9f3fb6d63a0ea0b60593 *tests/testthat/test-util-get_file_info.R
14d1411a2e03dc3e5846f8cec10db5ae *tests/testthat/test-vectorize_reader.R
5d2464789c4ea4ba83932c3125686316 *tests/testthat/test-with_cache.R
0cac3e8e82dee2e9c26938f0cc18a907 *tests/testthat/test-with_cache.R
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# filecacher 0.2.4

* `file_cache()` now uses a sub-folder "cache" rather than
the top directory.


# filecacher 0.2.3

* Initial CRAN submission.
13 changes: 9 additions & 4 deletions R/file_cache.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#' Gets or creates a `cachem` object for use with other functions.
#'
#' @param cache The path to an existing directory to use for caching.
#' If `NULL` (default) uses the current path, using [here::here()].
#'
#' For advanced use, also accepts (and passes on) an existing
#' `cachem` object. If so, all other parameters are ignored.
#' If `NULL` (default) uses a folder called "cache" in the current
#' path, using [here::here()]. The folder is created if it does not
#' already exist.
#'
#' **Advanced:** if an existing `cachem` object is provided, all other
#' parameters are ignored and the object is passed on as is. This
#' functionality is primarily used internally or for testing.
#' @inheritParams interpret_cache_type
#'
#' @return A [cachem::cache_disk()] object.
Expand All @@ -19,7 +23,8 @@ file_cache <- function(cache = NULL, type = NULL, ext_prefix = "cache_") {
}

if (is.null(cache)) {
cache <- here::here()
cache <- here::here("cache")
if (!dir.exists(cache)) dir.create(cache)
} else if (!is.character(cache) || !dir.exists(cache)) {
stop(
"`cache` must be an existing cache or the path to an existing directory."
Expand Down
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
# filecacher

<!-- badges: start -->

[![](https://cranlogs.r-pkg.org/badges/filecacher)](https://cran.r-project.org/package=filecacher)
<!-- badges: end -->

The main functions in this package are:

1. `with_cache`: Caches the expression in a local file on disk.
2. `cached_read`: A wrapper around a typical read function that caches
the result and the file list info. If the input file list info
hasn’t changed (including date modified), the cache file will be
read. This can save time if the original operation requires reading
from many files, or involves lots of processing.
1. `with_cache()`: Caches the expression in a local file on disk.
2. `cached_read()`: A wrapper around a typical read function that
caches the result and the file list info. If the input file list
info hasn’t changed (including date modified), the cache file will
be read. This can save time if the original operation requires
reading from many files, or involves lots of processing.

See examples below.

Expand All @@ -38,18 +40,21 @@ remotes::install_github("orgadish/filecacher")
## Example

``` r
EXAMPLE_DATA_FOLDER <- system.file("extdata", package = "filecacher")

# Example files: iris table split by species into three files.
IRIS_FILES_BY_SPECIES <- list.files(EXAMPLE_DATA_FOLDER, pattern = "_only[.]csv$", full.names = TRUE)
basename(IRIS_FILES_BY_SPECIES)
iris_files_by_species <- list.files(
system.file("extdata", package = "filecacher"),
pattern = "_only[.]csv$",
full.names = TRUE
)
basename(iris_files_by_species)
#> [1] "iris_setosa_only.csv" "iris_versicolor_only.csv"
#> [3] "iris_virginica_only.csv"


# Create a temporary directory to run these examples.
TEMP_DIR <- file.path(EXAMPLE_DATA_FOLDER, "temp")
dir.create(TEMP_DIR)
tf <- tempfile()
dir.create(tf)


something_that_takes_a_while <- function(x) {
Expand Down Expand Up @@ -95,42 +100,42 @@ time_pipeline <- function(pipeline_fn) {
print(function_name)

# Create a separate directory for the cache for this function.
cache_dir <- file.path(TEMP_DIR, paste0("temp_", function_name))
cache_dir <- tempfile(function_name, tmpdir = tf)
dir.create(cache_dir)

gc()

for (i in 1:3) {
print(system.time(pipeline_fn(IRIS_FILES_BY_SPECIES, cache_dir)))
print(system.time(pipeline_fn(iris_files_by_species, cache_dir)))
}
}

time_pipeline(normal_pipeline)
#> [1] "normal_pipeline"
#> user system elapsed
#> 0.048 0.009 0.570
#> 0.034 0.005 0.546
#> user system elapsed
#> 0.003 0.000 0.504
#> 0.002 0.001 0.505
#> user system elapsed
#> 0.003 0.001 0.503
#> 0.002 0.000 0.503
time_pipeline(pipeline_using_cached_read)
#> [1] "pipeline_using_cached_read"
#> user system elapsed
#> 0.553 0.049 1.146
#> 0.493 0.041 1.064
#> user system elapsed
#> 0.027 0.002 0.028
#> 0.030 0.003 0.042
#> user system elapsed
#> 0.009 0.001 0.009
#> 0.013 0.001 0.014
time_pipeline(pipeline_using_with_cache)
#> [1] "pipeline_using_with_cache"
#> user system elapsed
#> 0.008 0.002 0.510
#> 0.010 0.002 0.515
#> user system elapsed
#> 0.004 0.001 0.005
#> 0.009 0.001 0.009
#> user system elapsed
#> 0.006 0.001 0.006
#> 0.009 0.001 0.009


# Delete the temporary directory created to run these examples.
unlink(TEMP_DIR, recursive = TRUE)
unlink(tf, recursive = TRUE)
```
2 changes: 1 addition & 1 deletion inst/examples/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ cache$set("df2", mtcars) # Set objects using `$set`.
cache$get("df2") |>
all.equal(mtcars)

unlink(tf)
unlink(tf, recursive = TRUE)
2 changes: 1 addition & 1 deletion inst/examples/cached_read.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ iris_files |>
all.equal(iris)


unlink(tf)
unlink(tf, recursive = TRUE)
2 changes: 1 addition & 1 deletion man/cached_read.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions man/file_cache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/with_cache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 26 additions & 9 deletions tests/testthat/test-file_cache.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
test_that("file_cache with cache=NULL", {
expected_dir <- here::here("cache")
dir_already_exists <- fs::dir_exists(expected_dir)

.cache <- file_cache()
expect_equal(
fs::path_abs(.cache$info()$dir),
fs::path_abs(expected_dir)
)

if (!dir_already_exists) {
expect_true(fs::dir_exists(expected_dir))
.cache$destroy()
}
})

test_that("file_cache with type=NULL works", {
tf <- tempfile()
dir.create(tf)
Expand All @@ -8,7 +24,7 @@ test_that("file_cache with type=NULL works", {
cache$set("test", 3)
expect_true(fs::is_file(fs::path(tf, "test.cache_rds")))

unlink(tf)
unlink(tf, recursive = TRUE)
})

test_that("file_cache with type=rds works", {
Expand All @@ -21,7 +37,7 @@ test_that("file_cache with type=rds works", {
cache$set("test", 3)
expect_true(fs::is_file(fs::path(tf, "test.cache_rds")))

unlink(tf)
unlink(tf, recursive = TRUE)
})

test_that("file_cache with type=parquet works", {
Expand All @@ -34,7 +50,7 @@ test_that("file_cache with type=parquet works", {
cache$set("test", data.frame(x = 1:3))
expect_true(fs::is_file(fs::path(tf, "test.cache_parquet")))

unlink(tf)
unlink(tf, recursive = TRUE)
})

test_that("file_cache with type=csv, ext_prefix=NULL works", {
Expand All @@ -47,12 +63,13 @@ test_that("file_cache with type=csv, ext_prefix=NULL works", {
cache$set("test", data.frame(x = 1:3))
expect_true(fs::is_file(fs::path(tf, "test.csv")))

unlink(tf)
unlink(tf, recursive = TRUE)
})

test_that("file_cache with cache_mem does not error", {
expect_no_error({
mem_cache <- file_cache(cache = cachem::cache_mem())
})
expect_true(inherits(mem_cache, "cache_mem"))
test_that("file_cache passes existing cache object", {
cd <- cachem::cache_disk()
expect_identical(file_cache(cd), cd)

cm <- cachem::cache_mem()
expect_identical(file_cache(cm), cm)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-with_cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ test_that("with_cache works", {
expect_equal(run_with_cache(), "CHANGED")
expect_equal(run_with_cache(force = TRUE), "TEST")

unlink(tf)
unlink(tf, recursive = TRUE)
})

0 comments on commit cae28fa

Please sign in to comment.