---
title: "ffscrapr: Managing Cache"
author: Tan Ho
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ffscrapr: Managing Cache}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## Caching: It's Responsible Web Etiquette!

One of the many packages that inspired ffscrapr is Dmytro Perepolkin's [polite](https://dmi3kno.github.io/polite/) package, which sets out some pillars of responsible web etiquette, including introducing yourself, taking slowly, and never asking twice for the same thing.

ffscrapr gives you convenient routes to support these:

-   You can set a `user_agent` string with any connection to help identify yourself to the API platform (MFL actually gives you a higher rate limit if you [register with them as an API Client](https://api.myfantasyleague.com/2020/api_info)!)

-   You can set `rate_limit` options to help manage the load on the API (and to comply with any restrictions that APIs have set out)

-   Behind the scenes, ffscrapr is saving the results of expensive (i.e. slow, data-heavy) and stable (i.e. not likely to change rapidly) queries so that a) it can return things faster to you and b) minimize server load.

This vignette explains and helps you manage this caching.

## What is being cached? How long is it being cached for?

ffscrapr's core functions often loop over the same, stable endpoints to help return the data for each function - things like "player names/positions/teams", "franchise IDs/names", and "league scoring rules" are all used frequently. ffscrapr incorporates [memoise](https://github.com/r-lib/memoise) package to help with this functionality, and (as of this writing) saves the results of these functions:

-   `mfl_players()`, `sleeper_players()`, `fleaflicker_players()` - these functions are stored for 7 days
-   `ff_franchises()`, `ff_scoring()`, `ff_league()` - these functions are stored for 24 hours
-   `ff_standings()`, `ff_playerscores()`, `ff_starters()`, `ff_schedule()` , `ff_userleagues()` - these functions are stored for one hour
-   `dp_playerids()` and `dp_values()` - these functions are stored for 24 hours

You can find an updated list of cached functions in the [zzz.R](https://github.com/ffverse/ffscrapr/blob/main/R/zzz.R) source code page.

## Where is it being cached? 

By default, ffscrapr will save the results of each function call (i.e. function + arguments) into memory.

If you would like to have your cache stored on your local filesystem, you can adjust this behaviour by adding the following line to your (or your project) .Rprofile, or by adding it BEFORE you library/load the package.

```{r eval = FALSE}
options(ffscrapr.cache = "filesystem")
```

This will instruct ffscrapr to store the cache on your machine, as dictated by the typical cache location for your operating system. You can see where this is by running the following line of code:

```{r eval = FALSE}
rappdirs::user_cache_dir("ffscrapr")
```

## How can I clear the cache?

You can use the following function to clear the cache (whether it is in memory or on your filesystem):

```{r eval = FALSE}
.ffscrapr_clear_cache()
```

